Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

What is wrong with my batch file

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

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

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 …
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

722 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