• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 341
  • Last Modified:

File increment in batch file

I'm looking into a batch file for a file backup.  It's pretty straight forward, but I know nothing about batch files beyond the basics.

Basically every night I will have a file created called pos.dat

What I'd like to do is increment all previous files by one before placing the dat file while keeping only 100 dat files

I.E.  
      Delete previous pos100.dat
      rename pos99.dat to pos100.dat
      rename pos98.dat to pos99.dat
      rename pos97.dat to pos98.dat
      .
      .
      .
      rename pos01.dat to pos02.dat
      rename previous days pos.dat pos01.dat
      create new pos.dat

I know I can do this with a 100+ line batch file, but I did't know if there was a better way to do this with variables.


Thanks for your help!

Ryan
0
RyanMielke
Asked:
RyanMielke
  • 2
1 Solution
 
Harisha M GCommented:
Hi, it would be better if you have pos1 instead of pos01

Assuming that you change it, here is a code which does it:

Put this in a file, and name it as something.bat

____________________
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

DEL pos100.bat
FOR /l %%i IN (99,-1,1) DO (
  SET /a j=%%i+1
  REN pos%%i.bat pos!j!.bat
)
____________________

You now need to create the new file manually

---
Harish
0
 
RyanMielkeAuthor Commented:
Harish,

The only reason I was looking at pos01 instead of pos1 was to keep it clean if we were looking at a directory listing, but that's fine.

Thanks for your help!

Ryan
0
 
Harisha M GCommented:
Glad I could help :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now