Creating a Batch Script that will not copy if file exists

Posted on 2004-04-23
Last Modified: 2008-02-01
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
Question by:paulhaley
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 20

Expert Comment

by:Dufo G. Belski
ID: 10899058
If not exist path:\filename
{do your stuff here}

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

Author Comment

ID: 10899115
needs to be silent and not prompt
LVL 20

Accepted Solution

Dufo G. Belski earned 43 total points
ID: 10899202
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.
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!


Assisted Solution

Here2Help earned 41 total points
ID: 10902440
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

Author Comment

ID: 10907111
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
LVL 20

Expert Comment

by:Dufo G. Belski
ID: 10929223
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}

Assisted Solution

mrdtn earned 41 total points
ID: 11019016
As far as the sleep,

ping -n 5 localhost > null

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



Expert Comment

ID: 11198373
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT ( The crux of this batch f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor ( Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question