0

I want to rename the bulk files and folders at single time,how I can do this with batch scripting

I have the code for only to change either file or folder,not both

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET old=newdocV9
SET new=newdocV10
for /f "tokens=*" %%f in ('dir /b *.*') do (
  SET newname=%%f
  SET newname=!newname:%old%=%new%!
  move "%%f" "!newname!"
)

I am looking for the code to rename the files and folders at a single time using batch script

DavidPostill
  • 162,382

2 Answers2

0

Dbenham is right, your existing posted code should rename both files and folders. The only questionable point lays here: move folder folder with the same Source and Target might result to a breakpoint with The process cannot access the file because it is being used by another process error message (see an example in output below). Simple if /I could solve it as follows:

@echo off
SETLOCAL enableextensions enabledelayedexpansion
SET "old=ACK"
SET "new=XYZ"
for /f "tokens=*" %%f in ('dir /b *%old%*.*') do (
  SET "newname=%%~f"
  SET "newname=!newname:%old%=%new%!"
  if /I "!newname!"=="%%~f" (
        echo ==== "%%~f"
    ) else (
        echo move "%%~f" "!newname!"
    )
)
ENDLOCAL
goto :eof

Output:

==>D:\bat\SuperUser\881861.bat
move "ACKnowledge" "XYZnowledge"
move "assocbackup.txt" "assocbXYZup.txt"
move "ftypebackup.txt" "ftypebXYZup.txt"
move "StackOverflow" "StXYZOverflow"

==>move "ftypebackup.txt" "ftypebackup.txt"
        1 file(s) moved.

==>move "StackOverflow" "StackOverflow"
The process cannot access the file because it is being used by another process.

==>
JosefZ
  • 13,855
-1

The problem here is that a SET command does not work when used inside ( ) sections.

To overcome this problem, you can create a 2nd batch file and place all commands in there, then refer to that batchfile from your FOR command.

Your 2nd batchfile would look something like this:

::dorename.cmd
SET old=newdocV9
SET new=newdocV10

SET newname=%1
SET newname=!newname:%old%=%new%!
move "%%f" "!newname!"

Your for command would look something like this:

for /f "tokens=*" %%f in ('dir /b *.*') do dorename %%f
LPChip
  • 66,193