The following batch file works to take directory and file names from a txt file and rename the files in the directory according to the names given.
SETLOCAL EnableDelayedExpansion
(for /f "tokens=1,2 delims=;" %%A in ('"TYPE C:RENAME-ALL.txt"') do (
echo %%A | find /i ""
if !errorlevel! equ 1 (
RENAME "D:!mypath!%%A" "%%B"
) ELSE (
echo "found pattern"
echo %%A
set mypath=%%A
echo mypath is !mypath!
)
)
) >> C:RENAME-ALL-4.txt 2>&1
endlocal
========== RENAME-ALL.TXT ================
mydirectoryphotos2019 <= this is the directory
IMG_20190729_064619.jpg;IMG_20190825_064619.jpg <=former name is replaced by the latter name
In this case, the batch file renames the file IMG_20190729_064619.jpg in the mydirectoryphotos2019 to IMG_20190825_064619.jpg.
However, the batch file also tries to rename mydirectoryphotos2019 to "" (This should not happen.) Can this be suppressed? How?
question from:
https://stackoverflow.com/questions/65915586/batch-file-to-rename-files-also-tries-to-rename-directories 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…