My aim: Use Windows batch script to ensure that all files within given directory and subdirectories have a file extension that is lowercase
I have managed to get this far (not very far I admit !)..
for /R c:\test %%G in (*.*) do (
echo %%G
)
this successfully prints out all files with an extension (inc full path) but how do I check the file extension and ensure it is lowercase (I do not want to make the filename lowercase by the way .. just the extension).
Thanks in advance !
Greg
There are 2 (possibly 3) techniques you need to accomplish your task.
1) You need to parse out the file name and file extension – that is trivial as described in the FOR HELP. (type
HELP FORfrom the command line)%%~nG= file name%%~xG= file extension2) You need to store each file extension in a variable and then use code like the following to convert it to lowercase
You will need to enable delayed expansion with
setlocal enableDelayedExpansionat the top of the script.The above works because the search portion of search and replace is case insensitive.
3) In the unlikely situation that a file extension might containn a
!, you need to toggle delayed expansion on and off within the loop. But I seriously doubt you will run into that situation.Here is a finished, functioning script that disregards point 3.
Bali C had an interesting idea in his answer, but it did not quite work. I managed to make it work by creating a temporary file with a name consisting only of the file extension in a temporary folder. But it is slower than the above solution.