2

Because I have multiple back-up copies of all files, I use the following batch file to rename files so that I don't have to go into each drive/folder to rename files manually. The batch file works fine for renaming files. However, when renaming folders, sometimes it works, the other time, it generates another folder with the corrected name. When this happens, it doesn't happen to every folders to be renamed, only some of the folders.

Is there something wrong with the batch file? How can it be corrected?

chcp 65001

if exist C:\rename-all-4.txt del c:\rename-all-4.txt

SETLOCAL EnableDelayedExpansion

(for /f "tokens=1,2 delims=;" %%A in ('"TYPE C:\RENAME-ALL.txt"') do ( 
    echo %%A | find /i "\" 
    if errorlevel 1 (
        RENAME "D:\!mypath!%%A" "%%B" 
        RENAME "E:\!mypath!%%A" "%%B" 
        RENAME "\\PC1\D\!mypath!%%A" "%%B" 
        RENAME "\\PC1\E\!mypath!%%A" "%%B" 
 )  ELSE (
        echo "found pattern"
        echo %%A
        set mypath=%%A
        echo mypath is !mypath!
 )
)
) >> C:\RENAME-ALL-4.txt 2>&1
endlocal

CD /D C:\

The following is a shortened input file. After running the batch file, the original folder (中国人民银行_files) still exists along with a new folder (中國人民銀行_files).

News\
中国人民银行_files;中國人民銀行_files
6
  • @ It If a line in the input file ends with a slash, that's a path. There can be multiple paths in the input file and multiple folders/files (to be renamed) under a path. The example above only has one path "News\".
    – joehua
    Commented Jan 8, 2021 at 7:03
  • Start by properly indenting: to make it readable. I would do it for you, but I don't know this scripting language well. Commented Jan 8, 2021 at 7:04
  • Consider using Larry Wall's rename command. It will do it for you. On Debian systems you can install it with apt install rename. Commented Jan 8, 2021 at 7:05
  • @ ctrl-alt-delor I have edited the file with proper indentation. My batch file would rename folders but not always and I cannot tell when it'll rename and when it'll create new folders.
    – joehua
    Commented Jan 8, 2021 at 7:14
  • The listing in your looped file always obeys the order, first a line containing the name of a folder\ , and the line afterwards containing file names: fileX;fileY?
    – Io-oI
    Commented Jan 8, 2021 at 11:23

2 Answers 2

1
@echo off

setlocal enabledelayedexpansion
cd /d "%~dp0" & >nul chcp 65001
2>nul del/q /f .\rename-all-4.txt 

>> .\rename-all-4.txt 2>&1 ( 
    for /f tokens^=1-2^delims^=^; %%a in ('
    type c:\rename-all.txt')do echo=%%~a|>nul findstr /e \ && (
         echo="found pattern" && echo=%%~a
         set "mypath=%%~a" && call echo=mypath is !mypath!
        ) || (
         ren "d:\!mypath!%%~a" "%%~b"
         ren "e:\!mypath!%%~a" "%%~b"
         ren "\\pc1\d\!mypath!%%~a" "%%~b"
         ren "\\pc1\e\!mypath!%%~a" "%%~b"
    )) 

endlocal && CD /D C:\

  • This is an incorrect syntax:

if errorlevel  1 ... 
if errorlevel ????  1 ...

    errorlevel   equal integer :: 
if !errorlevel!  equ   1 ...
if %errorlevel%  equ   1 ...

Also, try to replace errorlevel condition to operators && and ||




6
  • You have changed your answer. Iwonder why. The old one works fine for me.
    – joehua
    Commented Jan 31, 2021 at 23:07
  • The old only print in screen
    – Io-oI
    Commented Jan 31, 2021 at 23:41
  • @joehua I just remove echo
    – Io-oI
    Commented Jan 31, 2021 at 23:43
  • The old one has this do if "%%B"=="", which I though was brilliant.
    – joehua
    Commented Feb 1, 2021 at 10:24
  • @joehua Where is this: if "%%B"==""
    – Io-oI
    Commented Feb 1, 2021 at 15:11
1
SETLOCAL EnableDelayedExpansion

(for /f "tokens=1,2 delims=;" %%A in ('"TYPE C:\RENAME-ALL.txt"') do if "%%B"=="" (
  echo "found pattern"
  echo %%A
  set  "mypath=%%A"
  echo mypath is !mypath!
 ) ELSE (
 RENAME "D:\!mypath!%%A" "%%B" 
RENAME "E:\!mypath!%%A" "%%B" 
RENAME "\\USER-PC1\D\!mypath!%%A" "%%B" 
RENAME "\\USER-PC1\E\!mypath!%%A" "%%B" 
 )
) >> C:\RENAME-ALL-4.txt 2>&1
endlocal

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .