File increment in batch file

Posted on 2006-04-20
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

      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!

Question by:RyanMielke
    LVL 37

    Accepted Solution

    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


    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


    Author Comment


    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!

    LVL 37

    Expert Comment

    by:Harisha M G
    Glad I could help :)

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Internal Web server not accesable 6 527
    Active Directory Replication 10 1,121
    windows explorer 21 161
    How to install Windows 2000 network drivers 4 68
    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…
    Synchronize a new Active Directory domain with an existing Office 365 tenant
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
    This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now