locked
VBScript to parse pattern in file. Find 1st, then Find 2nd, then start over. RRS feed

  • Question

  • Need to find path, then modified from this block of text. Finding the first Path. Then the next modified. Add those to array then find the next path and modified. In PowerShell I would use a Join, but I have to do this one in VBS.  Any help would be wonderful!

    Example Text:

    Path = c:\temp\EDIarchive\ARROW\ARROW.7z
    Type = 7z
    Method = LZMA
    Solid = +
    Blocks = 1
    Physical Size = 25061
    Headers Size = 1094

    ----------
    Path = ARROW_810_7613035_2015Jan29 - Copy.TXT
    Size = 3389
    Packed Size = 23967
    Modified = 2015-01-29 21:05:37
    Attributes = ....A
    CRC = 5C89B76B
    Encrypted = -
    Method = LZMA:1536k
    Block = 0

    Path = ARROW_810_7613035_2015Jan29.TXT
    Size = 3389
    Packed Size =
    Modified = 2015-01-29 21:05:37
    Attributes = ....A
    CRC = 5C89B76B
    Encrypted = -
    Method = LZMA:1536k
    Block = 0

    Path = ARROW_810_7625589_2015Jan30 - Copy.TXT
    Size = 3371
    Packed Size =
    Modified = 2015-01-30 16:16:17
    Attributes = ....A
    CRC = 040A5D3D
    Encrypted = -
    Method = LZMA:1536k
    Block = 0


    Andrew Beller III

    Thursday, September 3, 2015 8:59 PM

Answers

  • Goodness.  It was clear in my head..

    4.  Add "Path","Modified" to a list.  Dictionary Object or Array.
    5.  Loop over that list.
         If the datediff of "Modified" value is > than <some number> then 
              Remove file from the 7zip archive.

    Number 4 is where I am needing some help.  Finding each path and its modified date, then turning that into an array.

    Might you know a good way to do this?

    Thanks again.


    Andrew Beller III

    "Clear in your head" is one thing. Documenting your script so that becomes maintainable code is another.

    Here is some rough code that might be what you want. It generates a two-dimensional array aOutput with each array record consisting of the value of "Path" and the value of "Modified". It assumes that your data structure is exactly as per your example.

    Set oFSO = CreateObject("Scripting.Filesystemobject")
    Set oFile = oFSO.OpenTextFile("d:\Test.txt")
    aData = Split(oFile.ReadAll, vbCrLf)
    oFile.Close

    ReDim aOutput(UBound(aData)\10, 1)
    i = 0

    For Each sLine In aData
        If Left(sLine, 4) = "Path" Then aOutput(i, 0) = Split(sLine)(2)
        If Left(sLine, 8) = "Modified" Then
            aOutput(i, 1) = CDate(Split(sLine)(2))
            i = i + 1
        End If
    Next

    Thursday, September 3, 2015 10:36 PM
  • Found a way to do it.  Not the most elegant solution, but works quickly in this instance
    If purge = 0Then
            dlog lf, "Skipping purge as " & srcfolder & " specified 0"
        Else
            strCommand = zipList & Chr(34) & zipfile & chr(34)
            Set zout = objWS.Exec(strCommand)
            arrLines = Split(zout.StdOut.ReadAll,vbCrLf)
            For z = 0ToUBound(arrlines)
                cl = arrlines(z)
                IfLeft(cl,6) = "Path ="Then
                    strPath = cl
                EndIf
                IfLeft(cl,10) = "Modified ="Then
                    strModified = cl
                    thestuff = thestuff & strpath & "," & strModified & vbCrLf
                EndIf                    
            Next
            WScript.Echo thestuff
            'grab files older than purge and delete from zip file
        EndIf


      

    Thanks,


    Andrew Beller III

    Thursday, September 3, 2015 10:37 PM

