有没有一种方法可以将WriteLine输出组织为文本文件中的列?

时间:2019-01-18 19:19:05

标签: vbscript

是否可以将文本文件中的WriteLine数据输出分成几列(例如:Date | Location | Size)?

我尚未在线上看到有关此的任何信息,由于写入的数据不是静态的,因此不确定是否可能。我需要一个完全不同的功能来让脚本处理文本文件的格式吗?

Option Explicit
Dim sDirectoryPath,Search_Days,r_nr,iDaysOld,CmdArg_Object,lastModDate
Dim oFSO,oFolder,oFileCollection,oFile,oTF,Inp, SubFolder,fullpath
Set CmdArg_Object = Wscript.Arguments

Select Case (CmdArg_Object.Count)
    Case 3
        sDirectoryPath = CmdArg_Object.item(0)
        Search_Days = CmdArg_Object.item(1)
        r_nr = CmdArg_Object.item(2)
    Case Else
        WScript.Echo "SearchFiles.vbs requires 3 parameters:" & _
            vbCrLf & "1) Folder Path" & _
            vbCrLf & "2) # Days to Search" & _
            vbCrLf & "3) Recursive option (r/nr)"
        WScript.Quit
End Select

Set oFSO = CreateObject("Scripting.FileSystemObject")
iDaysOld=Date+(-1*Search_Days)
Inp = InputBox("Please Enter Desired Location of Log File:")
If Inp= "" Then
    Set oTF = oFSO.CreateTextFile("C:\output.txt")
Else
    Set oTF = oFSO.CreateTextFile(oFSO.BuildPath(Inp, "output.txt"))
End If
Set oFolder = oFSO.GetFolder(sDirectoryPath)
Set oFileCollection = oFolder.Files

WScript.Echo Now & " - Beginning " & Search_Days & " day search of " & sDirectoryPath

If r_nr = "r" Then
    oTF.WriteLine ("Search Parameters-") & _
        vbCrLf & "DirectoryPath: " & sDirectoryPath & _
        vbCrLf & "Older than: " & Search_Days &" Days " & _
        vbCrLf & "Recursive/Non-Recursive: " & r_nr & _
        vbCrLf & "------------------ "
    TraverseFolders oFSO.GetFolder(sDirectoryPath)

    Function TraverseFolders (FolderName)
        For Each SubFolder In FolderName.SubFolders
            For Each oFile In SubFolder.Files
                lastModDate = oFile.DateLastModified
                If (lastModDate <= iDaysOld) Then
                    oTF.WriteLine (oFile.DateLastModified) & " " & oFile.Path
                End If
            Next
            TraverseFolders(Subfolder)
        Next
    End Function
Else
    oTF.WriteLine ("Search Parameters:") & _
        vbCrLf & "DirectoryPath: " & sDirectoryPath & _
        vbCrLf & "Older than: " & Search_Days &" Days " & _
        vbCrLf & "Recursive/Non-Recursive: " & r_nr & _
        vbCrLf & "------------------------- "
    For Each oFile In oFileCollection
        lastModDate = oFile.DateLastModified
        If (lastModDate <= iDaysOld) Then
            oTF.WriteLine (oFile.DateLastModified) & " " & oFile.Path
        End If
    Next
End If

If Inp = "" Then
    WScript.Echo "Now - Finished! Results Placed in: C:\output.txt"
Else
    WScript.Echo "Now - Finished! Results Placed in: " & Inp
End If

1 个答案:

答案 0 :(得分:0)

您可以使用定界符分隔的输出格式,例如像这样:

Delim = vbTab
oTF.WriteLine "DateLastModified" & Delim & "Size" & Delim & "Path"
...
For Each oFile in oFileCollection
    oTF.WriteLine oFile.DateLastModified & Delim & oFile.Size & Delim & oFile.Path
Next

使用标签和精心选择的字段顺序的优点是,编辑者将在(主要是)正确的列中显示内容( ),您可以在其他程序中将其作为CSV导入。

如果您要使用固定宽度格式,则需要自己填充数据,例如具有自定义的填充功能,例如

Function LPad(s, l)
    n = 0
    If l > Len(s) Then n = l - Len(s)
    LPad = String(n, " ") & s
End Function

使用StringBuilder对象也是一种选择,如this answer中所述,用于另一个问题。