Normally I would have to call a command once per folder like this:

processdata.cmd C:\data\10-2-2019-run1

processdata.cmd C:\data\10-2-2019-run2

processdata.cmd C:\data\10-2-2019-run3


I want to write a for loop that will run that command once per folder and append the directory just like in the commands above

I don't want it to loop through sub-folders

The only folders there contain data, so I don't need it to exclude any folders or files.

Just a very simple For loop using windows CMD (preferably not powershell)

How can I do this?



Try this.

for /D %G in ("c:\data\*") do processdata.cmd "%~fG"

  • thnx, but I'm getting error: The following usage of the path operator in batch-parameter substitution is invalid: %~fG" For valid formats type CALL /? or FOR /? The syntax of the command is incorrect.
    – Mich
    Jul 26 '19 at 23:18
  • I read this and added another % for batch file, still getting an error stackoverflow.com/questions/30049351/… do process.cmd "%~fG" was unexpected at this time.
    – Mich
    Jul 26 '19 at 23:21
  • 1
    I got it, for batch file had to for /D %%G in ("c:\data\*") do processdata.cmd "%%G"
    – Mich
    Jul 26 '19 at 23:24
  • Great! Glad you were able to get it working. You need one % if the command is issued from the command-line, two if used in a batch file. The ~f I had between the % and the G is to substitute the full pathname of the folder, instead of just the folder name without the path. But if you run the processdata.cmd from the folder containing the folders you want to operate upon, you don't need to do that.
    – Jim
    Jul 27 '19 at 1:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.