简体   繁体   English

批处理 - 将DIR的输出写入变量

[英]Batch - Write output of DIR to a variable

I have to store the ouput of DIR in a variable. 我必须将DIR的输出存储在一个变量中。

This was asked before eg here or here or here . 这是如之前询问这里这里这里

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
)

But in my case I'm sure that the ouput of DIR /s/b *.sln will allways be a one-liner . 但在我的情况下,我敢肯定的输出中DIR /s/b *.sln将是百达一衬垫 To me it looks a bit ugly to have to 对我来说,它看起来有点难看
a) store the ouput in an external file and a)将输出存储在外部文件中
b) run a FOR loop over it though I already know it will only have one line. b)在它上面运行FOR循环虽然我已经知道它只有一行。

Is there any direct/simpler way to do this? 有没有直接/简单的方法来做到这一点?

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

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) %name%将包含文件的完整限定文件名(如果有多个文件,则包含最后一个文件)

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

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