0

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SkyRaider
  • 113
  • 3
  • 11

1 Answers1

0

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Travis
  • 646
  • 2
  • 7
  • 17
  • Alright :) Atleast it gave just the one file I wanted ... If the file was under the same folder as the name implied. – SkyRaider Dec 13 '13 at 07:22
  • The only problem with that one is that the files I need to match is under another folder. Like you said "This will filter it down to the dlls that have a file name the same as the name of the directory it is in", while I need something like "This will filter it down to the dlls that have a file name the same as the name of the directory it is in and it's subdirectories". The dll files are located one step below the folder with the same name. – SkyRaider Dec 13 '13 at 07:41
  • Apparently it lists the subdirectories with just $_.Directory.Name, or just GetFileNameWithout..... but when trying to match one to the other, it only matches the folder which it is in ( one folder under the main folder ) – SkyRaider Dec 13 '13 at 08:14
  • So if you had C:\foo\bar.DLL and C:\foo\bar\ , you'd want that DLL name? What about C:\foo\bar\foo.DLL? I guess I did not fully understand your requirements. – Travis Dec 13 '13 at 13:30
  • yes :) the C:\foo\bar\foo.DLL one, while searching for them in the c:\ directory. That's the problem. Your solution was great for for finding the files under the next folder, just not for the files under the folder in the next folder. – SkyRaider Dec 15 '13 at 07:46
  • Okay, I've edited the post with some slightly different code, and will pick up that example. – Travis Dec 15 '13 at 15:37
  • Started to test it, but was missing the end parantheses of one of the sentences. I tried inserting it where PowerShell suggested but that didn't work. It's the one in the last part "Contains" Contains **(** [IO.Path]::DirectorySeparatorChar + [IO.Path]::GetFileNameWithoutExtension **(** $_.Name.ToLower() **)** + [IO.Path]::DirectorySeparatorChar } - The suggestion said to insert **)** before the last } but after ..SeperatorChar – SkyRaider Dec 18 '13 at 12:43
  • It goes after the second [IO.Path]::DirectorySeparatorChar, before the '}'. Thanks for pointing that out. – Travis Dec 18 '13 at 13:13
  • Worked better than hoped. Even had to try it at C:\ ... I got alot of filenames repeating themselves under foldernames :) – SkyRaider Dec 19 '13 at 13:45