Question

I need to extract the fileversion of certain DLL files of which I do not know the name. All I know is where the file that holds the fileversion is located and that its name is the same as the folder it is hiding beneath.

In truth it's something like this:

C:\inetpub\wwwroot\APPFolder\bin\files (where the name of the Subfolder matches the name of the file in the bin folder)

After many tries (and a lot of failures), I've gone a bit back and forth, and I'm now leaning comfortably at this bit of code .... close, but no cigar:

Get-ChildItem -Path c:\temp\Documents -recurse -Filter *.dll | where-object{ (Get-ChildItem -Path c:\temp\Documents -recurse -Filter *.dll) -match $_.Directory.Name }

This code searches for all the files recursively under \temp\documents and then matches the files in the folders, which is nice ... but not quite what I wanted. Also the code above gives the folders where the files that match the folders they are in and not only the matched file.

So ... any suggestions? There are more than one DLL file in the bin folder mentioned above which is part of why I need to select the one with the same name as the APPFolder.

Was it helpful?

Solution

Here is a code sample which will give you the full file path to the files you're looking for.

    dir . -Recurse -Filter *.dll | ? { -not $_.PSIsContainer } | ? { $_.FullName.ToLower().Contains( [IO.Path]::DirectorySeparatorChar + [IO.Path]::GetFileNameWithoutExtension( $_.Name.ToLower() ) + [IO.Path]::DirectorySeparatorChar ) } | Select -ExpandProperty FullName

Breaking it down, we have:

dir . -Recurse -Filter *.dll

This will search the current directory and its children recursively for all files that have a .dll file extension:

 ? { -not $_.PSIsContainer }

This will eliminate directories that end in .dll. Unlikely, but possible

? { $_.FullName.ToLower().Contains( [IO.Path]::DirectorySeparatorChar + [IO.Path]::GetFileNameWithoutExtension( $_.Name.ToLower() ) + [IO.Path]::DirectorySeparatorChar }

This will filter it down to the DLL files that have a file name the same as the name of a directory above it in the path.

 Select -ExpandProperty FullName

This will give you the full file path.

You can then follow the answer to this question here to get the FileVersion.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top