批处理-将DIR的输出写入变量 [英] Batch - Write output of DIR to a variable
问题描述
我必须将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屋!