[英]How to insert the output of a command to variable in a batch file?
在Windows的批处理文件中,我希望某些变量具有dir /b
命令的输出。
如何实现呢?
批处理文件不能很好地处理此用例。 我确实找到了一个描述使用临时文件的技术的线程 。
@ECHO OFF
setlocal enabledelayedexpansion
set LF=^
rem ** The two empty lines are NECESSARY
set output=
FOR /F %%i in ('dir /b') do SET output=!output!!LF!%%i
ECHO !output!
在Windows上,预装了更好的工具。 它叫做vbscript(后来有Powershell)。 为什么不使用vbscript代替。
strFolder="c:\test"
Set objFS = CreateObject( "Scripting.FileSystemObject" )
Set objFolder = objFS.GetFolder(strFolder)
s=""
For Each strFile In objFolder.Files
s=s & strFile & vbCrLf
Next
WScript.Echo s
变量s
现在包含文件列表(相当于dir
)。 而且,如果要将每个文件名存储到数组中,也可以这样做。 (cmd.exe没有数组等)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.