Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
1  
Essentially the inverse of this superuser.com/questions/65302/… –  Martheen Cahya Paulo Dec 24 '12 at 14:56

2 Answers 2

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

share|improve this answer
    
+1 for answering the actual question! –  Bram Vanroy Dec 28 '12 at 14:28
up vote 0 down vote accepted

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: http://superuser.com/a/65304/125301,

share|improve this answer
    
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
    
It's a great tool for files, but not for folders. –  CustomX Oct 5 at 11:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.