JREN.BAT v2.8 - Rename files/folders using regular expressions

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
medp7060
Posts: 7
Joined: 05 Oct 2020 20:41

Re: JREN.BAT v2.8 - Rename files/folders using regular expressions

#76 Post by medp7060 » 09 Oct 2020 18:59

Agreed, Steffen. I started to seek a utility for renaming files on my exFAT drive D: when Windows 10 refused to make the case sensitive renaming. The fact that JREN.BAT worked on one occasion but failed on another indicates that the problem is with the Windows itself. The case-sensitive feature of JREN.BAT always works with NTFS drives, but may not work with FAT32, exFAT drives according to my testing.

aGerman
Expert
Posts: 3973
Joined: 22 Jan 2010 18:01
Location: Germany

Re: JREN.BAT v2.8 - Rename files/folders using regular expressions

#77 Post by aGerman » 10 Oct 2020 15:41

We're getting quite a bit off-topic now. However, I tried to write a little function to accomplish a case-aware renaming.

Code: Select all

@echo off &setlocal
>"foo.TXT" type nul
pause
call :renamecaseaware "foo.TXT" "foo.txt"
echo errorlevel %errorlevel%
pause
goto :eof

:renamecaseaware oldname newname
setlocal DisableDelayedExpansion
:: if we got different names we will just invoke REN as usual and get out of here
if /i "%~nx1" neq "%~2" ren "%~f1" "%~2" && (endlocal &exit /b 0) || (endlocal &exit /b 1)
:: assemble a temporary file name
set "tmpname=%~n1_%random%.~tmp"
:: in the unlikely case where the temporary file already exists retry with another file name
if exist "%~dp1%tmpname%" endlocal &goto renamecaseaware
:: rename the file using the temporary file name, get off if this fails
ren "%~f1" "%tmpname%" || (endlocal &exit /b 1)
:: rename the temporary file using the new name, if this fails we will bail out by restoring the original name
ren "%~dp1%tmpname%" "%~2" || (ren "%~dp1%tmpname%" "%~nx1" &endlocal &exit /b 1)
:: file name successfully changed 
endlocal &exit /b 0
Of course there's still a risk that it fails because the CALL command will change names containing ^ and % characters. At least I tried to avoid leaving crap in such cases.

Steffen

medp7060
Posts: 7
Joined: 05 Oct 2020 20:41

Re: JREN.BAT v2.8 - Rename files/folders using regular expressions

#78 Post by medp7060 » 13 Oct 2020 03:33

Thanks, Steffen. Your script works perfectly! It has no issues with % and ^ neither if not use the CALL command.

Post Reply