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

查看:486
本文介绍了将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.

推荐答案

您应该收到以下错误:| was unexpected at this time..您的直接问题是,未引号的特殊字符(如|)出现在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.

我总是从命令窗口运行批处理文件:从开始菜单中选择run 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天全站免登陆