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 9098001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:15:30+00:00 2026-06-17T00:15:30+00:00

I’m trying to create one of my first ever DOS script (more used to

  • 0

I’m trying to create one of my first ever DOS script (more used to bash).

I’m trying to create a script that would rename my pics using a sequential number and the year as a prefix. I created this script :

@ECHO OFF
setlocal EnableDelayedExpansion

echo.
SET /P Year=Please enter Year (YYYY) : 
SET /P Start=Starting Number          : 

dir     

set /A i=%Start%
for %%f in (*.JPG) do call :renameit "%%f"
goto done

:renameit

echo %1 %i%
set str=%i%
set str=0000!str:.=!
set str=!str:~-4!  
ren %1 PICT_%Year%_%str%.JPG
set /A i+=1

:done

Issue is that it sometimes works fine, sometimes goes through one additional look renaming back a file that was already renamed. Here are 2 examples (as you can see, I added a DIR and ECHO in the script above for troubleshooting) :

RUN 1 (CORRECT) :

Directory of C:\TEST

07/01/2013  10:55    <DIR>          .
07/01/2013  10:55    <DIR>          ..
04/08/2011  14:28            16.823 PICT_2012_0005.JPG
04/08/2011  14:28            16.823 PICT_2012_0006.JPG
04/08/2011  14:28            16.823 PICT_2012_0007.JPG
07/01/2013  10:56               368 script.cmd
           4 File(s)         50.837 bytes
           2 Dir(s)  30.342.901.760 bytes free
"PICT_2012_0005.JPG" 5
"PICT_2012_0006.JPG" 6
"PICT_2012_0007.JPG" 7

RUN 2 (FAILED) :

Directory of C:\TEST

07/01/2013  10:56    <DIR>          .
07/01/2013  10:56    <DIR>          ..
04/08/2011  14:28            16.823 PICT_2011_0005.JPG
04/08/2011  14:28            16.823 PICT_2011_0006.JPG
04/08/2011  14:28            16.823 PICT_2011_0007.JPG
07/01/2013  10:56               368 script.cmd
           4 File(s)         50.837 bytes
           2 Dir(s)  30.342.901.760 bytes free
"PICT_2011_0005.JPG" 5
"PICT_2011_0006.JPG" 6
"PICT_2011_0007.JPG" 7
"PICT_2012_0005.JPG" 8

Any idea what I’m doing wrong ?

Thanks

  • 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-17T00:15:30+00:00Added an answer on June 17, 2026 at 12:15 am

    Expanded Comment into a full answer.

    It is a known feature/issue/undefined behavior of the for command. When looping through a directory of files and making modification to those files, the for loop does not keep a static list of files in the folder. Instead it just checks for the next file until nothing is returned from the file system. However, when modifying files in the loop, the file system sees the modification as an additional file and it gets sent again to the for loop. This will cause an unknown number (based upon the race condition of the for loop and file system) of files to be reprocessed.

    See this forum for an in depth discussion about it.

    http://www.dostips.com/forum/viewtopic.php?f=3&t=4138&sid=463cb91964f497c478afcca79c33384a

    You can alternatively use this for statement: for /F "delims=" %%F in ('dir /B *.jpg') do call :renameit "%%~fF"

    The dir command will generate a static list of the files in the directory at its run time preventing the recursion of files.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I have a small JavaScript validation script that validates inputs based on Regex. I
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small

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.