user1868774
user1868774

Reputation: 113

Renaming of multiple files via batch script

I have this part of batch script which is doing following:

-Under every sub-folder it renames files: 'jpg,gif,png' to 'Poster 1', 'Poster 2', 'Poster 3' and so on... (no change on extension)

set i=0
for %%b in (*.jpg *.gif *.png) do (
  set /A i+=1
   ren "%%b" "Poster !i!%%~Xb"
  echo File "%%b" renamed to "Poster !i!%%~Xb"
)

rem PATCH to fix a bug in previous FOR command (PLEASE IGNORE: there isa bug in prev. FOR command, which renames one file twice, if there are just two image files in a folder) :

if !i! neq 0 if not exist "Poster 1.*" (
  ren "Poster !i!.*" "Poster 1.*"
  echo ...and renamed back to "Poster 1"
)

!!!!! What I need is following: !!!!!
-I need to add a requirement if there is just a one file in the folder to rename it to "Poster", as without it this script renames it to 'Poster 1'

Thank you!

Upvotes: 0

Views: 877

Answers (1)

Arun
Arun

Reputation: 1763

Just a logical solution, After the do loop , you can check the value of 'i' again and if it is 1 then you can perform the rename operation once again[Rename Poster1 to Poster]. Hope this helps

Upvotes: 1

Related Questions