I am looking to rename all directories on a certain hard drive (not C:) by capitalising the first letter of every directory (so:
\Images). Unfortunately I have not enough experience to write this from scratch.
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:
/ad option to rename files and directories:
dir /s /b /l /a-l-h-s-r
/a-h-s options to include hidden and system folders:
dir /s /b /l /ad-l-r
/l option to preserve case of all but initial character:
dir /s /b /ad-l-h-s-r