JREN.BAT v2.8 - Rename files/folders using regular expressions
Moderator: DosItHelp
Re: JREN.BAT v2.8 - Rename files/folders using regular expressions
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.
Re: JREN.BAT v2.8 - Rename files/folders using regular expressions
We're getting quite a bit off-topic now. However, I tried to write a little function to accomplish a case-aware renaming.
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
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
Steffen
Re: JREN.BAT v2.8 - Rename files/folders using regular expressions
Thanks, Steffen. Your script works perfectly! It has no issues with % and ^ neither if not use the CALL command.
JREN.BAT v2.8 - use of wildcards in setting up CMD-line switches
In the switches of jRen, how to define use of wildcards like asterisk in filename definitions?