for loop if file exists

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
joecepy
Posts: 27
Joined: 28 Sep 2012 13:58

for loop if file exists

#1 Post by joecepy » 06 Jan 2015 20:28

Anyone know why the following reports NOT FOUND for all files that are in the log file? I know they exist in subdirectory so /r should recurse through the subdirectory but it always report NOT FOUND

Code: Select all

setlocal enabledelayedexpansion

for /f "tokens=* delims=" %%f in (.\transferFile.log) do (   
   for /r %%x in (%%f) do (
      if exist %%f (
         echo %%f FOUND!
      ) else (
         echo %%f NOT FOUND!
      )
   )
)

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: for loop if file exists

#2 Post by Squashman » 06 Jan 2015 20:36

If exist %%x

joecepy
Posts: 27
Joined: 28 Sep 2012 13:58

Re: for loop if file exists

#3 Post by joecepy » 06 Jan 2015 20:45

Squashman wrote:If exist %%x


still reporting all NOT FOUND

edit: my bad you're a genius that worked thanks

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: for loop if file exists

#4 Post by Squashman » 06 Jan 2015 20:54

joecepy wrote:
Squashman wrote:If exist %%x


still reporting all NOT FOUND

edit: my bad you're a genius that worked thanks

IF you didn't use %%x there would have been no point in doing the extra FOR command.

joecepy
Posts: 27
Joined: 28 Sep 2012 13:58

Re: for loop if file exists

#5 Post by joecepy » 06 Jan 2015 21:03

so what is going on here...
for debugging I echo %%f and it prints out the path of %sTransferLogFile% rather than the content. Any thoughts?
EDIT: so I resolved the first for loop by using 'type %sTransferLogFile% '
but now I'm getting issue with second for loop. I tried single quote but doesn't seem to like the for /r %%x in ('D:\TestSearch\%%f')

Code: Select all

SET sTransferLogFile="D:\TransferLog_%timestamp%.log"

for /f "tokens=* delims=" %%f in (%sTransferLogFile%) do (
echo %%f 
   :: can I do this??
   for /r %%x in ("D:\TestSearch\%%f") do (
      if exist %%x (
         echo %%x file found!!
      ) else (
         echo %%x ERROR: NOT FOUND!
      )
   )
)

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: for loop if file exists

#6 Post by Squashman » 06 Jan 2015 21:22

You specify the path you want to search after the /R option.
FOR /R [[drive:]path] %variable IN (set) DO command [command-parameters]

Walks the directory tree rooted at [drive:]path, executing the FOR
statement in each directory of the tree. If no directory
specification is specified after /R then the current directory is
assumed. If set is just a single period (.) character then it
will just enumerate the directory tree.

joecepy
Posts: 27
Joined: 28 Sep 2012 13:58

Re: for loop if file exists

#7 Post by joecepy » 06 Jan 2015 21:39

Squashman wrote:You specify the path you want to search after the /R option.
FOR /R [[drive:]path] %variable IN (set) DO command [command-parameters]

Walks the directory tree rooted at [drive:]path, executing the FOR
statement in each directory of the tree. If no directory
specification is specified after /R then the current directory is
assumed. If set is just a single period (.) character then it
will just enumerate the directory tree.


ahh gotcha thanks.
Last question for tonight. So I added a statement after

Code: Select all

 if exist %%x (
         move %%x .\Archive
) else ( ...


I'd like to move it to one level up but it keeps putting it in .\Archive directory where the script is running.
so script is ran in D:\script\
and these files are located
D:\folder1\project\
--project1.txt
--project2.txt
D:\folder1\project\Archive\ <-- move project here

D:\folder2\program\
--program2.txt
--program3.txt
D:\folder2\program\Archive <-- move program here
Last edited by joecepy on 06 Jan 2015 21:48, edited 1 time in total.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: for loop if file exists

#8 Post by Squashman » 06 Jan 2015 21:46

Code: Select all

move %%x %%~dpx\Archive

joecepy
Posts: 27
Joined: 28 Sep 2012 13:58

Re: for loop if file exists

#9 Post by joecepy » 06 Jan 2015 22:12

Squashman wrote:

Code: Select all

move %%x %%~dpx\Archive


You rock! have a good night sir.

Post Reply