1

I have an applications folder that have more than 10 applications in. I want to list all files (including sub-folders) with directory and size info and save it under each application folder.

Here is the script (it is in C:\Users\xxxxxx\Desktop\apps)

$FolderList = Get-ChildItem -Directory
foreach ($folder in $FolderList)
{
    $thisPath = Get-Location
    Get-ChildItem -File -Filter * -Path $folder -Recurse |
    Sort-Object -Property Length -Descending|
    Select-Object -Property FullName, Length, @{l='Length (MB)';e={$_.Length/1MB}} | 
    Format-Table -AutoSize | 
    Out-File $thisPath\fileList_$folder.txt
}

Output:

FullName - Length - Length (MB)


C:\Users\xxxxxx\Desktop\apps\3\VSCodeUserSetup-x64-1.62.2.exe 79944464 76.2409820556641 C:\Users\xxxxxx\Desktop\apps\3\son.zip 18745870 17.8774547576904

It does what I want but in some of the outputs where the path is too long it didn't write the length or even full path.

FullName


C:\Users\xxxxxx\Desktop\apps\3\xxxxxxxxxxx/xxxxxx/xxxxxxxxxxxxxx/xxxxxxxxxxx/VSCodeUserSetu...
C:\Users\xxxxxx\Desktop\apps\3\son.zip

As I searched I saw that there is a char limit. How can I overcome this issue?

user1125953
  • 585
  • 2
  • 7
  • 18
  • Can this article help in your case? https://serverfault.com/questions/1087899/handle-exception-with-long-paths-in-powershell/1089831#1089831 – ZivkoK Jan 21 '22 at 10:06
  • I tried adding it to 'FullName' on 7th line but it didn't work. I'm not sure how to add it :/ – user1125953 Jan 21 '22 at 10:11
  • try this: Get-ChildItem -File -Filter * -Path "\\?\$($folder)" -Recurse ... If you have the required version of WMF and .NET FW – ZivkoK Jan 21 '22 at 10:27
  • I got this error: Get-ChildItem : Cannot retrieve the dynamic parameters for the cmdlet. Cannot process argument because the value of argument "path" is not valid. Change the value of the "path" argument and run the operation again. I cannot update the version unfortunately. – user1125953 Jan 21 '22 at 10:32
  • So you will have to use the workaround described in the article (substitute part of the path) – ZivkoK Jan 21 '22 at 10:43

1 Answers1

3

The -Path parameter is defined as a string array, but you are feeding it a DirectoryInfo object.

The second part of your question is about truncation, which happens because you use Format-Table -AutoSize on the data.

Format-* cmdlets are designed to display output on the console window only which has a limited width and all items longer are truncated.
Rule of Thumb: never use Format-* cmdlets when you need to process the data later.

Instead of saving a formatted table to a text file, I would suggest saving the (object) info as structured CSV file which makes working with the data later MUCH easier and without truncation. (you can for instance open it in Excel)

# just get an array of Full pathnames
$FolderList = (Get-ChildItem -Directory).Name #instead of Fullname
foreach ($folder in $FolderList) {
    # create the path for the output
    $fileOut = Join-Path -Path $folder -ChildPath ('filelist_{0}.csv' -f $folder)
    Get-ChildItem -File -Path $folder -Recurse |
    Sort-Object -Property Length -Descending |
    Select-Object -Property FullName, Length, @{l='Length (MB)';e={$_.Length/1MB}} | 
    Export-Csv -Path $fileOut -NoTypeInformation -UseCulture
}

I added switch -UseCulture to the Export-Csv cmdlet so you can afterwards simply double-click the csv file to open it in your Excel

user1125953
  • 585
  • 2
  • 7
  • 18
Theo
  • 57,719
  • 8
  • 24
  • 41
  • Thank you so much for the details, I understood the problem better. Now I have this error: Export-Csv : The given path's format is not supported. I removed -UseCulture and tried it, result is the same. $fileOut seems like: "C:\Users\xxxxxx\Desktop\apps\3\filelist_C:\Users\xxxxxx\Desktop\apps\3.csv" – user1125953 Jan 21 '22 at 11:20
  • It worked. Instead of $FolderList = (Get-ChildItem -Directory).FullName, I used $FolderList = (Get-ChildItem -Directory).Name and got the csv files. Thank you so much again. – user1125953 Jan 21 '22 at 11:22