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
  • 156,873
  • I can see room for improvement, but your existing posted code should rename both files and folders. Please provide more description as to what is not working. – dbenham Feb 24 '15 at 12:06
  • FYI, you might be interested in this SU question How can I mass rename files?. – CharlieRB Feb 24 '15 at 12:55
  • @dbenham - I agree that it should work as written, though it ought to have a filter to bypass files that do not match the old pattern. We don't know the Windows version: maybe this matters. – AFH Feb 24 '15 at 15:00

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,217
-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
  • 61,264
  • No, the OP is already using delayed expansion properly. Normal expansion of %old% and %new% works fine within the loop as long as the variables are defined before the loop is entered - which they are. Even if you were to extract the DO code into a new routine, there is no reason to create a new batch script. The code could simply be put into a labeled subroutine within the same script and CALLed. – dbenham Feb 24 '15 at 12:11