Alternative #1: FOR /R is more intuitive than #2 for me.
Alternative #2: FOR /F fixes the problem with "spaces in names" in BrianAdkins' suggestion.
Alternative #3: FORFILES would be my pick except that the path is in double quotes.
Brian or other gurus may have a more elegant solution or may be able to suggest a dozen other solutions but these three work. I tried using FOR TOKENS but then had to strip headers and footers so I reverted back to #1. I also considered creating a small .bat file and calling it but that adds another file (although it does provide greater flexibility, as would a function).
I tested all alternatives with directory and filenames with embedded spaces, a 200+ character filename, a filename with no extension, and on root of a small drive (just for time; a little slow -- just as Brian suggested -- but then so is searching in Windows Explorer; that is why I installed Everything search app).
Alternative #1: FOR /R
Best(?) While trying to figure out why Brian's solution didn't work for me I looked at HELP FOR and decided to try the /R approach. (Creating a file would be the same as in Alternative #2.)
@echo off & for /R "c:\deletelater\folder with spaces" %A in (*.*) do echo %~fA %~zA
Example - Works (different directory than above to demonstrate recursion)
@echo off & for /R "c:\deletelater" %A in (*.*) do echo %~fA %~zA
c:\DeleteLater\Name with Spaces.txt 19800676
c:\DeleteLater\NoSpacesLongName.txt 21745440
c:\DeleteLater\Folder with Spaces\2nd Name with Spaces.txt 5805492
c:\DeleteLater\Folder with Spaces\2ndNoSpacesLongName.txt 3870322
c:\DeleteLater\FolderNoSpaces\3rd Name with Spaces.txt 27874695
c:\DeleteLater\FolderNoSpaces\3rdNoSpacesLongName.txt 28726032
Alternative #2: FOR /F
BrianAdkins' suggested: @echo off & for /f %a in ('dir /s /b') do echo %~fa %~za
A corrected answer is:
@echo off & for /f "delims=*" %A in ('dir /s /b') do echo %~fA %~zA
A more complete answer with directories suppressed and output (appended) to a file is:
@echo Results on %DATE% for %CD% >> YourDirFile.txt & echo off & for /f "delims=*" %A in ('dir /s /b /a:-d') do echo %~fA %~zA >> YourDirFile.txt
Note: "delims=*" specifies a character not allowed in filenames.
Note: 2nd command also suppresses directories via /a:-d.
Note: Made the FOR variable name uppercase to clarify the distinction between variable and variable parameters if someone chooses different variable names.
Note: Appended to file just for grins as the OP asked for output to a file.
I suppose I should really check the status of ECHO and reset it as well.
Issue - Spaces in Names
Brian's proposed solution does not handle file and folder names containing spaces (at least not on my Vista configuration).
Example - Wrong
(Without delims; Includes suppressing directory per OP but with size both before and after filename for emphasis)
Truncated Name and Size (4 of 6 files incorrect):
@echo off & for /f %A in ('dir /s /b /a:-d') do echo %~zA %~fA %~zA
C:\DeleteLater\Name
21745440 C:\DeleteLater\NoSpacesLongName.txt 21745440
C:\DeleteLater\Folder
C:\DeleteLater\Folder
C:\DeleteLater\FolderNoSpaces\3rd
28726032 C:\DeleteLater\FolderNoSpaces\3rdNoSpacesLongName.txt 28726032
Example - Correct
(Note output to screen, not appended to file)
@echo off & for /f "delims=*" %A in ('dir /s /b /a:-d') do echo %~fA %~zA
C:\DeleteLater\Name with Spaces.txt 19800676
C:\DeleteLater\NoSpacesLongName.txt 21745440
C:\DeleteLater\Folder with Spaces\2nd Name with Spaces.txt 5805492
C:\DeleteLater\Folder with Spaces\2ndNoSpacesLongName.txt 3870322
C:\DeleteLater\FolderNoSpaces\3rd Name with Spaces.txt 27874695
C:\DeleteLater\FolderNoSpaces\3rdNoSpacesLongName.txt 28726032
Alternative #3: FORFILES (Quote Issue)
This solution is straight from the last two examples in the FORFILES documentation (forfiles /?
).
FORFILES /S /M *.doc /C "cmd /c echo @fsize"
FORFILES /M *.txt /C "cmd /c if @isdir==FALSE notepad.exe @file"
Combining these examples and writing to a file yields the answer (almost):
forfiles /s /c "cmd /c if @isdir==FALSE echo @path @fsize" >>ForfilesOut.txt
Note that the path is in quotes in the output.
Does not matter whether echo on
or echo off
is toggled.
Adding a blank line separating each directory would be a trivial extension of the IF.
Caution: Using the mask /m *.*
will not return files without extension (like last file in example)!
Aside: This writes a file in each directory with contents of just that directory:
forfiles /s /c "cmd /c if @isdir==FALSE echo @path @fsize >>ForfilesSubOut.txt"
Not what the OP wanted but sometimes handy.
Example - Works (but with fullpath in quotes)
forfiles /s /c "cmd /c if @isdir==FALSE echo @path @fsize"
"c:\DeleteLater\Name with Spaces.txt" 19800676
"c:\DeleteLater\NoSpacesLongName.txt" 21745440
"c:\DeleteLater\Folder with Spaces\2nd Name with Spaces.txt" 5805492
"c:\DeleteLater\Folder with Spaces\2ndNoSpacesLongName.txt" 3870322
"c:\DeleteLater\FolderNoSpaces\3rd Name with Spaces.txt" 27874695
"c:\DeleteLater\FolderNoSpaces\3rdNoSpacesLongName.txt" 28726032
"c:\DeleteLater\MoreFiles\A really really long file name that goes on and on 123456789 asdfghjkl zxcvnm qwertyuiop and still A really really long file name that goes on and on 123456789 qwertyuiop and still further roughly 225 characters by now.txt" 447
"c:\DeleteLater\MoreFiles\New Text Document no extension" 0
This example includes an extra directory with a super long filename and a filename with no extension.
Issue: Path in Quotes
So, is there an easy way to remove the unwanted(?) quotes per the OP example and save Alternative #3: FORFILES. (Rhetorical question: Are the quotes a feature or a flaw?)