2

I have bunch of files with similar keyword like

computer-stock-photo.jpg 
computer-stock-picture.jpg
computer-picture.jpg

What I want to do is suffix serial wise 2 digit numbers like

computer-stock-photo-01.jpg, 
computer-stock-picture-02.jpg, 
computer-picture-03.jpg

Also, the batch script should work with any extension like jpg or png.

The script I tried:

@echo off
setlocal disableDelayedExpansion
set "chars=0123456789"
for /f "eol=: delims=" %%F in ('dir /b /a-d *.jpg') do call :renameFile "%%F"
exit /b

:renameFile
setlocal enableDelayedExpansion
:retry
set "name="
for /l %%N in (1 1 8) do (
  set /a I=!random!%%36
  for %%I in (!I!) do set "name=!name!!chars:~%%I,1!"
)
echo if exist !name!.jpg goto :retry
endlocal & ren %1 %name%.jpg

The above script only work for jpg and add random numbers not serial wise.

  • Please note that Super User is not a script writing service. If you tell us what you have tried so far (including any scripts you are using) and where you are stuck then we can try to help with specific problems. You should also read How do I ask a good question?. – DavidPostill Apr 2 '16 at 17:29
  • Hello, I have tried this script – Dilip Apr 2 '16 at 17:48
  • The above script only work for jpg and add random numbers not serial wise.. – Dilip Apr 2 '16 at 17:49
  • sorry, new to superuser. Please check now. thanks – Dilip Apr 2 '16 at 18:01
1

I believe you'll want to post this question in Stack Overflow if you want someone to help you write a script for your purposes. To follow the outline of this forum here, I will provide you with a link to a program called Bulk Rename Utility. It's been recommended in the past by other Stack Exchange users and seems to fit your criteria.

http://www.bulkrenameutility.co.uk/Main_Intro.php

  • 1
    Batch scripts are on topic for Super User as long as the OP makes some effort to solve the problem himself. – DavidPostill Apr 2 '16 at 18:58
1

File rename with suffix as 01 02 03 04 etc

Throw away your unsuitable script. You don't need random numbers and it doesn't handle .png files.

I've written a new script from scratch as was easier than trying to fix your broken script.

Use the following batch file:

@echo off
setlocal enabledelayedexpansion
rem initialise counter
set /a "x=1"
rem process jpg and png files
for /f "usebackq tokens=*" %%i in (`dir /b *.jpg *.png`) do (
  rem split into name and extension
  set _name=%%~ni
  set _ext=%%~xi
  rem pad the counter to 2 digits
  set "y=0!x!"
  set "y=!y:~-2!"
  rem do the rename
  ren "%%i" "!_name!-!y!!_ext!"
  increment counter
  set /a "x+=1"
  )
endlocal

Limitations:

  • Only processes .jpg and .png in the current working directory.
  • Only processes up to 99 files.

Further Reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • dir - Display a list of files and subfolders.
  • enabledelayedexpansion - Delayed Expansion will cause variables to be expanded at execution time rather than at parse time.
  • for /f - Loop command against the results of another command.
  • parameters - A command line argument (or parameter) is any value passed into a batch script.
  • set - Display, set, or remove CMD environment variables. Changes made with SET will remain only for the duration of the current CMD session.
  • variables - Extract part of a variable (substring).
  • Great answer and great fix but can we remove the 99 limitation ? You can add 3 numbers like 001 0001 to make renaming limitation to atleast 999 or 9999 – Dilip Apr 4 '16 at 3:15
  • 1
    You can remove the limitation yourself, once you understand how the batch file works. Hint: Change the two lines following rem pad the counter to 2 digits – DavidPostill Apr 4 '16 at 11:10
  • Do you need more help? If this answer was helpful to you and answered your question, please don't forget to accept that answer. – DavidPostill Apr 4 '16 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.