问题
I got some help scripting a batch script to date stamp image files from foxidrive. However, i messed up on the directory and it date stamped everything in that folder. So right now i have 1000+ images (.jpg) files with this string on the end of each file name " - 09-07-2014 - 09-07-2014 - 09-07-2014 - 09-07-2014 - 21-07-2014 - 21-07-2014.jpg".
Is there a way to rename these by removing the string above from these file names on every .jpg file on a given folder? I am planning to write this on command prompt
Thanks.
回答1:
Launch this in the folder and then examine renfiles.bat
to see if the rename commands look right, before removing the .txt
and running it.
@echo off
for %%a in (*.jpg) do (
echo "%%a"|repl "^.(.*) - 09-07-2014 - 09-07-2014 - 09-07-2014 - 09-07-2014 - 21-07-2014 - 21-07-2014.*" "ren $& \q$1%%~xa\q" ax >> renfiles.bat.txt
)
This uses a helper batch file called repl.bat
(by dbenham) - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
Place repl.bat
in the same folder as the batch file or in a folder that is on the path.
回答2:
@echo off
set workdir=d:\test
cd %workdir%
for /f "delims=" %%i in ('dir /b /s *.jpg') do call :search "%%i"
goto :eof
:search
set filename=%~n1
set fileextn=%~x1
set newfilename=%filename: - 09-07-2014 - 09-07-2014 - 09-07-2014 - 09-07-2014 - 21-07-2014 - 21-07-2014=%
ren "%filename%%fileextn%" "%newfilename%%fileextn%" >nul 2>&1
:eof
Sample -
D:\>dir d:\test
Volume in drive D is New Volume
Volume Serial Number is B04C-AB59
Directory of d:\test
24/07/2014 19:16 <DIR> .
24/07/2014 19:16 <DIR> ..
24/07/2014 19:09 2 ABC Court Doc - 09-07-2014 - 09-07-2014 - 09-07-2014 - 09-07-2014 - 21-07-2014 - 21-07-2014.jpg
24/07/2014 19:09 2 YXZ Court Doc - 09-07-2014 - 09-07-2014 - 09-07-2014 - 09-07-2014 - 21-07-2014 - 21-07-2014.jpg
2 File(s) 4 bytes
2 Dir(s) 7,037,292,544 bytes free
D:\>draft.bat
D:\test>dir d:\test
Volume in drive D is New Volume
Volume Serial Number is B04C-AB59
Directory of d:\test
24/07/2014 19:16 <DIR> .
24/07/2014 19:16 <DIR> ..
24/07/2014 19:09 2 ABC Court Doc.jpg
24/07/2014 19:09 2 YXZ Court Doc.jpg
2 File(s) 4 bytes
2 Dir(s) 7,037,292,544 bytes free
Cheers, G
来源:https://stackoverflow.com/questions/24890079/command-prompt-scripting-to-rename-files