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.

DavidPostill
  • 162,382
Dilip
  • 31

2 Answers2

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

Raj Huff
  • 461
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).
DavidPostill
  • 162,382