Solved

What is wrong with my batch file

Posted on 2007-12-04
4
237 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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