Friday, December 6, 2019

windows - How do I Lowercase File Names and their Extensions in a Directory Recursively



I'm aware there's already questions like this but they either:




  • Throw errors that the file can't be found

  • Don't work or they uppercase instead




I'm unable to post comments in the other posted answers due to low reputation.



I don't mind, either, if it's .bat ran in the target directory or ran from the command line directly. I can only do basic stuff in MS-DOS.



So far I can set the directory I want:



x:
cd x:/folder1/folder2/target_folder



The result I want is this:



some_folder_in_target_folder/IMAGE1.jpg
some_folder_in_target_folder/Second/image1.JPG
some_folder_in_target_folder/Second/IMAGE2.JPG
some_folder_in_target_folder/Second/image3.jpg



to appear as:



some_folder_in_target_folder/image1.jpg
some_folder_in_target_folder/Second/image1.jpg
some_folder_in_target_folder/Second/image2.jpg
some_folder_in_target_folder/Second/image3.jpg


I don't want to modify the folder names themselves.




These files are on a USB drive. I don't know if that's causing some of the errors I saw from the other code samples I've tried.


Answer



I disagree with Seth and Luru in this special case, there is a wonderful solution with a small flaw, this cmd line should remedy that (if the output looks right, remove the echo):



For /r X:\Path %A in (.) do @For /f "eol=: delims=" %F in ('dir /l/b/a-d "%A" 2^>NUL') do @Ren "%~fA\%F" "%F"


In a batch the % has to be doubled:



For /r X:\Path %%A in (.) do @for /f "eol=: delims=" %%F in (

'dir /l/b/a-d "%%A" 2^>NUL'
) do Ren "%%~fA\%%F" "%%F"


The opposite, converting to uppercase would look clumsy in batch.



A powershell solution:



Get-ChildItem -Path X:\path -File|
where-Object {$_.Name -cne $_.Name.ToLower()}|

Rename-Item -NewName {$_.Name.ToLower()} -confirm


The same with aliases as a one liner:



gci -Path X:\path -File|? {$_.Name -cne $_.Name.ToLower()}|Ren -new {$_.Name.ToLower()} -confirm

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...