Excel列举文件夹列表,调用cmd遍历

@echo off
for /f "delims=" %%i in ('dir /a-d /b /s /od') do echo %%~ti,%%~dpnxi >>test1.csv

///

Private Sub CommandButton1_Click()

Dim shtAllpath As Excel.Worksheet
Dim i
Dim strFileName As String
Dim strFilePath As String
Set shtAllpath = ThisWorkbook.Worksheets("bat")


For i = 2 To 5
    strFilePath = Cells(i, 1).Text
    strFileName = Cells(i, 3).Text
    Call Sample1(strFilePath, strFileName)
Next i
End Sub

Sub Sample3(strPath As String)
    Dim WSH, wExec, sCmd As String, Result As String, tmp, buf As String, i As Long
    Set WSH = CreateObject("WScript.Shell")
    sCmd = "dir " & strPath
    Set wExec = WSH.Exec("%ComSpec% /c " & sCmd)
    Do While wExec.Status = 0
        DoEvents
    Loop
    Result = wExec.StdOut.ReadAll
    tmp = Split(Result, vbCrLf)
    For i = 2 To UBound(tmp)        ''3行目以降を変数に格納します
        buf = buf & tmp(i) & vbCrLf
    Next i
    MsgBox buf
    Set wExec = Nothing
    Set WSH = Nothing
End Sub


Sub Sample2(strPath As String, strName As String)
    Dim WSH, wExec, sCmd As String, Result As String, tmp, i As Long
    Set WSH = CreateObject("WScript.Shell")
    sCmd = "dir " & strPath & " /s /tw"
    Set wExec = WSH.Exec("%ComSpec% /c " & sCmd & "> E:\" & strName & ".txt")
'    Call WSH.Run("cmd /c " & sCmd, 0, True)
    Do While wExec.Status = 0
        DoEvents
    Loop
    Result = wExec.StdOut.ReadAll
    tmp = Split(Result, vbCrLf)
    For i = 0 To UBound(tmp)
        Cells(i + 1, 5) = tmp(i)
    Next i
    Set wExec = Nothing
    Set WSH = Nothing
End Sub


Sub Sample1(strPath As String, strName As String)
  Set WSH = CreateObject("WScript.Shell")
  iFileNum = FreeFile
  sTempFileName = "G:\备份\myfile.bat"
  Open sTempFileName For Output As #iFileNum
  Print #iFileNum, "@echo off"
  Print #iFileNum, "for /f ""delims="" %%i in ('dir " & strPath & " /a-d /b /s /od') do echo %%~ti,%%~dpnxi >> G:\备份\" & strName & ".csv"
  Close #iFileNum
  
'Invoke Batch Program".
  Set wExec = WSH.Exec("%ComSpec% /c" & sTempFileName)
    Do While wExec.Status = 0
        DoEvents
    Loop
    Set wExec = Nothing
    Set WSH = Nothing


End Sub



你可能感兴趣的:(Excel列举文件夹列表,调用cmd遍历)