Unable to execute FOR loop from batch file

I am trying to execute the following command from inside a batch file:

@echo off & for /f "delims=*" %A in ('dir /s /b') do echo %~fA %~zA >> "\path\to\output.txt"

I found this command from another thread. It prints out the entire file listing along with the file size.

I am able to execute this just fine when pasting into a command line window, but when I throw it into a batch file nothing happens. Am I missing something? Why would this failed when executed from a .bat?

0

2 Answers

Try %% e.g. for %%f rather than for %f, that might work In batch files you have to use for %%f rather than for %f The for %f is only for the command line. Change all references, so I suppose %%~zA rather than %~zA

You should really have troubleshooted and tried even a simple for loop, like for %a in (*.*) do @ECHO %a you'd then have realized it also worked in command line and not in a batch file and perhaps you'd have found out about the %% requirement.

Please try

Use % when running code from the windows command line.

Use %% when running code from the .bat script, inside a file.

Change inside a .bat file:

@echo off & for /f "delims=*" %%A in ('dir /s /b') do echo %%~fA %%~zA >> "\path\to\output.txt"

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like