VinnyGuitara VinnyGuitara - 3 years ago 401
PowerShell Question

Recursive directory listing with 8.3 shortname notation

I have a PS command:

PowerShell.exe "dir -Verbose -Recurse | Select-Object Mode, Length, Name, Directory | Export-Csv "listing.csv""

This works to give me a directory listing the way I want it but there is one caveat: If the directory structure or filename maxes out (I think its 260 chars) it does not list this file in the resulting csv and it throws an error.

Does anyone have any suggestions to make this compatible with 8.3 notation? I think this is the only way to get all the files.

Answer Source

I've dealt with this a bunch, 8.3 notation unfortunately still doesn't work in a lot of cases as the filenames could still be over 260 characters. It's possible to use drive mounting to shorten the filenames but that can be a bit of a pain. Best way I've found is to circumvent the problem using a .NET library called AlphaFS that does not have the 260 character limitation, you can read more or download it here. Will take a bit of getting used to if you haven't worked directly with .NET classes before but it's much better than trying weird hacks or dealing with 8.3 notation. Once you have the assembly loaded you would replace dir -verbose -recurse with ([Alphaleonis.Win32.Filesystem.Directory]::GetFiles((pwd).path,"*","AllDirectories")). Note that that will only get files, for directories you use the same code except replacing GetFiles with GetDirectories

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download