?
Solved

File increment in batch file

Posted on 2006-04-20
3
Medium Priority
?
335 Views
Last Modified: 2010-04-13
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
Comment
Question by:RyanMielke
  • 2
3 Comments
 
LVL 37

Accepted Solution

by:
Harisha M G earned 400 total points
ID: 16502566
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
 

Author Comment

by:RyanMielke
ID: 16502665
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
 
LVL 37

Expert Comment

by:Harisha M G
ID: 16502699
Glad I could help :)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month15 days, 23 hours left to enroll

850 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