0
votes

I'm trying to write some VBScript to parse the output generated when calling unzip -l from the command line. Specifically, I want to know the date modified and name of each file from this output.

The output looks like this:

  Length      Date    Time    Name
---------  ---------- -----   ----
    62208  01/12/2015 08:35   some/file/path/A file name.txt
    61440  01/12/2015 09:07   some/file/path/A file name2.txt

How would I go about parsing this in VBScript so that I could store the file name and date/time within a variable? I can see s few problems arising. Firstly, I can't just parse by spaces because the file names and date modified have spaces. Secondly, if the file size is too large the entire line becomes shifted. This stops me from just pulling the info I need in by the character position.

1

1 Answers

1
votes

Use a regular expression that looks for data separated by one or more spaces:

>> s = "    62208  01/12/2015 08:35   some/file/path/A file name.txt "
>> WScript.Echo qq(s)
>> s = Trim(s)
>> WScript.Echo qq(s)
>> Set r = New RegExp
>> r.Pattern ="^(\d+)\s+(\S+)\s+(\S+)\s+(.+)$"
>> Set m = r.Execute(s)
>> WScript.Echo qq(m(0).Submatches(0)), qq(m(0).SubMatches(3))
>>
"    62208  01/12/2015 08:35   some/file/path/A file name.txt "
"62208  01/12/2015 08:35   some/file/path/A file name.txt"
"62208" "some/file/path/A file name.txt"

(qq() just double quotes a string; if you need the name of the file, apply the .GetFileName method of the FileSystemObject)

Spoon feed:

Option Explicit

Function qq(s) : qq = """" & s & """" : End Function

Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")

Dim s : s = "    62208  01/12/2015 08:35   some/file/path/A file name.txt "
s = Trim(s) ' no leading/trailing spaces
Dim r : Set r = New RegExp
r.Pattern = "^(\d+)\s+(\S+\s+\S+)\s+(.+)$"
Dim m : Set m = r.Execute(s)
WScript.Echo CLng(m(0).SubMatches(0))
WScript.Echo CDate(m(0).SubMatches(1)) ' german locale!
WScript.Echo qq(m(0).SubMatches(2))
WScript.Echo qq(oFS.GetFileName(m(0).SubMatches(2)))

output:

cscript 27987576.vbs
62208
12.01.2015 08:35:00
"some/file/path/A file name.txt"
"A file name.txt"