Solved

What is wrong with my batch file

Posted on 2007-12-04
4
242 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
[X]
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
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

751 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