I am looking to rename all directories on a certain hard drive (not C:) by capitalising the first letter of every directory (so: \images -> \Images). Unfortunately I have not enough experience to write this from scratch.
- 1,295
- 14
- 34
- 56
-
1Essentially the inverse of this http://superuser.com/questions/65302/is-there-a-way-to-batch-rename-files-to-lowercase – Martheen Dec 24 '12 at 14:56
2 Answers
The following native batch script will rename all directories in drive X:, ignoring read only, hidden, and system directories and ignoring reparse points. It converts the names to lower case and then capitalizes the initial character of the directory name (English characters). It does not handle unicode in names.
@echo off
setlocal disableDelayedExpansion
set "drive=x"
set "tempFile=%temp%\initUpper%random%.txt"
dir /s /b /l /ad-l-h-s-r %drive%:\* >"%tempFile%"
for /f "usebackq eol=: delims=" %%F in ("%tempFile%") do (
set "old=%%F"
set "new=%%~nxF"
setlocal enableDelayedExpansion
for %%C in (
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
) do if /i "!new:~0,1!" equ "%%C" set "new=%%C!new:~1!"
ren "!old!" "!new!" 2>nul || echo unable to rename !old!
endlocal
)
del "%tempFile%"
The script uses a temp file for performance reasons. It could be modified to have FOR /F read the output of the DIR command directly. But the output can be quite large, and FOR /F becomes very slow if the command result set is very large.
There are many tweaks that can be applied to the DIR command. For example:
Remove the /ad option to rename files and directories: dir /s /b /l /a-l-h-s-r
Remove the /a-h-s options to include hidden and system folders: dir /s /b /l /ad-l-r
Remove the /l option to preserve case of all but initial character: dir /s /b /ad-l-h-s-r
- 11,194
- 6
- 30
- 47
As Martheen Cahya Paulo mentions in the comments, this question has already been answered, however indirectly.
You can use a great tool, Space Tornado Renamer, as posted here: https://superuser.com/a/65304/125301,
- 1,295
- 14
- 34
- 56
-
Yeah, any of the numerous free/paid mass renamers can do this easily. I'm sure it can be accomplished using a batch file as well, but if it's not absolutely necessary I would advise you to avoid them (especially when they get complicated and hard to maintain/update). – Karan Dec 24 '12 at 15:54
-