Looping through files in a directory

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
donyagir
Posts: 1
Joined: 10 Sep 2011 01:45

Looping through files in a directory

#1 Post by donyagir » 10 Sep 2011 01:53

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?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Looping through files in a directory

#2 Post by aGerman » 10 Sep 2011 09:38

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

Post Reply