batch file to rename folders creates new folders
Solution 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 ||
-
About using
errorlevel
in.bat
vs.cmd
file.
-
Some further reading:
[√] If
[√] Find
[√] For Loop
[√] For /F Loop
[√] !ErrorLevel! %Errorlevel%
[√] Conditional Execution || && ...
[√] Understanding start, 2>nul, cmd, and other symbols in a batch file
Solution 2:
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