我正在尝试在上下文菜单中运行某些命令提示符脚本。这比打开命令提示符并输入脚本名称并按 Enter 键要快得多。
所以我在
ib.cmd
中编写了一个名为C:\CustomExecutables\
的命令提示符脚本。
@echo on
setlocal enabledelayedexpansion
set "counter=0"
set "total=0"
set "error=false"
if "%~1"=="" (
set "target=%cd%"
) else (
set "target=%~1"
)
echo Target Directory: !target!
for %%F in ( "!target!\*.jpg" "!target!\*.png" "!target!\*.webp" ) do (
set /a "total+=1"
)
if %total% equ 0 (
echo No files to process
pause
exit /b
)
for %%F in ( "!target!\*.jpg" "!target!\*.png" "!target!\*.webp" ) do (
set /a "counter+=1"
title [!counter!/!total!] Processing %%~nxF ...
echo [!counter!/!total!] Processing %%~nxF ...
setlocal disabledelayedexpansion
"E:\PROGRAM\IrfanView\i_view64.exe" "%%~fF" /advancedbatch /convert="%%~dpFconverted\%%~nF.webp"
endlocal
if !errorlevel! neq 0 (
set "error=true"
echo An error occurred
pause
exit /b
)
)
if !error! equ false (
title Finished processing !counter! files
echo Finished processing !counter! files
exit
)
endlocal
如果给出第一个参数,脚本将处理给定目录中的图像文件。如果没有,它将处理当前工作目录中的文件。
我已经使用这个脚本一段时间了,从命令提示符调用它,所以我不认为这个脚本有任何错误。
接下来,我编写了
.reg
文件将该脚本注册到上下文菜单。
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Classes\Directory]
[HKEY_CURRENT_USER\Software\Classes\Directory\background]
[HKEY_CURRENT_USER\Software\Classes\Directory\Background\shell]
[HKEY_CURRENT_USER\Software\Classes\Directory\Background\shell\QuickTask]
"MUIVerb"="Quick Task"
"Icon"="C:\\Windows\\System32\\shell32.dll,137"
"ExtendedSubCommandsKey"="Directory\\\\ContextMenus\\\\QuickTask"
[HKEY_CURRENT_USER\Software\Classes\Directory\ContextMenus]
[HKEY_CURRENT_USER\Software\Classes\Directory\ContextMenus\QuickTask]
[HKEY_CURRENT_USER\Software\Classes\Directory\ContextMenus\QuickTask\shell]
[HKEY_CURRENT_USER\Software\Classes\Directory\ContextMenus\QuickTask\shell\IrfanViewer Convert]
"MUIVerb"="IrfanViewer Batch Convert"
"Icon"="E:\\PROGRAM\\IrfanView\\i_view64.exe"
[HKEY_CURRENT_USER\Software\Classes\Directory\ContextMenus\QuickTask\shell\IrfanViewer Convert\command]
@="cmd.exe /K \"C:\\CustomExecutables\\ib.cmd\" \"%V\""
但是每当我单击“IrfanViewer 批量转换”时,命令提示符都会显示“文件名、目录名或卷标签语法不正确”并且不执行脚本的任何代码。
我什至尝试将
@="cmd.exe /K \"C:\\CustomExecutables\\ib.cmd\" \"%V\""
部分更改为@="cmd.exe /S /K \"C:\\CustomExecutables\\ib.cmd\" \"%V\""
,但这并没有改变任何东西。
我不明白为什么会发生这个错误。我该如何解决这个问题?
看来我必须在
!target!
中用引号括住 echo
。
我修改了
ib.cmd
这样
if "%~1"=="" (
set "target=%cd%"
) else (
set "target=%~1"
)
echo Target Directory: "!target!"
我找不到脚本问题的原因是我通常不会在目录中放置任何空间,除非我必须这样做。所以在我的正常使用中并没有造成问题。