0

Is there any way to separate the WriteLine data output in a text file into columns (ex: Date | Location | Size)?

I've yet to see any information regarding this anywhere online, unsure if possible since the data being written isn't static. Would I need an entirely different function in order to have the script handle the formatting of the text file?

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
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
A.P.
  • 3
  • 3
  • Put spaces at the end of your labels to pad them. You know the lengths of your labels. When you don't know you pad the string in code to however many spaces. Using `x = x & space(40 - len(x))` – catcat Jan 18 '19 at 19:29
  • As a side note, you may want to avoid nested function definitions like that. – Ansgar Wiechers Jan 18 '19 at 19:44

1 Answers1

1

You could use a delimiter-separated output format, e.g. like this:

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

Using tabs and a carefully chosen order of fields has the advantage that editors will display the content in (mostly) proper columns and you can import it as CSV in other programs.

If you're aiming for a fixed-width format you need to pad the data yourself e.g. with custom padding functions, e.g.

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

Using a StringBuilder object would also be an option, as described in this answer to another question.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328