21

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
user3026965
  • 673
  • 4
  • 8
  • 22

6 Answers6

13

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
  • very nice! `(@For /F "Delims=" %A in ('dir /B/S/A-D') Do @Echo %~fA %~zA) > filelist.txt` – user3026965 May 11 '17 at 20:55
  • 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. – user3026965 May 12 '17 at 04:56
  • 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.txt` –  May 12 '17 at 07:08
  • 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.GetChildItemCommand` – user3026965 May 18 '17 at 23:29
  • Looks like you are using an earlier PS version. See changed answer. –  May 19 '17 at 10:03
11
Get-ChildItem -Recurse | select FullName,Length | Format-Table -HideTableHeaders | Out-File filelist.txt
Daniel Agans
  • 657
  • 1
  • 6
  • 14
  • 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. – user3026965 May 11 '17 at 03:55
  • even a simple text format with CSV or tab or one blank space separated fields are OK between the path\filename.txt filesize. – user3026965 May 11 '17 at 03:57
  • 1
    Believe you could just pipe to `convertto-preferredformat` – Daniel Agans May 11 '17 at 04:13
  • 1
    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. – user3026965 May 11 '17 at 04:17
  • 1
    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. – user3026965 May 11 '17 at 06:32
  • 2
    this does not list the size for me on Win 10 – JacobIRR Apr 23 '19 at 18:02
5

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://stackoverflow.com/a/23434457/7270462 for the tip about Export-Csv

ErrCode
  • 186
  • 2
  • 11
3
forfiles /s /c "cmd /c echo @path @fsize" >filelist.txt
franzo
  • 1,379
  • 1
  • 15
  • 30
  • This was the only version that worked for me. Maybe it's that I'm visiting from Unixland and my phrasebook is dated? – Parapluie Oct 21 '22 at 14:45
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.

Rumit Patel
  • 8,830
  • 18
  • 51
  • 70
Al Raja
  • 11
  • 1
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

Dimitry Ernot
  • 6,256
  • 2
  • 25
  • 37