Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8217435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:28:09+00:00 2026-06-07T12:28:09+00:00

I scoured the web and could not find the exact code I’m looking for…

  • 0

I scoured the web and could not find the exact code I’m looking for… I found things that are very similar but did not get them to work. Here is a rough idea what I want:

int i = 1;
FOR (i; i < 9999; i++)
IF EXIST filename.log THEN
REN filename%i%.log
ELSE IF EXIST filename%i%.log THEN
REN filename%(i+1)%.log

Basically I want to check if a filename exists and if so, rename it to filename0001 — from there on, each time the batch is run, if filename#### is found, it renames it to one after that. So of course after the first time this is run, when it finds that filename exists it will rename it to filename0002 and so on.

Thank you!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T12:28:11+00:00Added an answer on June 7, 2026 at 12:28 pm

    Edited in response to comment. Also added test to make sure debug.log exists. Don’t want to rename files unnecessarily

    This 1st solution always has the most recent log as debug.log, the next most recent as debug0001.log, the next as debug0002.log, etc. The oldest log will have the highest number.

    @echo off
    setlocal enableDelayedExpansion
    set "base=debug"
    if exist "%base%.log" for /f "eol=: delims=" %%F in (
      'dir /b /o-n "%base%*.log" ^| findstr /rix /c:"%base%.log" /c:"%base%[0-9][0-9][0-9][0-9].log"'
    ) do (
      set "name=%%~nF"
      set /a "n=10000!name:*%base%=! %% 10000 + 1"
      ren "%%F" "%base%!n!.log"
    )
    

    To make the oldest log have 0001 and the newest have the highest number, then a small change is needed. Only one rename is needed.

    @echo off
    setlocal enableDelayedExpansion
    set "base=debug"
    if exist "%base%.log" for /f "eol=: delims=" %%F in (
      'dir /b /o-n "%base%*.log" ^| findstr /rix /c:"%base%.log" /c:"%base%[0-9][0-9][0-9][0-9].log"'
    ) do (
      set "name=%%~nF"
      set /a "n=10000!name:*%base%=! %% 10000 + 1"
      ren "%base%.log" "%base%!n!.log"
      goto :break
    )
    :break
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've scoured the Web looking for examples on how to do this. I've found
I have scoured the help files for Visual Studio and could not locate any
I've scoured the web and found a lot about working with git-svn and branches,
I really have scoured Google for a similar question and found nothing, which confuses
I'm looking for a nice rules-based web app firewall (WAF) that I can use
Ive scoured everywhere and haven't found a situation similar to mine.. New to Xcode/Objective
I have scoured for 2 hours now across the internet, I have found courses
I have scoured the internet to find which CMS's run on CloudSites and the
I've scoured over the questions here, but cannot seem to find an answer, so
I'm a web developer looking to get my feet wet with coding up a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.