批处理-将DIR的输出写入变量 [英] Batch - Write output of DIR to a variable

查看:614
本文介绍了批处理-将DIR的输出写入变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将DIR的输出存储在变量中.

I have to store the ouput of DIR in a variable.

这是在例如此处此处.

它们都或多或少提供了与我现在正在使用的答案类似的答案:

They all provide an answer more or less similar to what I'm using right now:

%= Find the *.sln file and write its path to the file temp.temp =%
DIR /s/b *.sln > temp.temp

%= Read out the content of temp.temp again to a variable =%
SET /p texte=< temp.temp

%= Remove the temp.temp file =%
DEL temp.temp

%= Get only the filename without path =%
FOR %%A IN ("%texte%") DO (
    SET Name=%%~nxA
)

但就我而言,我确信DIR /s/b *.sln的输出将始终是单线.对我来说,必须
a)将输出存储在外部文件中,然后
b)在其上运行FOR循环,尽管我已经知道它将只有一行.

But in my case I'm sure that the ouput of DIR /s/b *.sln will allways be a one-liner. To me it looks a bit ugly to have to
a) store the ouput in an external file and
b) run a FOR loop over it though I already know it will only have one line.

有没有直接/简单的方法可以做到这一点?

推荐答案

for /f "delims=" %%a in ('dir /s /b *.sln') do set "name=%%a"

实际上是最有效的方法(您可以直接处理命令的输出,不需要临时文件).
%name%将包含文件的完整合格文件名(如果有多个,则包含文件的最后一个)

indeed is the most efficient method (you can process the output of a command directly, no need for a temporary file).
%name% will contain the full qualified file name of your file (or the last of the files, if there are more than one)

这篇关于批处理-将DIR的输出写入变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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