[英]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.