loopsbatch-file

How to loop over a directory supplied via argument in batch?


I would like to loop through a directory (supplied by argument 1) and print the contents of the directory (I am using echo as a test; I will be using each file as an input for a command later on).

I want to run the command:

./script.bat C:/Foo/

Which will trigger the following (the for loop is where the help is needed)

@echo off

set arg1=%1
shift

cd /d %arg1%
for %%i in (*.*) do @echo %%i

Then list all of the files in the C:\Foo directory

Is this the best way of doing this? I would prefer to remove the cd call and have the loop iterate over %arg1, but I am not sure this is the batch way (I am a bash guy).


Solution

  • If anyone else is looking for this, I was able to achieve what I wanted by

    for /f %%i in ('dir /s/b %arg1%') do @echo %%i