Solved

What is wrong with my batch file

Posted on 2007-12-04
4
239 Views
Last Modified: 2010-04-16
I am working on a batch file to create an automated archived backup that I will schedule every 2 hours to run.  It works until it gets through to the end of the script.  Then I get the error 'A duplicate file name exists, or the file cannot be found.'  What is wrong with me?

@echo
copy file.txt backup
cd backup
if exist file4.txt ren file4.txt file5.txt
if exist file3.txt ren file3.txt file4.txt
if exist file2.txt ren file2.txt file3.txt
if exist file1.txt ren file1.txt file2.txt
if exist file.txt ren file.txt file1.txt
del file.txt
pause
0
Comment
Question by:bleujaegel
  • 2
4 Comments
 
LVL 33

Accepted Solution

by:
knightEknight earned 500 total points
ID: 20408186
You have this:

  if exist file.txt ren file.txt file1.txt
  del file.txt

First you rename file.txt, then you try to delete it.  I suggest this instead:




@echo
copy file.txt backup
cd backup
if exist file5.txt del file5.txt
if exist file4.txt ren file4.txt file5.txt
if exist file3.txt ren file3.txt file4.txt
if exist file2.txt ren file2.txt file3.txt
if exist file1.txt ren file1.txt file2.txt
if exist file.txt ren file.txt file1.txt
pause
0
 
LVL 8

Expert Comment

by:Jeff Perry
ID: 20408187
at the end of the batch you are still in the backup directory. Try changing the end of the batch to

if file.txt exist del file.txt
cd ..
pause
0
 
LVL 8

Expert Comment

by:Jeff Perry
ID: 20408225
Oh also make sure your file.txt file is not a file.txt.txt file. Remember that windows by default hides common extensions so file.txt.txt will look like file.txt in the directory.
0
 
LVL 2

Author Comment

by:bleujaegel
ID: 20409112
Ok.  I see.  It gets to the last iteration, then it renames file.txt to file1.txt.  Then the next command is to delete, but there is no file.txt left and fails.  Nice work!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

813 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now