• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 714
  • Last Modified:

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
3 Solutions
Dufo G. BelskiRetired bureaucrat/desktop supportCommented:
If not exist path:\filename
{do your stuff here}

will only process the commands after "if not exist" if the file does not exist.
paulhaleyAuthor Commented:
needs to be silent and not prompt
Dufo G. BelskiRetired bureaucrat/desktop supportCommented:
At what point are you getting a prompt?

copy /y source destination

will suppress prompting to confirm you want to overwrite an existing file, but my understanding is that you don't want to copy if the file exists.
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

hey.  Try this, change directories and filename as needed.

IF EXIST C:\Dir\Filename.ext then goto exist

Copy /y /v C:\Dir\Filename.ext D:\OtherDirlocatation\Filename.ext

REM This label will contain your commands if the file exists
paulhaleyAuthor Commented:
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
Dufo G. BelskiRetired bureaucrat/desktop supportCommented:
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}
As far as the sleep,

ping -n 5 localhost > null

sleeps for 5 seconds.  Change the 5 to the desired value.


I feel this will do the job:

@echo off

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
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.


Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now