投稿‎ > ‎

ファイル一覧作成

posted Aug 21, 2018, 10:34 PM by Zhang Wenxu
Public Sub ListAllFiles()
    Dim cmd As String
    Dim dir As String
    Dim lines As Variant
    Dim line As Variant
    Dim cel As Range
    Dim ret As String
   
    dir = Cells(1, 1).Value
    cmd = "dir /b /s " & dir
    ret = runCmd(cmd)
    lines = Split(ret, vbCrLf)
    Set cel = Cells(2, 2)
    For Each line In lines
        If InStr(line, ".svn") > 0 Then GoTo next_row
        cel.Value = line
        Set cel = cel.Offset(1, 0)
next_row:
    Next
End Sub

Public Function runCmd(strCmd As String) As String
    Dim fso As New FileSystemObject
    Dim tempFile As String
    Dim wsh As New WshShell
    Dim waitOnReturn As Boolean
    Dim windowStyle As Integer
    Dim oFile As Variant
   
    tempFile = fso.GetSpecialFolder(TemporaryFolder) & "\" & Replace(fso.GetTempName(), ".tmp", ".txt")
    strCmd = "cmd /c " & strCmd & " > " & tempFile & " 2>&1"
    waitOnReturn = True
    windowStyle = 0
    wsh.Run strCmd, windowStyle, waitOnReturn
    On Error Resume Next
    'runCmd = fso.OpenTextFile(tempFile, ForReading, False).ReadAll
    Set oFile = fso.GetFile(tempFile)
    'If IsNull(oFile) Then MsgBox ("File not found: " & strPath): Exit Function
    If IsNull(oFile) Then
        runCmd = "File not found: " & tempFile
        Exit Function
    End If
   
    With oFile.OpenAsTextStream()
        runCmd = .Read(oFile.Size)
        .Close
    End With
End Function
Comments