带有与号的 Windows 批处理文件和处理路径



我是编写Windows批处理文件的新手,正在努力让这段代码来处理与号。

批处理文件旨在使用 imagemagick 从一个文件夹树中的图像创建拇指到另一个文件夹树中。一切都很好,直到路径上有与号。对于大多数命令,双引号可以解决这个问题,但是当它命中"如果不存在"%thumbpath%"时,如果 %thumbpath 中有与号,它会消失。

我尝试使用简短的 8.3 版本的路径,虽然它不会倒下,但如果文件不存在,它也不会返回 true。

@echo off
setlocal enableextensions
cd /D "%~dp0"
call :processFiles
pause
goto :eof
:processFiles
for /R %%f in (*.jpg) do call :processFile "%%f", "%%~nxf", "%%~pf", "%%~df"

:processFile
:: parameters 1=full path, 2=filename, 3=path(no drive no file), 4=drive
setlocal ENABLEDELAYEDEXPANSION
set "source=%~1"
set fileName=%~2
set "folder=%~4%~3"
:: replace media path with thumbs folder
set "folder=%folder:media=mediathumbs%"
:: create the directory tree if required
if not exist "%folder%" md "%folder%"
set "thumbpath=%folder%%fileName%"
:: the following line causes an error when there is an ampersand in %thumbpath%
if not exist "%thumbpath%" (
    magick "%source%" -resize 250x250 -unsharp  0x6+0.5+0 "%thumbpath%"
)
exit /b

忏悔时间...我的代码中有一行回显 %thumbpath%,就在魔术线之前。这是罪魁祸首,因为我没有双引号 %thumbpath%。

最新更新