关于VBA的TextStream StdOut相关程序的学习——源代码(刘永富博士的ExcelVBA编程开发)

发布时间 2023-05-29 14:58:37作者: 林而宁

Sub test3() '标准输出-查找相关目录下所有的GIF格式文件。
Dim TS1 As IWshRuntimeLibrary.TextStream
Dim TS2 As IWshRuntimeLibrary.TextStream
Set WShell = New IWshRuntimeLibrary.WshShell
Set WE = WShell.Exec("cmd.exe /k")
Set TS1 = WE.StdIn
TS1.WriteLine Text:="cd /d E:\Chess & Dir *.gif /a /b /s"
TS1.Close
WE.Terminate
Do While WE.Status = WshRunning
DoEvents
Loop
Set TS2 = WE.StdOut
Dim i As Integer
Do Until TS2.AtEndOfStream
i = i + 1
Range("a" & i).Value = TS2.ReadLine
Loop
TS2.Close
End Sub

程序中关于 Dir *.gif /a /b /s的理解,如下表分析可看出,

是找到目录下所有.gif的文件-属性显示-拓展名和文件名。