Reputation: 217
I've taken the script from here to find the highest numbered file - Choose Highest Numbered File - Batch File
@echo off
setlocal enabledelayedexpansion
set max=0
for %%x in (*-*.png) do (
set "FN=%%~nx"
set "FN=!FN:*-=!"
if !FN! GTR !max! set max=!FN!
)
The tricky part is I have many folders that contain numbered files:
folderA/fileA-01.png
folderA/fileA-02.png
folderA/fileA-03.png
folderA/fileA-04.png
folderB/fileB-01.png
folderB/fileB-02.png
folderB/fileB-03.png
folderB/fileB-04.png
folderB/fileB-05.png
I would like to rename each highest numbered file from each folder to fileA-max.png and fileB-max.png - i.e. instead of the highest number use "max".
I'm not frequently doing batch files and when I do it's usually something very basic and simplistic so if you could - please help me with this one.
Upvotes: 1
Views: 213
Reputation: 37589
@echo off &setlocal disableDelayedExpansion
for /d %%a in (*) do (
for /f "tokens=1*delims=-:" %%b in ('dir /b /a-d /o-n "%%~a"^|findstr /n $') do if %%b==1 (
for /f "delims=-" %%d in ("%%~nc") do echo(ren "%%~a\%%~c" "%%~d-max%%~xc"
)
)
Upvotes: 2