Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

What is wrong with my batch file

Posted on 2007-12-04
4
Medium Priority
?
258 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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
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 video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

877 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