windows - 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
解决方案
忏悔时间......我的代码 echo %thumbpath% 在魔法线之前有一行。这是罪魁祸首,因为我没有双引号 %thumbpath%。
推荐阅读
- angular - 如何在 Angular 中使用 Laravel @can()
- python - 情节上的永久尖峰线?
- tensorflow - Tensorflow2 对象检测 API:PR-Curve
- java - 如何添加内容类型标头应用程序/json 以取消映射后端点?
- android - 尽管我安装了 SDK,但未显示 Android API 30
- python - 我如何使用用户 ID 将其与条件匹配,以便当用户向频道发送消息时,机器人可以为用户执行特定任务
- javascript - 组件,ReactJS - 渲染没有返回任何内容
- javascript - 火狐浏览器无法在 playwright 中启动
- angular - 即使在导入 ReactiveFormModule 、 groupForm 和 ControlForm 之后,此错误也会持续出现,该怎么办
- hive - GreenPlum SQL 到 HiveSQL 的查询语句迁移