All replies

  • Need to find path, then modified from this block of text.
    Finding the first Path.
    Then the next modified.
    Add those to array then find the next path and modified.
    I have to do this one in VBS.  Any help would be wonderful.

    Sorry, your description is as clear as mud. You also need to decide if you need help with your existing script (which is OK) or if you want someone to write the whole script for you (which is likely to fall on deaf ears).

    Thursday, September 3, 2015 9:13 PM
  • Hi.. This is actually the last part of a script I have created.  It uses 7zip to grab files from folders older than the specified days and zip them up.  This last part is to clear the .7z file of files older than another specified date.  I am grabbing the 7z.exe -l -slt <filename> output and trying to parse it.  The output I pasted above is the result of that 7z command.  I need to grab the path then modified date for each file from the 7zip output so I can check over them and delete as needed.

    Here is the full script, but its just the last part of the DoFolder function that I am needing help with.  It driving me crazy:-)  Hope this makes more sense.

    Const bDEBUG = True
    Const doLog = True

    Dim objfso
    Dim srcfolders
    Dim i
    Dim netarchive
    Dim args
    Dim srcdrive
    Dim ediarchive
    Dim strYear, strDay, strMonth, strDate
    Dim strTime, strHour, strMinute, strSecond, ltime
    Dim cdir
    Dim objFolder
    Dim str7zcontentfile
    Dim objWS
    Dim zipPurge

    Set objWS  = CreateObject ("WScript.Shell")
    Set objfso = CreateObject("Scripting.FileSystemObject")
    Set oDic = CreateObject("scripting.dictionary")
    Set args = WScript.Arguments

    'Drive Letter on EDI server where archive folder exists
    srcdrive = args.item(0)

    cdir = objfso.GetParentFolderName(WScript.ScriptFullName)

    'Source folder where archive folders are that need processed
    ediarchive = srcdrive & "EDIarchive\"

    'Rules definition file
    ediinputfile = cdir & "\ediarchivesource.txt"

    strDate  = Date()
    strYear  = DatePart ("yyyy", strDate)
    strDay   = DatePart ("d", strDate)
    strMonth = DatePart ("m", strDate)
    ldate = strMonth&strDay&strYear

    strTime = Time()
    strHour = DatePart("h",strTime)
    strMinute = DatePart("n",strTime)
    strSecond = DatePart("s",strTime)
    ltime = strHour&strMinute&strSecond

    lf = cdir & "\EDIZipArchive.log"

    srcfolders = Split(objfso.OpenTextFile(ediinputfile,1).ReadAll,vbCrLf)

    'Zip command
    zipProgram = Chr(34) & cdir & "\7z\7z.exe" & Chr(34) & " a -t7z "
    zipList = Chr(34) & cdir & "\7z\7z.exe" & Chr(34) & " l -so -slt "
    zipPurge = Chr(34) & cdir & "\7z\7z.exe" & Chr(34) & " d "

    dlog lf, "Script run on date " & strDate
    str7zcontentfile = cdir & "\edizipcontents.txt"

    For b = 0ToUBound(srcfolders)
        WScript.Echo srcfolders(b)
    Next

    For i = 0ToUBound(srcfolders)
        arrline = Split(srcfolders(i),",")
        'Folder name to process
        srcfolder = arrline(0)
        
        'Files older than this number will be added to zip file.  If zero then skip
        days = CInt(arrline(1))
        
        'Files in zip file older than this number will be deleted from zip file.  If zero then skip
        purge = CInt(arrline(2))

        'Zip file used for archive
        zipfile = ediarchive & srcfolder & "\" & srcfolder & ".7z"

        dlog lf, vbcrlf & "[Processing folder " & srcfolder & "]"
        
        If days > 0Then
            Set objFolder = objfso.GetFolder(ediarchive & srcFolder)
            Call DoDirectory(objFolder)
        Else
            dlog lf, vbCrLf & "[Skipped processing " & srcFolder & ",0]"
        EndIf
    Next

    Function dlog(lf,t)
        OnErrorResumeNext
        Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
        If doLogThen fso.OpenTextFile(lf,8,True).WriteLine t
        If bDEBUGThen WScript.Echo t
    EndFunction

    Sub DoDirectory (objFold)
        Dim objUserDir, objFile, iDiff, fmodified, arrKeys, key
        Dim strCheckString, strY, strM, strD, strCommand, iResult, fn
        Dim strArchFile
        IfIsObject (odic) Then
            oDic.RemoveAll
        EndIf
        If objfold.files.count < 1Then
            dlog lf, "No files in " & objfold
            ExitSub
        EndIf
        ForEach objFilein objFold.Files
            IfNot(LCase(Right (objFile.Name, 3)) = ".7z") Then
                fmodified = objFile.DateLastModified
                iDiff = DateDiff("d", fmodified, strDate)
                If iDiff > daysThen
                    'Add dictionary object for file
                    oDic.Add objFile.Name, objFile.Path
                    dlog lf, "Added " & objFile.Path & " (" & fmodified & ") to dictionary for archive"
                EndIf
            EndIf
        Next
        
        'Loop over the dictionary and create archive.txt file
        If odic.Count > 0Then
            arrKeys = oDic.Keys
            If objFSO.FileExists(str7zcontentfile) Then
                objFSO.GetFile(str7zcontentfile).Delete
                dlog lf, "Deleted old content file: " & contentfile
            Else
                dlog lf, "No existing content file found.  Nothing to delete: " & contentfile
            EndIf
            dlog lf, "Creating " & str7zcontentfile & " for 7zip to read from"
            ForEach keyIn arrKeys
                objFSO.OpenTextFile(str7zcontentfile,8,True).WriteLine oDic.Item(key)
            Next   
            
            strCommand = zipProgram & Chr(34) & zipfile & chr(34) & " -y @" & str7zcontentfile
            dlog lf, "Zip it: " & strCommand
            iResult = objWS.Run(strCommand, 0, True)
            If iResult <> 0Then
                dlog lf, "Result = " & iResult
            Else
                dlog lf, "Result = Succssfully zipped contents!"
            EndIf
                
            'cleanup
            ForEach keyIn arrKeys
                fn = oDic.Item(key)
                dlog lf, "Deleting " & fn
                objFSO.DeleteFile fn, True
            Next
        EndIf
        
        'Remove entries in .7z file older than purge
        Dim arrContents()
        Dim cnt : cnt = 0
        If purge = 0Then
            dlog lf, "Skipping purge as " & srcfolder & " specified 0"
        Else
            strCommand = zipList & Chr(34) & zipfile & chr(34)
            Set zout = objWS.Exec(strCommand)
            arrLines = Split(zout.StdOut.ReadAll,vbCrLf)
            For z = 0ToUBound(arrlines)
                'Parse 7zip results grabbing the Path and Modified date.
                'Check if its older than purge.  If so add it to list 
                'of files to delete.        
            Next
            
        EndIf
    EndSub



    Andrew Beller III

    Thursday, September 3, 2015 9:44 PM
  • Unzip all to temp folder.  Adjust files using normal methods.  Re-zip.

    Don't try to re-invent the wheel.  It will make you very old very fast.


    \_(ツ)_/

    Thursday, September 3, 2015 9:53 PM
  • yeah, I thought about that, however; these zip files have 1000's of EDI TXT files processed from SAP so that would take a very long time.  Just grabbing the output from the -l -slt command is fast which is why I am trying this method.

    Looping over the files unzipped, deleting, the re-zipping would be easier, but super slow with that many small files:-)

    Thanks so much!


    Andrew Beller III

    Thursday, September 3, 2015 9:57 PM
  • Let's examine what you're saying:
    I need to grab the path then modified date for each file from the 7zip output so I can check over them and delete as needed.

    Could this mean the following?

    1. I am dealing with blocks of text that look like so:
      Path = ARROW_810_7613035_2015Jan29.TXT
      Size = 3389
      Packed Size =
      Modified = 2015-01-29 21:05:37
      Attributes = ....A
      CRC = 5C89B76B
      Encrypted = -
      Method = LZMA:1536k
      Block = 0
    2. I need to extract the value of the "Path" item, e.g. ARROW_810_7613035_2015Jan29.TXT.
    3. I now need to extract the value of the "Modified" item, e.g. 2015-01-29 21:05:37.
    4. I must now . . .
      (you write "I can check over them and delete as needed." which to me is pretty meaningless)



    Thursday, September 3, 2015 10:01 PM
  • Goodness.  It was clear in my head..

    4.  Add "Path","Modified" to a list.  Dictionary Object or Array.
    5.  Loop over that list.
         If the datediff of "Modified" value is > than <some number> then 
              Remove file from the 7zip archive.

    Number 4 is where I am needing some help.  Finding each path and its modified date, then turning that into an array.

    Might you know a good way to do this?

    Thanks again.


    Andrew Beller III

    Thursday, September 3, 2015 10:10 PM
  • Goodness.  It was clear in my head..

    4.  Add "Path","Modified" to a list.  Dictionary Object or Array.
    5.  Loop over that list.
         If the datediff of "Modified" value is > than <some number> then 
              Remove file from the 7zip archive.

    Number 4 is where I am needing some help.  Finding each path and its modified date, then turning that into an array.

    Might you know a good way to do this?

    Thanks again.


    Andrew Beller III

    "Clear in your head" is one thing. Documenting your script so that becomes maintainable code is another.

    Here is some rough code that might be what you want. It generates a two-dimensional array aOutput with each array record consisting of the value of "Path" and the value of "Modified". It assumes that your data structure is exactly as per your example.

    Set oFSO = CreateObject("Scripting.Filesystemobject")
    Set oFile = oFSO.OpenTextFile("d:\Test.txt")
    aData = Split(oFile.ReadAll, vbCrLf)
    oFile.Close

    ReDim aOutput(UBound(aData)\10, 1)
    i = 0

    For Each sLine In aData
        If Left(sLine, 4) = "Path" Then aOutput(i, 0) = Split(sLine)(2)
        If Left(sLine, 8) = "Modified" Then
            aOutput(i, 1) = CDate(Split(sLine)(2))
            i = i + 1
        End If
    Next

    Thursday, September 3, 2015 10:36 PM
  • Found a way to do it.  Not the most elegant solution, but works quickly in this instance
    If purge = 0Then
            dlog lf, "Skipping purge as " & srcfolder & " specified 0"
        Else
            strCommand = zipList & Chr(34) & zipfile & chr(34)
            Set zout = objWS.Exec(strCommand)
            arrLines = Split(zout.StdOut.ReadAll,vbCrLf)
            For z = 0ToUBound(arrlines)
                cl = arrlines(z)
                IfLeft(cl,6) = "Path ="Then
                    strPath = cl
                EndIf
                IfLeft(cl,10) = "Modified ="Then
                    strModified = cl
                    thestuff = thestuff & strpath & "," & strModified & vbCrLf
                EndIf                    
            Next
            WScript.Echo thestuff
            'grab files older than purge and delete from zip file
        EndIf


      

    Thanks,


    Andrew Beller III

    Thursday, September 3, 2015 10:37 PM
  • Your code looks very nice.  Thank you so much!  Not sure, but it does seem like you are on the attack with many of your comments.  Hope you cheer up and have a wonderful weekend.

    Thanks so much for your help!

    --Drew


    Andrew Beller III

    Thursday, September 3, 2015 10:43 PM