Announcement

Collapse
No announcement yet.

Archiving files with winzip

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Archiving files with winzip

    I am looking for an automated solution for the following (currently manual) procedure:

    Once a month we archive files in a backup folder and zip it up to another folder.
    Sounds simple?
    Here's the catch:
    The files that are zipped up need to be done on the 2nd of each month (can be easily added to a scheduled task) and include files from the last day of the previous month, all the files from the last month, as well as the files from the first day of the current month.
    These files are then to be zipped up and moved to another folder that needs to be created for the previous month (Something like July 2010 or similar) Of course the month that is backed up changes every month.
    The files in the back up folder does not require to be deleted, as it will be populated again by a Suresync job.
    Is this possible?
    What would I use for the scripting?
    Robocopy?
    XCopy?

  • #2
    Re: Archiving files with winzip

    and include files from the last day of the previous month,
    all the files from the last month,
    would not the one be included in the other?
    "...if I turn out to be particularly clear, you've probably misunderstood what I've said” - Alan Greenspan

    Comment


    • #3
      Re: Archiving files with winzip

      Let me rephrase that:
      The files from the last day of two months ago, all the files from last month, and the files from the first day of the current month.
      In other words, on the 2nd of August I want to zip up the files from the 30th of June, the whole of July and the 1st of Aug.

      Comment


      • #4
        Re: Archiving files with winzip

        This is not exactly what you are asking for, but something I threw together in 5 minutes..

        Some quick notes:
        1) This will Backup a set folder (it does not traverse the folder so c:\TEMP\*.* will be backed up, but c:\TEMP\123\*.* will not)
        2) I use 7z (Open Source) to compress files. This creates and password protects the zip file. (make sure 7z is in your path 7z.exe and 7z.dll)
        3) It assumes you want to call it the previous month's name as the file name (IE: June_2010.zip if you run the script in July in 2010, you can easily change the name if you want.
        4) I know you asked for 2 days into the previous month, but this was faster to show before I leave for the weekend, hope it helps

        Code:
         
        ' -----  Change These to your settings -------
          ZipPassword = "1234567890"
          archive_folder = "C:\TEMP"
          filelist = "c:\test\filelist.lst"
          daystobackup = 32
          if month(date()) = 1 then 
            archive_filename = "C:\somepath\" & monthname(12) & "_" & year(date()-1) & ".zip"
          else
            archive_filename = "C:\somepath\" & monthname(month(date())-1) & "_" & year(date()) & ".zip"
          end if
         
         
        Sub compressfiles()
          Set oScriptShell = CreateObject("WScript.Shell")
          runcmd = "cmd /c 7z.exe a -p" & ZipPassword & " """ & archive_filename & """ @" & filelist & " -y"
          oScriptShell.Run runcmd,1,true
          set oscriptshell = nothing
        End Sub
         
        Sub Processfolder(folderpath)
          Set objFSO = CreateObject("Scripting.FileSystemObject")
          set newzipfile= objFSO.CreateTextFile(filelist,true)
          objStartFolder = folderpath
          Set objFolder = objFSO.GetFolder(objStartFolder)
          Set colFiles = objFolder.Files
          For Each objFile in colFiles
            filename = objfile.name
            filedate = objfile.DateLastModified
            if datediff("d",filedate,now()) < daystobackup then
              newzipfile.writeline filename
            end if
          Next
          newzipfile.close
          set colfiles  = nothing
          set objfolder = nothing
          set newzipfile= nothing
          compressfiles()
          objfso.deletefile filelist
          set objFSO    = nothing
        End Sub
         
        Processfolder(archive_folder)
        I don't check the email the site is tied to over the weekend, but if you have any questions I'll answer them first thing next week.

        Hobie
        Last edited by Hobie; 30th July 2010, 22:13.

        Comment


        • #5
          Re: Archiving files with winzip

          Thanks Hobie, I will have a look at the script on Monday.

          Comment


          • #6
            Re: Archiving files with winzip

            Hobie, what is the suffix for the script?

            I tried running it, but cannot debug it properly.
            Is there any way I can pause the script after it has run to see what is causing the error?

            Comment


            • #7
              Re: Archiving files with winzip

              The script is a vbs script.

              copy and paste it into notepad and save the file as anyname.vbs.

              Once it is saved you can run the script by double clikcing it.

              I recommend you do the following before you run the script:
              1) download 7z and uncompress to somewhere in your path (or in the same folder as the script)
              2) Edit any variables (Where it says change these settings)

              Hobie

              Comment


              • #8
                Re: Archiving files with winzip

                I've done all that, but it is still not working.
                How can I see what is causing the script to fail?

                Comment


                • #9
                  Re: Archiving files with winzip

                  I did not trap for any errors, so if a problem is occuring it should tell you on the screen.

                  You could rem the line "objfso.deletefile filelist" so you can open the file and look a the results. (Add ' to the front of the line)

                  Another option is to add alerts in various places.

                  Code:
                   
                  Sub compressfiles()
                    wscript.echo "About to compress the files" ' <- Alert to show you the step
                    Set oScriptShell = CreateObject("WScript.Shell")
                    runcmd = "cmd /c 7z.exe a -p" & ZipPassword & " """ & archive_filename & """ @" & filelist & " -y"
                    oScriptShell.Run runcmd,1,true
                    set oscriptshell = nothing
                  End Sub
                  Code:
                   
                    For Each objFile in colFiles
                      filename = objfile.name
                      filedate = objfile.DateLastModified
                      wscript.echo filename & " > " & filedate  '<- Will create an alert with EVERY filename and date modified 
                      if datediff("d",filedate,now()) < daystobackup then
                        wscript.echo filename & " : " & filedate & " : Days old " & datediff("d",filedate,now()) ' <- shows each file that will be compressed and their age from today in days
                        newzipfile.writeline filename
                      end if
                    Next
                  The syntax is simply : wscript.echo <string>

                  The alerts do not really check for errors, but it will slow down the script as it cannot proceed until you hit OK.

                  If you are getting an erorr post it here so I can try to help.

                  Hobie
                  Last edited by Hobie; 2nd August 2010, 19:23. Reason: typos

                  Comment


                  • #10
                    Re: Archiving files with winzip

                    Thanks for that, Hobie, it's been a great help. At least now I can see what the script is actually doing.

                    I have done a few mods, like using Winzip (our company uses Winzip):

                    Code:
                     
                    archive_folder = "C:\TEMP\Downloads\"
                      filelist = "c:\test\filelist.lst"
                      foldername = "FADS_OUB_"
                      newfolder = "C:\somepath"
                      daystobackup = 33
                      if month(date()) = 1 then 
                        archive_filename = foldername & monthname(12) & "_" & year(date()-1)
                      else
                        archive_filename = foldername & monthname(month(date())-1) & "_" & year(date())
                      end if
                      
                    Sub compressfiles()
                      wscript.echo "About to compress the files" ' <- Alert to show you the step
                      Set oScriptShell = CreateObject("WScript.Shell")
                      runcmd = "cmd /c P:\Winzip\Winzip32 -a " & newfolder & archive_filename & ".zip" & " " & archive_folder & " @" & filelist
                      oScriptShell.Run runcmd,1,true
                      set oscriptshell = nothing
                    End Sub
                     
                    Sub Processfolder(folderpath)
                      Set objFSO = CreateObject("Scripting.FileSystemObject")
                      set newzipfile= objFSO.CreateTextFile(filelist,true)
                      objStartFolder = folderpath
                      Set objFolder = objFSO.GetFolder(objStartFolder)
                      Set colFiles = objFolder.Files
                      For Each objFile in colFiles
                        filename = objfile.name
                        filedate = objfile.DateLastModified
                        wscript.echo filename & " > " & filedate  '<- Will create an alert with EVERY filename and date modified 
                        if datediff("d",filedate,now()) < daystobackup then
                          wscript.echo filename & " : " & filedate & " : Days old " & datediff("d",filedate,now()) ' <- shows each file that will be compressed and their age from today in days
                          newzipfile.writeline filename
                        end if
                      Next  
                      newzipfile.close
                      set colfiles  = nothing
                      set objfolder = nothing
                      set newzipfile= nothing
                      compressfiles()
                      'objfso.deletefile filelist
                      set objFSO    = nothing
                    End Sub
                     
                    Processfolder(archive_folder)
                    However, when I run the script it gets to the point where it starts Winzip, adds all the files and then displays the following output error:

                    Action: Add (and replace) files Include subfolders: yes Save full path: no
                    Warning: name not matched: @c:\test\filelist.lst
                    updating B010509.OUB
                    updating B010510.OUB
                    updating B010511.OUB
                    updating B010511001.OUB
                    updating M052600F002.OUB
                    updating M053300F.OUB
                    updating M093700F001.OUB
                    updating M094000F.OUB
                    updating M094000F001.OUB
                    updating M096600F004.OUB
                    updating M102000F.OUB
                    updating M102100F.OUB
                    updating M102200F008.OUB
                    updating M102700F.OUB
                    updating M104300F002.OUB
                    updating M104300F004.OUB
                    updating M104400F.OUB
                    updating M104400F003.OUB
                    updating M105000F003.OUB
                    updating M105600F001.OUB
                    updating M105900F001.OUB
                    updating M106400F.OUB
                    updating M107100F.OUB
                    updating M109200F004.OUB
                    updating M109300F001.OUB
                    updating M109500F.OUB
                    updating M109500F002.OUB
                    updating M109500F003.OUB
                    updating M109600F.OUB
                    updating M109800F.OUB
                    updating M110200F.OUB
                    updating M110700F004.OUB
                    updating M110900F005.OUB
                    updating M111100F001.OUB
                    updating M752300F001.OUB
                    updating M753000F.OUB
                    updating M753300F.OUB
                    updating M754500F.OUB
                    updating M756000F.OUB
                    updating M756600F.OUB
                    updating M758700F.OUB
                    updating M762300F.OUB
                    replacing old Zip file


                    The filelist does exist, and the zip file does not appear anywhere.
                    Surely I am doing something wrong?

                    Comment


                    • #11
                      Re: Archiving files with winzip

                      I have found the zip file in C:
                      Don't know why it ended up there, and it also has not done any filtering, but zipped everything in the origin folder.
                      Last edited by krokkie; 3rd August 2010, 04:10.

                      Comment


                      • #12
                        Re: Archiving files with winzip

                        Try changing this line:
                        newzipfile.writeline filename
                        to
                        newzipfile.writeline folderpath & filename

                        Code:
                         
                          For Each objFile in colFiles
                            filename = objfile.name
                            filedate = objfile.DateLastModified
                            wscript.echo filename & " > " & filedate  '<- Will create an alert with EVERY filename and date modified 
                            if datediff("d",filedate,now()) < daystobackup then
                              wscript.echo filename & " : " & filedate & " : Days old " & datediff("d",filedate,now()) ' <- shows each file that will be compressed and their age from today in days
                              newzipfile.writeline folderpath & filename
                            end if
                          Next
                        If this does not work I would recommend you add one more alert and try to run the command from the command prompt manually: ( Sub compressfiles() )

                        If there is a syntax error on the command line you should be able to see it, but if

                        Code:
                         
                        Sub compressfiles()
                          Set oScriptShell = CreateObject("WScript.Shell")
                          runcmd = "cmd /c P:\Winzip\Winzip32 -a " & newfolder & archive_filename & ".zip" & " " & archive_folder & " @" & filelist
                          wscript.echo runcmd
                          oScriptShell.Run runcmd,1,true
                          set oscriptshell = nothing
                        End Sub
                        Hobie

                        Comment


                        • #13
                          Re: Archiving files with winzip

                          Thanks for that Hobie, now it works great.
                          It is now zipping only the files I want and putting the zip file in the same folder.

                          For my next trick, I need to create a subfolder under C:\somepath for the month, in this case C:\somepath\July2010 or something similar.

                          As I will be using this script for other archives as well, but all the zip files end up in one location, I need it to check if the folder exists, and if not, create it.

                          Can you help?

                          Comment


                          • #14
                            Re: Archiving files with winzip

                            Quick way to create a folder. (First checks to find the folder, will create if needed)

                            Code:
                             
                            Sub Check_Folder(foldern)
                              Set objFSO = CreateObject("Scripting.FileSystemObject")
                              If not objFSO.FolderExists(foldern) Then
                                Set objFolder = objFSO.CreateFolder(foldern)
                              End If
                              set objFolder = nothing
                              set objFSO = nothing
                            End Sub
                            Usage: (I added to some of the code I have given you already to give an example)

                            Foldername = Month_Year (IE: August_2010 if the date is 8/4/2010)

                            Code:
                             
                              newfolder = "C:\somepath"
                              daystobackup = 33
                              if month(date()) = 1 then 
                                archive_filename = foldername & monthname(12) & "_" & year(date()-1)
                              else
                                archive_filename = foldername & monthname(month(date())-1) & "_" & year(date())
                              end if
                              foldername = newfolder & "\FADS_OUB_\" & monthname(month(date())) & "_" & year(date())
                              Check_Folder(foldername)
                            Code:
                            Sub compressfiles()
                              Set oScriptShell = CreateObject("WScript.Shell")
                              runcmd = "cmd /c P:\Winzip\Winzip32 -a " & foldername & "\" & archive_filename & ".zip" & " " & archive_folder & " @" & filelist
                              wscript.echo runcmd
                              oScriptShell.Run runcmd,1,true
                              set oscriptshell = nothing
                            End Sub

                            #NOTE# I just typed on the petri site, sorry for any typos.

                            Hobie
                            Last edited by Hobie; 4th August 2010, 14:30. Reason: fixed check_folder sub

                            Comment


                            • #15
                              Re: Archiving files with winzip

                              Thank you so much for that, I have it working the way it should be.
                              The last stage is to combine all the subfolders, for which I have added a variable, but cannot get it to work.

                              To explain a little better, in C:\Temp\Downloads I will have a few subfolders that are to be archived. Let's call them Folder1, Folder2, etc.

                              The script should zip each folder with a name (that's where the FADS_OUB comes in) and zip it to the destination folder on C:\Somepath\month_year)
                              So I will end up with a lot of zip files in c:\somepath\month_year.

                              Here is the script, I have tidied it up a bit. The variables will be the origin subfolder and the zip filename prefix:

                              Code:
                               
                               origin_folder = "C:\TEMP\Downloads\"
                                filelist = "c:\test\filelist.lst"
                                destination_folder = "C:\somepath\"
                                daystobackup = 33
                                new_folder = origin_folder & subfolder 
                                 Dim ArgObj, var1, var2 
                               Set ArgObj = WScript.Arguments 
                               'First parameter
                               var1 = ArgObj(0) 
                               'Second parameter
                               var2 = ArgObj(1) 
                               msgbox "sourcefolder=" & var1 & " Zip_Filename=" & var2
                               'Clear object out of memory
                               set ArgObj = Nothing
                                if month(date()) = 1 then 
                                  archive_filename = zip_filename & monthname(12) & "_" & year(date()-1) & ".zip"
                                else
                                  archive_filename = zip_filename & monthname(month(date())-1) & "_" & year(date()) & ".zip"
                                end if
                                subfolder = destination_folder & monthname(month(date())-1) & "_" & year(date())
                                Check_Folder(foldername)
                              Sub Check_Folder(foldern)
                                wscript.echo "About to create new destination sub" ' <- Alert to show you the step
                                Set objFSO = CreateObject("Scripting.FileSystemObject")
                                If not objFSO.FolderExists(foldern) Then
                                  Set objFolder = objFSO.CreateFolder(foldern)
                                End If
                                set objFolder = nothing
                                set objFSO = nothing
                              End Sub
                              Sub compressfiles()
                                wscript.echo "About to compress the files" ' <- Alert to show you the step
                                Set oScriptShell = CreateObject("WScript.Shell")
                                runcmd = "cmd /c P:\Winzip\Winzip32 -a " & subfolder & "\" & archive_filename & " @" & 
                              filelist
                                oScriptShell.Run runcmd,1,true
                                set oscriptshell = nothing
                              End Sub
                               
                              Sub Processfolder(folderpath)
                                Set objFSO = CreateObject("Scripting.FileSystemObject")
                                set newzipfile= objFSO.CreateTextFile(filelist,true)
                                objStartFolder = folderpath
                                Set objFolder = objFSO.GetFolder(objStartFolder)
                                Set colFiles = objFolder.Files
                                For Each objFile in colFiles
                                  filename = objfile.name
                                  filedate = objfile.DateLastModified
                                  wscript.echo filename & " > " & filedate  '<- Will create an alert with EVERY filename and 
                              date modified 
                                  if datediff("d",filedate,now()) < daystobackup then
                                    wscript.echo filename & " : " & filedate & " : Days old " & datediff("d",filedate,now()) 
                              ' <- shows each file that will be compressed       and their age from today in days
                                  newzipfile.writeline folderpath & filename
                                  end if
                                Next  
                                newzipfile.close
                                set colfiles  = nothing
                                set objfolder = nothing
                                set newzipfile= nothing
                              compressfiles()
                                'objfso.deletefile filelist
                                set objFSO    = nothing
                              End Sub
                              Processfolder(new_folder)

                              Comment

                              Working...
                              X