Link to home
Start Free TrialLog in
Avatar of paulhaley
paulhaley

asked on

Creating a Batch Script that will not copy if file exists

Hi All

I need to write a script that will be a scheduled job, part of this needs to be if a file exists it will not mov the new file into the area concerned, if it does not exist it will copy it,  does a what if command exist in DOS,

the OS is is Windows 2000 Server and i am writing a batch file that will FTP a file down then transfer the file to another area for processing , this file name will always be the same, so if the previous one is still there the new one cannot go into the folder, the new file has to wait until this one has been processed

Thank you all in anticipation
Avatar of A Syscokid
A Syscokid

If not exist path:\filename
{do your stuff here}

will only process the commands after "if not exist" if the file does not exist.
Avatar of paulhaley

ASKER

needs to be silent and not prompt
ASKER CERTIFIED SOLUTION
Avatar of A Syscokid
A Syscokid

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi All

    Thanks for the input thus far, is there anyway I could insert a sleep command

so it would loop sleep whilst the file is there, once the previous file had been processed it would then copy

many thanks in anticipation
Hi.  Sorry for the delay.

What I would do is write a second batch file that contains the commands you need to process the first file, if it exists (call it, say, process.bat).  Then

If exist path\filename call process.bat
{then your ftp and copy commands here}
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,
I feel this will do the job:

::-----8<----filename.bat----------------
@echo off

:start
if not exist dir\file.ext goto start
REM #  Add your code here.

::------------------------------------------

NOTE: in my experience, not ALL versions of DOS can run "if NOT exist". If this is the case for you, then replace:
if not exist dir\file.ext goto start
with:
if exist dir\file (
  REM #  File exists, continue to run code.
) ELSE (
  REM #  File does not exist, loop back.
  goto start
)

This will create an infinite loop while the file does not exist, but once the file exists, and is recognised by the batch, it will run the code that you want.

:o)
[R.d]