Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

batch file to rename files also tries to rename directories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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" 
 )
) >> C:RENAME-ALL-4.txt 2>&1
endlocal

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...