Check if multiply files exist batch

August 21, 2010 at 05:58:44
Specs: Windows 7, Q9550 / 4 Gig
Hey,

I have often to deal with the following routine in batch scripts I make: I have several files in a folder together with my script. When script runs it checks if those files exist in the previous folder of the path and if not it copies them there. Until now I am doing this using the command:

cd..
IF EXIST file1 (GOTO con1) ELSE (GOTO 1)
:1
cd myfolder
copy file1 .\..
cd..
:con1

and so on for file2, file3, etc.

Is there a way to save my time creating a shorter script that will do the job?

Btw I use parameters like .\., .\.., etc. I know so far only those with a range a couple of folders before or after the working folder in path. Are there more with a wider range (more folder/levels in path)? Where can I find more infos about this subject?

Making the same question in other words, if I have a script in C:/a/b/c/d/e/f/g/h/i/j/ folder and I want to do something in b folder I have to write the path. But what if this script will run in another computer where path /a/b/c/d/e/f/g/h/i/j/ is placed in another hard disk and not in C?

Thanks in advance.


See More: Check if multiply files exist batch

Report •


#1
August 25, 2010 at 07:14:29
Really? No one's going to pick this up?

I'm not entirely sure what you're doing, but I'm pretty sure you could condense it into one line:

for %%a in (file1 file2 file3) do if not exist ..\%%a copy %%a ..

But what if this script will run in another computer where path /a/b/c/d/e/f/g/h/i/j/ is placed in another hard disk and not in C?
Don't specify the drive. The drive is separate from the directory, and the current drive is assumed if it's not specified.

If you want the official word on paths, read the MSDN article. It's probably not the best source, but you could do much worse.


Report •

#2
August 25, 2010 at 08:39:59
Thanks for response, it has been for a while this question on air..

for %%a in (file1 file2 file3) do if not exist ..\%%a do copy %%a ..

Is "do" part of the command above? because it does not recognize it. If not what I have to enter there? Or it is not a batch script?

Sorry if I was not clear enough, I'll continue my script so you have a better idea:

cd..
IF EXIST file1 (GOTO con1) ELSE (GOTO 1)
:1
cd myfolder
copy file1 .\..
cd..
:con1

IF EXIST file2 (GOTO con2) ELSE (GOTO 2)
:2
cd myfolder
copy file1 .\..
cd..
:con2

IF EXIST file3 (GOTO con3) ELSE (GOTO 3)
:3
cd myfolder
copy file1 .\..
cd..
:con3

and so on...

Maybe sometimes some of the files are present and do not need to copy all of them, but only those that are missing.

Thank you very much.



Report •

#3
August 25, 2010 at 08:52:37
Second one isn't; bad editing on my part.

Suggested reading:

FOR /?
IF /?


Report •

Related Solutions

#4
August 25, 2010 at 09:34:07
Excellent! Exactly what I was looking for, thanks a lot!

Report •

Ask Question