如何将 where.exe 的特定结果存储在多个结果的变量中
How to store a specific result of where.exe in a variable for multiple results
在批处理脚本中,我需要找到特定可执行文件的位置(路径)。我为此使用 where.exe,它就像一个魅力。
@ECHO OFF
:: save the result of the command 'where.exe epstopdf' into the variable progpath
for /f "delims=" %%a in ('where.exe epstopdf') do @set progpath="%%a"
:: echo result
echo %progpath%
现在我遇到了问题,我安装了 imagemagick 因为我想使用工具 convert。我的问题是,执行 where.exe convert
returns 两个结果
C:\Program Files\ImageMagick\ImageMagick-6.9.0-Q16\convert.exe
C:\Windows\System32\convert.exe
不幸的是,当我将脚本更改为
@ECHO OFF
for /f "delims=" %%a in ('where.exe convert') do @set progpath="%%a"
echo %progpath%
最后的结果C:\Windows\System32\convert.exe
存储在变量progpath
中。
如何让脚本将特定结果保存在变量中(这里是第一个)?是否可以在换行符或类似的地方停止 for 循环?
PS:我怀念在 Linux 系统上工作的日子
Goto
可能 "break" 你的 for 循环
@ECHO OFF
for /f "delims=" %%a in ('where.exe convert') do @set progpath="%%a"&goto break
:break
echo/%progpath%
"I need the n-th" 有问题。更好地过滤您的需求。
示例:您知道,您正在明确搜索 imageMagick
:
for /f "delims=" %%a in ('where.exe convert^|find "ImageMagick"') do @set progpath="%%a"
或者您知道,您不想获取 windows-内置命令,而是搜索任何其他命令:
for /f "delims=" %%a in ('where.exe convert^|find /v "System32"') do @set progpath="%%a"
请记住,您可以为 for
语句的操作执行多行操作。
for /f "delims=" %%a in ('where.exe convert') do (
if /i not "%%a"=="C:\Windows\System32\convert.exe" set progpath="%%a"
)
PS: I miss working on a Linux system
两个字:学习PowerShell。除非别无选择,否则不要在批处理上浪费时间。
在批处理脚本中,我需要找到特定可执行文件的位置(路径)。我为此使用 where.exe,它就像一个魅力。
@ECHO OFF
:: save the result of the command 'where.exe epstopdf' into the variable progpath
for /f "delims=" %%a in ('where.exe epstopdf') do @set progpath="%%a"
:: echo result
echo %progpath%
现在我遇到了问题,我安装了 imagemagick 因为我想使用工具 convert。我的问题是,执行 where.exe convert
returns 两个结果
C:\Program Files\ImageMagick\ImageMagick-6.9.0-Q16\convert.exe
C:\Windows\System32\convert.exe
不幸的是,当我将脚本更改为
@ECHO OFF
for /f "delims=" %%a in ('where.exe convert') do @set progpath="%%a"
echo %progpath%
最后的结果C:\Windows\System32\convert.exe
存储在变量progpath
中。
如何让脚本将特定结果保存在变量中(这里是第一个)?是否可以在换行符或类似的地方停止 for 循环?
PS:我怀念在 Linux 系统上工作的日子
Goto
可能 "break" 你的 for 循环
@ECHO OFF
for /f "delims=" %%a in ('where.exe convert') do @set progpath="%%a"&goto break
:break
echo/%progpath%
"I need the n-th" 有问题。更好地过滤您的需求。
示例:您知道,您正在明确搜索 imageMagick
:
for /f "delims=" %%a in ('where.exe convert^|find "ImageMagick"') do @set progpath="%%a"
或者您知道,您不想获取 windows-内置命令,而是搜索任何其他命令:
for /f "delims=" %%a in ('where.exe convert^|find /v "System32"') do @set progpath="%%a"
请记住,您可以为 for
语句的操作执行多行操作。
for /f "delims=" %%a in ('where.exe convert') do (
if /i not "%%a"=="C:\Windows\System32\convert.exe" set progpath="%%a"
)
PS: I miss working on a Linux system
两个字:学习PowerShell。除非别无选择,否则不要在批处理上浪费时间。