jdhitsolutions / PSScriptTools

:wrench: :hammer: A set of PowerShell functions you might use to enhance your own functions and scripts or to facilitate working in the console. Most should work in both Windows PowerShell and PowerShell 7, even cross-platform. Any operating system limitations should be handled on a per command basis. The Samples folder contains demonstration script files
MIT License
901 stars 112 forks source link

[Request]: Get-FolderSizeInfo long file/folder names enabler #134

Closed scriptingstudio closed 1 year ago

scriptingstudio commented 1 year ago

Describe the request

Introduce new switch parameter, say, -EnableLn

# line 22
foreach ($item in $path) {
    if ($EnableLn) {
        $item = if ($item -match '^\\\\') {"\\?\UNC$($item.substring(2))"} else {"\\?\$item"}
    }

PowerShell version

5.1

Platform

Windows 10 Pro or Enterprise

UPDATE

missed one slash after "UNC" $item = if ($item -match '^\\\\') {"\\?\UNC\$($item.substring(2))"} else {"\\?\$item"}

jdhitsolutions commented 1 year ago

Can you give me an example I can duplicate?

scriptingstudio commented 1 year ago

Well, yesterday I tried to analyze the corporate file storage, and in order not to reinvent the wheel, I remembered your script. Our storage has a lot of long filenames and the script was throwing errors. Then I searched with the prefix \\?\UNC\ and the errors gone. In order not to remember every time about prefixes I added a new parameter.

https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=registry

EXAMPLES

\\?\C:\Windows
\\?\UNC\server\share
scriptingstudio commented 1 year ago

offtopic

line 94 $data = $($d.GetFiles()).Where({$_.attributes -notmatch "hidden"}) I believe it should be @ instead of $ $data = @($d.GetFiles()).Where({$_.attributes -notmatch "hidden"})

You say, There appears to be a bug with the array list in Windows PowerShell. Did you try [system.collections.generic.list[object]]?

Why are you declaring _enumdir function inside of cycle and not in begin block?

jdhitsolutions commented 1 year ago

I haven't looked at this code in awhile. I think I wrote much of it as I visualized the process. Probably does make sense to move the private _enumdir function. It is only used in Windows PowerShell 5.1 but it won't hurt to define it.

jdhitsolutions commented 1 year ago

I switched to System.Collections.Generic.List[] instead of the array.

jdhitsolutions commented 1 year ago

I have a better understanding of the long file name format. I will need to treat the path as a literal path.

jdhitsolutions commented 1 year ago

@scriptingstudio Would it make more sense to use add a parameter set and introduce -LiteralPath?

scriptingstudio commented 1 year ago

You have already mixed literal and regex notation by using -literalpath parameter in Convert-Path. You will have to introduce parameter sets or at least to somehow parameterize Convert-Path selecting path/literalpath that can overcomplicate your code.

My primary goal was to scan a storage having long file names and I added the parameter. It worked perfectly.

jdhitsolutions commented 1 year ago

I'm realizing now how much I've kludged together in this command.

jdhitsolutions commented 1 year ago

The command is not the most elegant code I've ever written, but it works. I have published v2.46.0 to the PowerShell Gallery with a revised version of Get-FolderSizeInfo. I don't have an environment to test it so thoroughly let me know if this works better for you.

scriptingstudio commented 1 year ago

For my purposes it works. -EnableLongFileName parameter works.

jdhitsolutions commented 1 year ago

I forgot to add a parameter alias, EnableLn. I'll add that in the next release.

jdhitsolutions commented 1 year ago

Added the EnableLN parameter alias in v2.47.0