Windows 如何将命令的输出插入批处理文件中的变量?
在Windows上的批处理文件中,我希望某些变量具有Windows 如何将命令的输出插入批处理文件中的变量?,windows,batch-file,Windows,Batch File,在Windows上的批处理文件中,我希望某些变量具有dir/b命令的输出 如何实现这一点?批处理文件没有很好地处理这个用例。我确实找到了一个。在Windows上,预装了一个更好的功能。它被称为vbscript(后来出现了Powershell)。为什么不改用vbscript呢 strFolder="c:\test" Set objFS = CreateObject( "Scripting.FileSystemObject" ) Set objFolder = objFS.GetFolder(str
dir/b
命令的输出
如何实现这一点?批处理文件没有很好地处理这个用例。我确实找到了一个。在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没有数组等)只要给那些因为不喜欢VB语法而不喜欢这种方法的人一个提示(比如我自己)——同一个实用程序(wscript
/cscript
,Windows附带)也支持JScript(IE的Javascript引擎)。我最近使用JScript构建了一个完整的构建系统,它非常好,因为我喜欢Javascript。
@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!