简体   繁体   English

如何将命令的返回值与管道一起用作批处理文件中的变量

[英]How to use a command's return value with pipe as a variable in batch file

I want to extract a file in in windows, right after search for it, like this: 我想在搜索后立即在Windows中提取文件,如下所示:

7z [args] | ls | grep filename

In my windows batch script, its: 在我的Windows批处理脚本中,其:

7z [args] | dir /B | findstr filename

the search alone works perfectly like in linux, but i just cant pass it forward to a variable, or straight to 7z as an input. 单独的搜索就像在linux中一样完美地工作,但是我只是不能将它向前传递给变量,或直接传递给7z作为输入。

I tried 我试过了

  • pipe from the left 从左边的管道
  • pipe from the right 右边的管道
  • for /f "delims=" %%a in ('dir /B ^| findstr onboard.zip') do @set Value=%%a

But all my solutions crashed with error. 但是我所有的解决方案都因错误而崩溃。

My idea to pass the found file to any variable, OR give it straight forward to the extracting tool. 我的想法是将找到的文件传递给任何变量,或者直接将其传递给提取工具。 Do you have any working solution/workaround for this? 您对此有任何可行的解决方案/解决方法吗? Thanks in advance! 提前致谢!

After 1.5 days i realized that DIR have built in search, that can solve my problem without the pipe: 1.5天后,我意识到DIR已内置搜索功能,可以在没有管道的情况下解决我的问题:

dir /b *filename

but still really interested in your - pipe included - solution :) 但仍然对您的-包括管道-解决方案真的很感兴趣

I am not quite sure of what you want. 我不太确定你想要什么。 Does 7z produce a list of filenames? 7z产生文件名列表吗?

for /f "usebackq tokens=*" %f in (`7z [args]`) do (if exist "%f" echo found file "%f")

If this is used inside a batch script, the percent characters must be doubled. 如果在批处理脚本中使用它,则百分比字符必须加倍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM