Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Zip.vbs script  (Read 44938 times)

0 Members and 2 Guests are viewing this topic.

jormeno

    Topic Starter


    Rookie

    Thanked: 1
    • Experience: Familiar
    • OS: Windows 7
    Zip.vbs script
    « on: May 22, 2013, 01:51:17 PM »
    Hello,

    New to the forums here. I have been banging in my head learning on how to write .bat and .vbs scripts. I am have been working on the coding myself for the last week and fixed many things that were wrong which is awesome, but now I am stuck so here it goes.

    The scripts that I am trying to create is to zip a file without using 3rd party tools such as winzip or 7zip. Below is the .bat coding.

    @echo Compressing your Database now.
    SET MY_Path=C:\PATH\DIR\     ( FYI I set the path because the path had a space in it. )
    cscript "%MY_PATH%\DIR\DIR\zip.vbs" "%MY_PATH%\DIR\DIR\FILE.bak" "%MY_PATH%\DIR\DIR\FILE.zip"
    @pause


    Here is the coding for the .vbs file

    'Get command-line arguments.
    Set objArgs = WScript.Arguments
    InputFolder = objArgs(0)
    ZipFile = objArgs(1)

    'Create empty ZIP file.
    CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)

    Set objShell = CreateObject("Shell.Application")

    Set source = objShell.NameSpace(InputFolder).Items

    objShell.NameSpace(ZipFile).CopyHere(source)

    'Required!
    wScript.Sleep 2000


    Now when I click on the batch file the following error shows up.

    C:\DIR\DIR\DIR\DIR\zip.vbs(11, 1) Microsoft VBScript runtime error: Object required: 'objShell.NameSpace(...)'


    Thanks in advance for the assistance


    If there is a way to do this all in just one script either in .bat or .vbs that would be awesome!
    You won't know unless you try.

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: Zip.vbs script
    « Reply #1 on: May 22, 2013, 04:29:18 PM »
    Quote
    cscript "%MY_PATH%\DIR\DIR\zip.vbs" "%MY_PATH%\DIR\DIR\FILE.bak" "%MY_PATH%\DIR\DIR\FILE.zip"

    From what I can see, the first argument should be a folder, not a file and there does not appear to be an object reference to the zip file. This snippet creates the reference and you can fix the cscript argument in the batch file to point to a folder.

    Code: [Select]
    'Get command-line arguments.
    Set objArgs = WScript.Arguments
    InputFolder = objArgs(0)
    ZipFile = objArgs(1)

    'Create empty ZIP file.
    CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)

    Set objShell = CreateObject("Shell.Application")

    Set source = objShell.NameSpace(InputFolder).Items
    Set target = objShell.NameSpace(zipfile)
    target.CopyHere(source)

    'Required!
    wScript.Sleep 2000

     8)
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    jormeno

      Topic Starter


      Rookie

      Thanked: 1
      • Experience: Familiar
      • OS: Windows 7
      Re: Zip.vbs script
      « Reply #2 on: May 23, 2013, 05:11:24 AM »
      When I replaced the code it I am still getting same error and the same result is occurring as well an empty zip file is created. I apologize I didn't mention that earlier, I assumed it was due to the vbs error.
      You won't know unless you try.

      Sidewinder



        Guru

        Thanked: 139
      • Experience: Familiar
      • OS: Windows 10
      Re: Zip.vbs script
      « Reply #3 on: May 23, 2013, 05:13:09 AM »
      Please disregard the previous post and any confusion it may have caused. There was  software malfunction at my end.

      This little snippet should help.

      Code: [Select]
      Set WshShell = WScript.CreateObject("WScript.Shell")
      Set fso = CreateObject("Scripting.FileSystemObject")
      Set objShell = CreateObject("Shell.Application")
      Set objArgs = WScript.Arguments

      Set zip = fso.CreateTextFile(objArgs(1))
      zip.Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0)
      zip.Close
      WScript.Sleep 500

      Set target = objShell.NameSpace(objArgs(1))
      Set src = objShell.NameSpace(objArgs(0)).Items

      target.CopyHere src
      WScript.Sleep 10000

      The batch file argument also needs correcting:

      Quote
      cscript "%MY_PATH%\DIR\DIR\zip.vbs" "%MY_PATH%\DIR\DIR\FILE.bak" "%MY_PATH%\DIR\DIR\FILE.zip"

      "%MY_PATH%\DIR\DIR\FILE.bak" should be a folder not a file. FYI: You can zip individual files using a slightly tweaked technique.

       8)
      The true sign of intelligence is not knowledge but imagination.

      -- Albert Einstein

      jormeno

        Topic Starter


        Rookie

        Thanked: 1
        • Experience: Familiar
        • OS: Windows 7
        Re: Zip.vbs script
        « Reply #4 on: May 23, 2013, 09:53:29 AM »
        I would like for it to be for an individual file. The reason I put %MY_PATH%\DIR\DIR\FILE.bak is because the one directory has a space and the only work around that worked for me was by defining it in SET.
        You won't know unless you try.

        Sidewinder



          Guru

          Thanked: 139
        • Experience: Familiar
        • OS: Windows 10
        Re: Zip.vbs script
        « Reply #5 on: May 23, 2013, 11:32:36 AM »
        File Zip:
        Code: [Select]
        Set objShell = CreateObject("Shell.Application")
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set objArgs = WScript.Arguments

        Set f = fso.CreateTextFile(objArgs(1), True)
        f.Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0)
        f.Close

        Set objFile = objShell.NameSpace(objArgs(1))
        objFile.CopyHere objArgs(0)
        WScript.Sleep 1000

         8)
        The true sign of intelligence is not knowledge but imagination.

        -- Albert Einstein

        jormeno

          Topic Starter


          Rookie

          Thanked: 1
          • Experience: Familiar
          • OS: Windows 7
          Re: Zip.vbs script
          « Reply #6 on: May 23, 2013, 12:27:57 PM »
          When I updated the .vbs file I got the following error when I tried to run it.

          ---------------------------
          Windows Script Host
          ---------------------------
          Script:   C:\ProgramData\Maxwell Systems\ProContractorMX\Backup\zip.vbs
          Line:   5
          Char:   1
          Error:   Subscript out of range
          Code:   800A0009
          Source:    Microsoft VBScript runtime error

          ---------------------------
          OK   
          ---------------------------

          Also when I tried to run it with the .bat file I got zip.vbs(10, 1) Microsoft VBScript runtime error: Object required: 'objFile'

          Press any key to continue . . .



          Thanks by the way for your help.
          You won't know unless you try.

          Sidewinder



            Guru

            Thanked: 139
          • Experience: Familiar
          • OS: Windows 10
          Re: Zip.vbs script
          « Reply #7 on: May 23, 2013, 02:26:35 PM »
          The first error message looks like it came from an editor or IDE environment. I can recreate this by not passing exactly two parameters to the script execution.

          Try running the VBScript standalone from the command line:

          Code: [Select]
          cscript C:\ProgramData\Maxwell Systems\ProContractorMX\Backup\zip.vbs "%MY_PATH%\DIR\DIR\FILE.bak" "%MY_PATH%\DIR\DIR\FILE.zip"

          If successful, copy and paste the command line into your batch file and you are done. If not please post the VBScript and the batch file you are using.

          The script is not doing any error checking, so it's is important that the first file name (%MY_PATH%\DIR\DIR\FILE.bak) exists and that two file names are passed to the script (the script will build/overwrite the zip file on each execution, but you must pass a label)

           8)




          The true sign of intelligence is not knowledge but imagination.

          -- Albert Einstein

          jormeno

            Topic Starter


            Rookie

            Thanked: 1
            • Experience: Familiar
            • OS: Windows 7
            Re: Zip.vbs script
            « Reply #8 on: May 24, 2013, 05:50:12 AM »
            Here is batch File.

            @echo Compressing your Database now.
            SET MY_Path=C:\dir\dir with space\
            cscript "%MY_PATH%\dir\Backup\zip.vbs" "%MY_PATH%\dir\Backup\file.bak" "%MY_PATH%\dir\Backup\file.zip"
            @pause

            The reason I set the path is because one of the dir's have a space in it and that prevents me from running it in command line. If there is a way to fix that would be awesome.  I have tried single quotes and double quotes and trip quotes. And find the set command and that worked some what.


            Here is my .vbs that i modifed from your suggestion.

            Set objShell = CreateObject("Shell.Application")
            Set fso = CreateObject("Scripting.FileSystemObject")
            Set objArgs = WScript.Arguments

            Set f = fso.CreateTextFile(objArgs(1), True)
            f.Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0)
            f.Close

            Set objFile = objShell.NameSpace(objArgs(1))
            objFile.CopyHere objArgs(0)
            WScript.Sleep 1000
            You won't know unless you try.

            Sidewinder



              Guru

              Thanked: 139
            • Experience: Familiar
            • OS: Windows 10
            Re: Zip.vbs script
            « Reply #9 on: May 24, 2013, 07:23:10 AM »
            Grasping at straws here because I cannot get this to fail. Long file names, short file names, embedded spaces, this sucker seems bullet proof.

            Try running the VBScript standalone at the command line (do not use the batch file and spell everything out):

            Code: [Select]
            cscript "C:\dir\dir with space\dir\Backup\zip.vbs" "C:\dir\dir with space\dir\Backup\file.bak" "C:\dir\dir with space\dir\Backup\file.zip"

            Post any error messages.  Also check to see if the zip file is created and contains the BAK file. 8)

            Does this directory (C:\dir\dir with space\dir\Backup) really exist on you machine or are you just using it as a placeholder for posting purposes?
            The true sign of intelligence is not knowledge but imagination.

            -- Albert Einstein

            jormeno

              Topic Starter


              Rookie

              Thanked: 1
              • Experience: Familiar
              • OS: Windows 7
              Re: Zip.vbs script
              « Reply #10 on: May 24, 2013, 07:37:29 AM »
              Grasping at straws here because I cannot get this to fail. Long file names, short file names, embedded spaces, this sucker seems bullet proof.

              Try running the VBScript standalone at the command line (do not use the batch file and spell everything out):

              Code: [Select]
              cscript "C:\dir\dir with space\dir\Backup\zip.vbs" "C:\dir\dir with space\dir\Backup\file.bak" "C:\dir\dir with space\dir\Backup\file.zip"

              Post any error messages.  Also check to see if the zip file is created and contains the BAK file. 8)

              Does this directory (C:\dir\dir with space\dir\Backup) really exist on you machine or are you just using it as a placeholder for posting purposes?

              its a place holder
              You won't know unless you try.

              jormeno

                Topic Starter


                Rookie

                Thanked: 1
                • Experience: Familiar
                • OS: Windows 7
                Re: Zip.vbs script
                « Reply #11 on: May 24, 2013, 07:39:51 AM »
                Also I did run it manually and that VB error prints to screen zip.vbs(10, 1) Microsoft VBScript runtime error: Object required: 'objFile'
                You won't know unless you try.

                Salmon Trout

                • Guest
                Re: Zip.vbs script
                « Reply #12 on: May 24, 2013, 07:41:06 AM »

                The reason I set the path is because one of the dir's have a space in it and that prevents me from running it in command line.

                How does it do that?

                jormeno

                  Topic Starter


                  Rookie

                  Thanked: 1
                  • Experience: Familiar
                  • OS: Windows 7
                  Re: Zip.vbs script
                  « Reply #13 on: May 24, 2013, 07:47:15 AM »
                  How does it do that?


                  ok that was weird when I ran it again to answer your question It ran with no errors but no file was in the zip folder, but when I run the batch file i get the VB error i posted
                  You won't know unless you try.

                  Sidewinder



                    Guru

                    Thanked: 139
                  • Experience: Familiar
                  • OS: Windows 10
                  Re: Zip.vbs script
                  « Reply #14 on: May 24, 2013, 09:29:11 AM »
                  OK, this has become a quest.

                  I modified the script to do some error processing. Run the VBScript standalone from the command prompt. Do not use the batch file and do not double click the script from explorer.

                  Code: [Select]
                  Set objShell = CreateObject("Shell.Application")
                  Set fso = CreateObject("Scripting.FileSystemObject")
                  Set objArgs = WScript.Arguments

                  If objArgs.Count <> 2 Then
                    WScript.Echo "Two Arguments Required ... Job Fail"
                    WScript.Quit
                  End If

                  If Not fso.FileExists(objArgs(0)) Then
                    WScript.Echo "Input File Not Found ... Job Fail"
                    WScript.Quit
                  End If

                  If Not fso.FolderExists(Mid(objArgs(1), 1, (InStrRev(objArgs(1), "\")))) Then
                  WScript.Echo "Output Folder Not Found ... Job Fail"
                  WScript.Quit
                  End If


                  Set f = fso.CreateTextFile(objArgs(1), True)
                  f.Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0)
                  f.Close

                  Set objFile = objShell.NameSpace(objArgs(1))
                  objFile.CopyHere objArgs(0)
                  WScript.Sleep 1000

                  Please post the results. Also please post the arguments actually used, not the placeholders. 8)
                  The true sign of intelligence is not knowledge but imagination.

                  -- Albert Einstein