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

The above coding is working for renaming of only either files or folders.

I need the code to rename files and folders recursively

sat
  • 11

4 Answers4

1

The code you posted does rename both files and folders. However, it does not recursively rename files and folders within folders.

Given that your DIR mask has an extension, and since folder names do not typically have extensions, my guess is you simply want to rename files, and not folders. If this is the case, then all you need do is add the DIR /S option to recurse into the child folders, and the /A-D option to exclude folders. You must set newname to just the file name, disregarding the path, via %%nxF. And the MOVE command requires the destination path of %%dpF. This will give a solution that usually works, though there can be complications (more on that later).

@echo off
setlocal enableDelayedExpansion
set "old=V1"
set "new=newdocV2"
for /f "delims=" %%F in ('dir /b /s /a-d *.ods') do (
  set "newname=%%~nxF"
  set "newname=!newname:%old%=%new%!"
  move "%%F" "%%~dpF!newname!"
)

If you truly want to rename both files and folders in one pass, then obviously you must remove the
/A-D option. But less obvious is you must sort the result of the DIR command descending to make sure that children are processed before parents, otherwise a child rename will fail because the listed path will no longer be valid.

@echo off
setlocal enableDelayedExpansion
set "old=V1"
set "new=newdocV2"
for /f "delims=" %%F in ('dir /b /s *.ods ^| sort /r') do (
  set "newname=%%~nxF"
  set "newname=!newname:%old%=%new%!"
  move "%%F" "%%~dpF!newname!"
)

Now for a host of things that can go wrong, and how to fix them, as well as other improvements.

  1. A file or folder name may include the ! character (rare, but possible). Expansion of a FOR variable while delayed expansion is enabled will corrupt the value if it contains !. The solution is to toggle delayed expansion on and off within the loop.

  2. The new name for file A may match an already existing name for file B, and the MOVE command will overwrite B with A. The end result is only one file, when before you had two. This is probably a bad thing. The solution is to use the REN command instead of MOVE. In this case the rename will fail, and I think that is a good thing. The REN command requires the new name without the destination path.

  3. You probably want a listing of how files/folders were renamed. This is especially true if you get an error, so that you can see exactly which rename failed. The simple solution is to ECHO the command before executing it.

  4. There is no need to go through the motions of renaming if the new name matches the old name. This is accomplished by adding an IF condition.

    @echo off setlocal disableDelayedExpansion set "old=V1" set "new=newdocV2" for /f "delims=" %%F in ('dir /b /s *.ods ^| sort /r') do ( set "source=%%F" set "oldname=%%~nxF" setlocal enableDelayedExpansion set "newname=!oldname:%old%=%new%!" if /i "!newname!" neq "!oldname!" ( echo ren "!source!" "!newname!" ren "!source!" "!newname!" ) endlocal )

There are still potential problems. For example, batch does not have a mechanism to replace the = character within a variable value. This problem is quite difficult to solve with pure batch.

Life is much simpler if you simply use my JREN.BAT utility - a hybrid JScript/batch script that uses regular expressions to rename files or folders. JREN.BAT is pure script that runs natively on any Windows machine from XP onward. Full documentation is avaliable via JREN /?. You may want to use
JREN /?|MORE, but my console window is configured with a large buffer that allows me to scroll up to view previous lines, so I don't need MORE.

You need to understand regular expressions to use the tool. Not an issue in this case, but if your search or replace term contains regular expression meta characters, then they would need to be escaped to get the correct literal interpretation. But the power of regular expressions is incredible - it allows you to be very specific as to exactly what changes are made.

I use the /I option to make the search insensitive to upper/lower case.

JREN.BAT renames only files or folders, therefore it requires two commands to rename both.

jren "V1" "newdocV2" /i /s /fm "*.ods"
jren "V1" "newdocV2" /i /s /fm "*.ods" /d

Since JREN is a batch script, you must use CALL JREN if you put the above commands within another batch script.

dbenham
  • 11,794
0

here my code is working successfully for renaming the folders and files successfully.I want to rename the content also inside the files.

Here is the batch file script,which I am using to rename both folders and files,

@echo off
setlocal disableDelayedExpansion
set "old=V21"
set "new=V22"
for /f "delims=" %%F in ('dir /b /s *. *.ods ^| sort /r') do (
  set "source=%%F"
  set "oldname=%%~nxF"
  setlocal enableDelayedExpansion
  set "newname=!oldname:%old%=%new%!"
  if /i "!newname!" neq "!oldname!" (
    echo ren "!source!" "!newname!"
    ren "!source!" "!newname!"
  )
  endlocal
)  
suspectus
  • 5,008
0

With the below code,I am able to successfully changing the renaming for Folders and files from V31 to V32,but unable to change inside the xml files from V31 to V32 with the below code.

@echo off
setlocal disableDelayedExpansion
set "old=V31"
set "new=V32"
for /f "delims=" %%F in ('dir /b /s *. *.xslt ^| sort /r') do (
  set "source=%%F"
  set "oldname=%%~nxF"
  setlocal enableDelayedExpansion
  set "newname=!oldname:%old%=%new%!"
  if /i "!newname!" neq "!oldname!" (
    echo ren "!source!" "!newname!"
    ren "!source!" "!newname!"
  ) )

for /f "delims=" %%i in (%xsltfile%) do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:%old%=%new%!"
    echo(!line!
    endlocal
))>"%newfile%"
del %textfile%
rename %newfile%  %xsltfile%

  )
)

  endlocal
)  
Smittey
  • 103
0

with the below code,I am able to successfully changing the renaming for Folders and files from V31 to V32,but unable to change inside the xml files from V31 to V32 with the below code.

@echo off
setlocal disableDelayedExpansion
set "old=V31"
set "new=V32"
for /f "delims=" %%F in ('dir /b /s *. *.xslt ^| sort /r') do (
  set "source=%%F"
  set "oldname=%%~nxF"
  setlocal enableDelayedExpansion
  set "newname=!oldname:%old%=%new%!"
  if /i "!newname!" neq "!oldname!" (
    echo ren "!source!" "!newname!"
    ren "!source!" "!newname!"
  ) )

for /f "delims=" %%i in (%xsltfile%) do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:%old%=%new%!"
    echo(!line!
    endlocal
))>"%newfile%"
del %textfile%
rename %newfile%  %xsltfile%

  )
)

  endlocal
)