Wednesday, June 20, 2012

Batch DOS - Renaming Folders


Let's say, I have following folders and files:
A1  ( Folder)
 |----> A1.txt A1.doc

B1 ( Folder)
 |----> B1.txt B1.doc

C1 ( Folder)
|----> C1.txt C1.doc
When I run batch file, "1" should remove from folder, not from file name though. So, after you run it. you will get like :
  A  ( Folder)
     |----> A1.txt A1.doc

    B ( Folder)
     |----> B1.txt B1.doc

    C ( Folder)
    |----> C1.txt C1.doc


Answer:
Use for /d to iterate over directories:

setlocal enabledelayedexpansion enableextensions
for /d %%f in (*) do (
  set N=%%f
  set N=!N:1=!
  ren "%%f" "!N!"
)


This removes any 1 from the folder name, though. If the number vary and are only one character long you can do

set N=!N:~0,-1!


in the appropriate place above instead.

No comments: