Page 1 of 1

Looping through files in a directory

Posted: 10 Sep 2011 01:53
by donyagir
Hi,

I am trying to loop through some files in a directory as such:

@echo off
set direc=C:/Users/jackie/Desktop/test
for /f %%a IN (%direc%) do echo %%a
pause


but I get this error: The system cannot find the file C:/Users/jackie/Desktop/test.

Can someone help me figure this out? I am trying to get to every file in the test directory, but its telling me it can't find the directory itself!!! When I change /f to /r, then it sees everything under Desktop...Can someone help?

Re: Looping through files in a directory

Posted: 10 Sep 2011 09:38
by aGerman
With FOR /F you have to loop against the output of DIR in this case.

Code: Select all

for /f "delims=" %%a in ('dir /a-d /b /s "%direc%"') do echo %%a

FOR /R has a different syntax

Code: Select all

for /r "%direc%" %%a in (*.*) do echo %%a

Regards
aGerman