我必须经常操纵数十个(如果不是数百个)图像。当我得到图像时,它通常是1018u182012480d1j80.jpg类型文件名,所以我可以选择所有图像,然后按“F2”将它们全部重命名为图像(1).jpg | image(2).jpg | ...有人可以给我写一个批处理脚本来删除括号和文件名中的空格吗?
如果可能的话,如果我甚至不必打开它并编辑批处理文件以更新当前目录,IE,如果它只能使用%CD%或其他东西来获取当前文件夹,那就太棒了那太棒了。
我根本不了解批处理文件,但是我发现这将删除括号,但我必须添加sourcedir。
@ECHO OFF
SETLOCAL
SET "sourcedir=C:\.." #Can we make this just find the location?
FOR /f "delims=" %%a IN (
'dir /b /s /a-d "%sourcedir%\*" '
) DO (
SET "name=%%~na"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "newname=!name:)=!"
SET "newname=!newname:(=!"
IF "!name!" neq "!newname!" (
IF EXIST "%%~dpa!newname!%%~xa" (ECHO cannot RENAME %%a
) ELSE (REN "%%a" "!newname!%%~xa")
)
endlocal
)
答案 0 :(得分:2)
如果文件名/路径不包含任何 !
感叹号,则脚本应该有效。
提供文件夹路径作为参数(请参阅下面的示例调用)。这甚至可以通过文件浏览器的拖放功能(拖动文件夹,放弃批处理)来完成。阅读call /?
或Command Line arguments (Parameters)了解%~1
解释。
使用.jpg
行中的"%sourcedir%\*.jpg"
缩小为FOR /f
个文件。
@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
SET "sourcedir=%~1" supplied string
if NOT defined sourcedir SET "sourcedir=%CD%" current directory if nothing supplied
rem basic validity check
if NOT exist "%sourcedir%\*" (
echo wrong directory "%sourcedir%"
goto :next
)
FOR /f "delims=" %%a IN ('dir /b /s /a-d "%sourcedir%\*" ') DO (
SET "name=%%~na"
SETLOCAL ENABLEDELAYEDEXPANSION
rem remove ) right parenthesis
SET "newname=!name:)=!"
rem remove ( left parenthesis
SET "newname=!newname:(=!"
rem remove ↓ space(s)
SET "newname=!newname: =!"
IF "!name!" neq "!newname!" (
IF EXIST "%%~dpa!newname!%%~xa" (
ECHO cannot RENAME %%a
) ELSE (
rem operational RENAME command is merely ECHO-ed for debugging purposes
ECHO RENAME "%%a" "!newname!%%~xa"
)
)
ENDLOCAL
)
:next
rem pause to see output
pause
不带参数的示例调用 - 适用于当前目录:
d:\bat\SU> D:\bat\SO\43097467.bat
RENAME "d:\bat\SU\New Text Document.txt" "NewTextDocument.txt"
RENAME "d:\bat\SU\Files\ruzna pisma.png" "ruznapisma.png"
RENAME "d:\bat\SU\Files\volume control options.png" "volumecontroloptions.png"
RENAME "d:\bat\SU\Files\volume mixer.png" "volumemixer.png"
RENAME "d:\bat\SU\Files\1126981\ERKS 100004_thumb.jpg" "ERKS100004_thumb.jpg"
Press any key to continue . . .
带参数的示例调用 - 适用于提供的目录:
d:\bat\SU> D:\bat\SO\43097467.bat D:\test\43097467
RENAME "D:\test\43097467\image do - Copy (2).bmp" "imagedo-Copy2.bmp"
cannot RENAME D:\test\43097467\image do - Copy (3).bmp
RENAME "D:\test\43097467\image do - Copy (4).bmp" "imagedo-Copy4.bmp"
Press any key to continue . . .
示例调用,带空格的参数必须用一对双引号括起来:
d:\bat\SU> D:\bat\SO\43097467.bat "D:\bat\odds and ends\a b"
RENAME "D:\bat\odds and ends\a b\c d\my list_Utf8.txt" "mylist_Utf8.txt"
RENAME "D:\bat\odds and ends\a b\c d\e f\h i batch.bat" "hibatch.bat"
RENAME "D:\bat\odds and ends\a b\c d\e f\System locale.png" "Systemlocale.png"
Press any key to continue . . .
d:\bat\SU>