Monday, February 18, 2013

Modify list of directories name from Command Prompt

I have one requirement to modify name for list of directories through command prompt. Operating system is Windows Server 2008 R2.
Example - I do have following directory structure
  • ROOT
    • 123#5
      • 11
      • 22
    • 123#6
      • 11
      • 22
      • 33
    • 123#7
    • 123#8
      • 44
I want to update above mentioned directory structure as following structure through batch file or command prompt.
  • ROOT
    • 123~5
      • 11
      • 22
    • 123~6
      • 11
      • 22
      • 33
    • 123~7
    • 123~8
      • 44 

      Batch File  

      @echo off
      
      setlocal EnableDelayedExpansion
      
      for /r "C:\root" %%d in (.) do (
        set dirname=%%~nxd
        echo !dirname! | findstr "#" >nul
        if !errorlevel! equ 0 (
          set newname=!dirname:#=~!
          ren "%%~dpnd" "!newname!"
        )
      )
      
      endlocal

No comments:

Post a Comment