将 FIND 命令的输出保存到变量 [英] Save output from FIND command to variable

查看:24
本文介绍了将 FIND 命令的输出保存到变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,我正在尝试获取 FIND 命令的输出并将其保存到变量中.具体来说,我正在使用:

Like the title says, I'm trying to take the output from a FIND command and save it to a variable. Specifically, I'm using:

DIR /b /s "C:" | FIND "someexe.exe"

找到一个特定的 .exe 文件,这似乎工作正常,但我想保存 FIND 的结果以供以后在同一脚本中使用.

to locate a specific .exe file, which seems to work fine, but then I want to save the results of FIND to use later in the same script.

我尝试了各种不同的调整:

I've tried various different tweaks of:

for /f "usebackq" %%i in (`DIR /b /s "C:" | FIND "someexe.exe"`) do SET foobar=%%i

但是当我尝试运行脚本时,命令窗口会立即关闭(可能是由于某些错误,我尝试在下一行放置 PAUSE 命令无济于事).

but when I try to run the script the command window immediately closes (presumably due to some error, I tried putting a PAUSE command in the next line to no avail).

我认为这是我做错的一些愚蠢的小事,但如果有人能告诉我它是什么,我会很感激.只是为了进一步参考,我不在乎someexe.exe"的副本有多少,我只需要其中一个的路径.

I assume it's some stupid minor thing that I'm doing wrong but if someone could show me what it is I'd appreciate it. Just for further reference, I don't care how many copies of "someexe.exe" exist, I just need the path for one of them.

推荐答案

你应该得到这个错误:|这时候出乎意料..您的直接问题是未引用的特殊字符(如 | 必须在出现在 FOR/F (命令")中时使用 ^ 进行转义.

You should be getting this error: | was unexpected at this time.. Your immediate problem is unquoted special characters like | must be escaped using ^ when they appear in a FOR /F ('command').

for /f "usebackq" %%i in (`DIR /b /s "C:" ^| FIND "someexe.exe"`) do SET foobar=%%i

听起来您正在通过在桌面或 Windows 资源管理器中双击来运行批处理文件.那行得通,但是在批处理终止后窗口立即关闭.在您的情况下,由于语法错误,它在到达 PAUSE 之前终止.

It sounds like you are running your batch file by double clicking from either your desktop or Windows Explorer. That works, but then the window immediately closes after the batch terminates. In your case it terminates before reaching PAUSE because of the syntax error.

我总是从命令窗口运行我的批处理文件:从开始菜单中您想运行 cmd.exe.这将打开一个命令控制台.然后 CD 到您的批处理文件所在的目录,然后通过键入其名称运行该批处理文件(不需要扩展名).现在脚本终止后窗口保持打开状态.您可以使用 SET 检查您的变量,运行另一个脚本,等等.

I always run my batch files from a command window: From the Start menu you want to run cmd.exe. That will open up a command console. Then CD to the directory where your batch file resides and then run the batch file by typing its name (no extension needed). Now the window stays open after the script terminates. You can examine your variables using SET, run another script, whatever.

在您的情况下无需使用 FIND - DIR 可以直接找到文件.此外,您的文件路径可能包含空格,在这种情况下,它将被解析为标记.您需要设置 "DELIMS=" 或 "TOKENS=*" 以便获得完整的路径.

There is no need to use FIND in your case - DIR can find the file directly. Also, the path of your file may include spaces, in which case it will be parsed into tokens. You need to set "DELIMS=" or "TOKENS=*" so that you get the complete path.

我不明白为什么人们在执行命令时使用 USEBACKQ.只有当我尝试对文件使用 FOR/F 并且由于空格和/或特殊字符而需要将文件括在引号中时,我才发现它很有用.

I never understand why people use USEBACKQ when they are executing a command. I only find it useful if I am trying to use FOR /F with a file and I need to enclose the file in quotes because of spaces and/or special characters.

此外,由于目录不可访问,您可能会遇到错误.将 stderr 重定向到 nul 会清理输出.再次,> 必须转义.

Also, you may run across errors due to inaccessible directories. Redirecting stderr to nul cleans up the output. Here again, the > must be escaped.

for /f "delims=" %%F in ('dir /b /s "c:someexe.exe" 2^>nul') do set foobar=%%F

这篇关于将 FIND 命令的输出保存到变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