Page 1 of 1

for loop if file exists

Posted: 06 Jan 2015 20:28
by joecepy
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!
      )
   )
)

Re: for loop if file exists

Posted: 06 Jan 2015 20:36
by Squashman
If exist %%x

Re: for loop if file exists

Posted: 06 Jan 2015 20:45
by joecepy
Squashman wrote:If exist %%x


still reporting all NOT FOUND

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

Re: for loop if file exists

Posted: 06 Jan 2015 20:54
by Squashman
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.

Re: for loop if file exists

Posted: 06 Jan 2015 21:03
by joecepy
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!
      )
   )
)

Re: for loop if file exists

Posted: 06 Jan 2015 21:22
by Squashman
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.

Re: for loop if file exists

Posted: 06 Jan 2015 21:39
by joecepy
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

Re: for loop if file exists

Posted: 06 Jan 2015 21:46
by Squashman

Code: Select all

move %%x %%~dpx\Archive

Re: for loop if file exists

Posted: 06 Jan 2015 22:12
by joecepy
Squashman wrote:

Code: Select all

move %%x %%~dpx\Archive


You rock! have a good night sir.