Discussion forum for all Windows batch related topics.
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.
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.
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
Thanks, Steffen. Your script works perfectly! It has no issues with % and ^ neither if not use the CALL command.