List files with path and file size only in Command Line
Asked Answered
T

6

23

Windows Command Line (or maybe PowerShell).

How can I list all files, recursively, with full path and filesize, but without anything else and export to a .txt file. Much preferably a code that works for whichever current directory I am in with the Command Line (so does not require manual entering of the target directory).

None of these provides path\filename and filesize only:

dir /s > filelist.txt
dir /s/b > filelist.txt
dir /s/o:-d > filelist.txt

Desired output (fullpath\file.ext filesize):

c:\aaa\file.ext 7755777    
c:\aaa\bbb\1.txt 897667
c:\aaa\bbb\2.ext 67788990
c:\aaa\bbb\nnn\a.xls 99879000
Toffeenosed answered 11/5, 2017 at 0:35 Comment(0)
D
13
Get-ChildItem -Recurse | select FullName,Length | Format-Table -HideTableHeaders | Out-File filelist.txt
Donaugh answered 11/5, 2017 at 2:36 Comment(6)
Awesome! The only cosmetic issue is that the output places an extra blank line between all filenames. Any way to get rid of these blank lines? I am dealing with many TBs and >500K different files.Toffeenosed
even a simple text format with CSV or tab or one blank space separated fields are OK between the path\filename.txt filesize.Toffeenosed
Believe you could just pipe to convertto-preferredformatDonaugh
one other functional issue: if the path or filename is too long, then the path\filename.ext is trimmed and followed by ... just before the filesize (length) column. for example: C:\aaa\bbb\ccc\verylongfilena... 99999999. any possible fix? thank you.Toffeenosed
this works very nicely: Get-ChildItem -Recurse | select Length,LastWriteTime,FullName | Format-Table -Wrap -AutoSize | Out-File filelist.txt very long path\filename lines are broken into a new line (wrap), but not truncated.Toffeenosed
this does not list the size for me on Win 10Ericaericaceous
R
14

PowerShell:

gci -rec -file|%{"$($_.Fullname) $($_.Length)"} >filelist.txt

earlier PowerShell versions:

gci -rec|?{!$_.PSIsContainer}|%{"$($_.Fullname) $($_.Length)"} >filelist.txt

Batch file:

(@For /F "Delims=" %%A in ('dir /B/S/A-D') Do @Echo %%~fA %%~zA) >filelist.txt

Cmdline

(@For /F "Delims=" %A in ('dir /B/S/A-D') Do @Echo %~fA %~zA) >filelist.txt
Renwick answered 11/5, 2017 at 10:0 Comment(6)
very nice! (@For /F "Delims=" %A in ('dir /B/S/A-D') Do @Echo %~fA %~zA) > filelist.txtToffeenosed
unfortunately, in CLI, (@For /F "Delims=" %A in ('dir /B/S/A-D') Do @Echo %~fA %~zA) > filelist.txt does not perform in large subdir trees. tried 1 TB/100K files, kept running for hours until i terminated with ctrl c. still works on small dir trees.Toffeenosed
You might try these two lines Type nul >filelist.txt and @For /F "Delims=" %A ('dir /B/S/A-D ') Do @Echo %~fA %~zA >>filelist.txtRenwick
in powershell, i get the following error: Get-ChildItem : A parameter cannot be found that matches parameter name 'file'. At line:1 char:15 + gci -rec -file <<<< |%{"$($_.Fullname) $($_.Length)"} > filelist.txt + CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommandToffeenosed
Looks like you are using an earlier PS version. See changed answer.Renwick
If you want to import this exported txt into something like Excel, separating the filename from the size with a tab (`t) instead of a space will be easier if using the text-to-columns function.Haworth
D
13
Get-ChildItem -Recurse | select FullName,Length | Format-Table -HideTableHeaders | Out-File filelist.txt
Donaugh answered 11/5, 2017 at 2:36 Comment(6)
Awesome! The only cosmetic issue is that the output places an extra blank line between all filenames. Any way to get rid of these blank lines? I am dealing with many TBs and >500K different files.Toffeenosed
even a simple text format with CSV or tab or one blank space separated fields are OK between the path\filename.txt filesize.Toffeenosed
Believe you could just pipe to convertto-preferredformatDonaugh
one other functional issue: if the path or filename is too long, then the path\filename.ext is trimmed and followed by ... just before the filesize (length) column. for example: C:\aaa\bbb\ccc\verylongfilena... 99999999. any possible fix? thank you.Toffeenosed
this works very nicely: Get-ChildItem -Recurse | select Length,LastWriteTime,FullName | Format-Table -Wrap -AutoSize | Out-File filelist.txt very long path\filename lines are broken into a new line (wrap), but not truncated.Toffeenosed
this does not list the size for me on Win 10Ericaericaceous
U
6

OP's chosen answer using PowerShell (and their comment that they used Get-ChildItem -Recurse | select Length,LastWriteTime,FullName | Format-Table -Wrap -AutoSize | Out-File filelist.txt) was almost what I wanted for processing in Excel. Thanks for that part.

Unfortunately, (as they mentioned) the output had wrapped lines for long file paths, which isn't quite what I wanted.

The following command will produce a CSV formatted file (no wrapped lines):

Get-ChildItem -Recurse | select Length,LastWriteTime,FullName | Export-Csv -path filelist.csv -NoTypeInformation

Kudos to https://mcmap.net/q/585386/-powershell-format-table-to-csv for the tip about Export-Csv

Unlash answered 10/7, 2020 at 14:40 Comment(0)
A
3
forfiles /s /c "cmd /c echo @path @fsize" >filelist.txt
Achernar answered 11/6, 2019 at 19:27 Comment(1)
This was the only version that worked for me. Maybe it's that I'm visiting from Unixland and my phrasebook is dated?Sucrose
L
1

The following removes the wrapping issue you have:

Get-ChildItem -Recurse | select Length,LastWriteTime,FullName | Format-Table -Wrap -AutoSize | out-string -width 4096 | clip

Have a look at this Reference.

Lightning answered 3/3, 2018 at 6:46 Comment(0)
L
0

Go to folder and shift+Right Click ---> Powershell and then put in this code.

gci -rec -file|%{"$($_.Length)~$($_.Name)~$($_.FullName)"} >filelist.txt

Ctrl+A then Ctrl+C ---> Copy into Excel

Labannah answered 2/5, 2019 at 14:44 Comment(2)
This is an adjustment of @LotPings comment that worked for me.Labannah
You will need to go to the data tab and use "text to column" --> delimited and instead of separating on "tab" change it to "other" and use "~"Labannah

© 2022 - 2024 — McMap. All rights reserved.