This solution is straight from the last two examples in the FORFILES documentation ( forfiles /?). (Note output to screen, not appended to file) off & for /f "delims=*" %A in ('dir /s /b /a:-d') do echo %~fA %~zA Truncated Name and Size (4 of 6 files incorrect): off & for /f %A in ('dir /s /b /a:-d') do echo %~zA %~fA %~zAĢ1745440 C:\DeleteLater\NoSpacesLongName.txt 21745440Ģ8726032 C:\DeleteLater\FolderNoSpaces\3rdNoSpacesLongName.txt 28726032 (Without delims Includes suppressing directory per OP but with size both before and after filename for emphasis) I suppose I should really check the status of ECHO and reset it as well.īrian's proposed solution does not handle file and folder names containing spaces (at least not on my Vista configuration). Note: Appended to file just for grins as the OP asked for output to a file. Note: Made the FOR variable name uppercase to clarify the distinction between variable and variable parameters if someone chooses different variable names. Note: 2nd command also suppresses directories via /a:-d. Note: "delims=*" specifies a character not allowed in filenames. (Creating a file would be the same as in Alternative #2.) off & for /R "c:\deletelater\folder with spaces" %A in (*.*) do echo %~fA %~zAĮxample - Works (different directory than above to demonstrate recursion) off & for /R "c:\deletelater" %A in (*.*) do echo %~fA %~zAĬ:\DeleteLater\Name with Spaces.txt 19800676Ĭ:\DeleteLater\NoSpacesLongName.txt 21745440Ĭ:\DeleteLater\Folder with Spaces\2nd Name with Spaces.txt 5805492Ĭ:\DeleteLater\Folder with Spaces\2ndNoSpacesLongName.txt 3870322Ĭ:\DeleteLater\FolderNoSpaces\3rd Name with Spaces.txt 27874695Ĭ:\DeleteLater\FolderNoSpaces\3rdNoSpacesLongName.txt 28726032īrianAdkins' suggested: off & for /f %a in ('dir /s /b') do echo %~fa %~zaĪ corrected answer is: off & for /f "delims=*" %A in ('dir /s /b') do echo %~fA %~zAĪ more complete answer with directories suppressed and output (appended) to a file is: 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 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).īest(?) 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. bat file and calling it but that adds another file (although it does provide greater flexibility, as would a function). I tried using FOR TOKENS but then had to strip headers and footers so I reverted back to #1. %~ftzaI - expands %I to a DIR like output lineĪlternative #1: FOR /R is more intuitive than #2 for me.Īlternative #2: FOR /F fixes the problem with "spaces in names" in BrianAdkins' suggestion.Īlternative #3: FORFILES would be my pick except that the path is in double quotes.īrian or other gurus may have a more elegant solution or may be able to suggest a dozen other solutions but these three work. %~dp$PATH:I - searches the directories listed in the PATHĮnvironment variable for %I and expands to theĭrive letter and path of the first one found. %~fsI - expands %I to a full path name with short names only %~nxI - expands %I to a file name and extension only %~dpI - expands %I to a drive letter and path only The modifiers can be combined to get compound results: Search, then this modifier expands to the %~$PATH:I - searches the directories listed in the PATHĮnvironment variable and expands %I to theįully qualified name of the first one found. %~aI - expands %I to file attributes of file %~sI - expanded path contains short names only %~xI - expands %I to a file extension only %~fI - expands %I to a fully qualified path name %~I - expands %I removing any surrounding quotes (") You can now use the following optional syntax: It's not very nning it for folders containing a ton of files may be sketchy, so try it on small folders first.įrom the "for /?" help text (I used 'a' instead of 'I') In addition, substitution of FOR variable references has been enhanced. This should do it: off & for /f %a in ('dir /s /b') do echo %~fa %~za
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |