首页 > 解决方案 > 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\=\media\thumbs\%"

:: 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

标签: windowsbatch-filecommand-lineampersand

解决方案


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


推荐阅读