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 44942 times)

0 Members and 1 Guest are viewing this topic.

jormeno

    Topic Starter


    Rookie

    Thanked: 1
    • Experience: Familiar
    • OS: Windows 7
    Re: Zip.vbs script
    « Reply #15 on: May 24, 2013, 01:32:24 PM »
    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)



    OK well Here is the error when I run from command prompt

    Input Error: There is no file extension in "C:\dir\dir with space but first part of the dir with space".  I also What i provided above is all I have I an new to scripting so still learning So not sure about arguments or place holders but i did give you all the code. Is there way to run the script on command with a dir that has a space? Like "Program files" cause when i run it in cmd it says Input Error: There is no file extension in "C:\dir\Program" and doesn't show the word files. Hope that makes sense
    You won't know unless you try.

    oldun

    • Guest
    Re: Zip.vbs script
    « Reply #16 on: May 24, 2013, 07:45:45 PM »
    When the command is executed from the command line, the paths which contain spaces must be enclosed in quotes, as stated by Sidewinder.
    i.e.
    Quote
    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"

    When using the batch file, (or manually setting the variable MY_PATH in cmd), you need to:
    EITHER change "SET MY_Path=C:\PATH\DIR\" to "SET MY_Path=C:\PATH\DIR" (NOTE: No trailing backslash)
    OR change every occurrence of "%MY_PATH%\DIR\..." to "%MY_PATH%DIR\..." (NOTE: No backslash after %MY_PATH%"

    jormeno

      Topic Starter


      Rookie

      Thanked: 1
      • Experience: Familiar
      • OS: Windows 7
      Re: Zip.vbs script
      « Reply #17 on: May 28, 2013, 06:18:37 AM »
      ok I fixed the set path portion of the script and it ran with no errors but the zip file is empty. Thanks again.
      You won't know unless you try.

      Sidewinder



        Guru

        Thanked: 139
      • Experience: Familiar
      • OS: Windows 10
      Re: Zip.vbs script
      « Reply #18 on: May 28, 2013, 01:02:40 PM »
      New and Improved Script v4.

      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: " & objArgs(0) & " Not Found ... Job Fail"
        WScript.Quit
      End If

      zipFolder = Mid(objArgs(1), 1, (InStrRev(objArgs(1), "\")) - 1)
      If Not fso.FolderExists(zipFolder) Then
      WScript.Echo "Output Folder: "  &  zipFolder & " 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

      Quote
      ok I fixed the set path portion of the script

      What does this mean? Do not use the batch file, run from the command prompt by typing:

      cscript "scriptname.vbs" "drive:\path\file.bak" "drive:\path\file.zip"

      Replace drive and path with the proper values. Use the quotes,

      If you do not get the results you're looking for, post back the script you used, the command line exactly as you typed it at the prompt, and any error messages you received.

       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 #19 on: May 28, 2013, 01:13:40 PM »
        New and Improved Script v4.

        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: " & objArgs(0) & " Not Found ... Job Fail"
          WScript.Quit
        End If

        zipFolder = Mid(objArgs(1), 1, (InStrRev(objArgs(1), "\")) - 1)
        If Not fso.FolderExists(zipFolder) Then
        WScript.Echo "Output Folder: "  &  zipFolder & " 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

        What does this mean? Do not use the batch file, run from the command prompt by typing:

        cscript "scriptname.vbs" "drive:\path\file.bak" "drive:\path\file.zip"

        Replace drive and path with the proper values. Use the quotes,

        If you do not get the results you're looking for, post back the script you used, the command line exactly as you typed it at the prompt, and any error messages you received.

         8)


        Ok so I ran the script from the command line and from the zip.vbs. No errors were shown. It did create a zip folder but the zip folder was empty there was no file in it.


        .vbs code is


        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: " & objArgs(0) & " Not Found ... Job Fail"
          WScript.Quit
        End If

        zipFolder = Mid(objArgs(1), 1, (InStrRev(objArgs(1), "\")) - 1)
        If Not fso.FolderExists(zipFolder) Then
           WScript.Echo "Output Folder: "  &  zipFolder & " 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


        Batch code is

        @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


        and where you asked "What does this mean? Do not use the batch file, run from the command prompt by typing:ok I fixed the set path portion of the script.

        I was referring to what oldun suggested which worked. Thanks again.
        You won't know unless you try.

        Sidewinder



          Guru

          Thanked: 139
        • Experience: Familiar
        • OS: Windows 10
        Re: Zip.vbs script
        « Reply #20 on: May 28, 2013, 01:23:57 PM »
        Quote
        I was referring to what oldun suggested which worked. Thanks again.

        OK. So if you have a method that works, is this thread closed?

        The true sign of intelligence is not knowledge but imagination.

        -- Albert Einstein

        Salmon Trout

        • Guest
        Re: Zip.vbs script
        « Reply #21 on: May 28, 2013, 01:33:40 PM »
        Here is a script I found on the web and adapted.

        Usage:
        cscript //nologo Scriptname.vbs "file to zip.ext" "zip file name.zip"
        cscript //nologo Scriptname.vbs "folder to zip" "zip file name.zip"


        (use quotes in either parameter if they have spaces)

        examples

        create a zip and add a file to it
        cscript //nologo MyZip.vbs "u:\Text-to-Phone\Bus Girona-Aeropuerto.txt" d:\testme.zip

        add another file to an existing zip
        cscript //nologo MyZip.vbs "u:\Text-to-Phone\2-Bus Girona-Aeropuerto.txt" d:\testme.zip

        create a zip and add a folder (and sub folder tree) to it
        cscript //nologo MyZip.vbs  "D:\Virtual Machines\ST62K\caches\GuestAppsCache" "s:\Folder with spaces\test.zip"

        Notes:
        No error checking; will fail silently if the zip already contains a top level object the same name (i.e. files with the same name must be in different folders)
        Tested on Windows 7 Professional 64-bit and XP Professional 32-bit
        Files are compressed

        Set oFSO = CreateObject("Scripting.FileSystemObject")
        ToZip    = oFSO.GetAbsolutePathName(WScript.Arguments.Item(0))
        ZipName  = oFSO.GetAbsolutePathName(WScript.Arguments.Item(1))

        d=WindowsZip(ToZip, ZipName)

        Function WindowsZip(sFile, sZipFile)
          Set oZipShell = CreateObject("WScript.Shell")
          Set oZipFSO   = CreateObject("Scripting.FileSystemObject")
          If Not oZipFSO.FileExists(sZipFile) Then
            NewZip(sZipFile)
          End If
          Set oZipApp = CreateObject("Shell.Application")
          sZipFileCount = oZipApp.NameSpace(sZipFile).items.Count
              aFileName = Split(sFile, "\")
              sFileName = (aFileName(Ubound(aFileName)))
                  sDupe = False
          For Each sFileNameInZip In oZipApp.NameSpace(sZipFile).items
            If LCase(sFileName) = LCase(sFileNameInZip) Then
                sDupe = True
                Exit For
            End If
          Next
          If Not sDupe Then
                wscript.echo "Adding " & sfile
                oZipApp.NameSpace(sZipFile).Copyhere sFile
                On Error Resume Next
                Do Until sZipFileCount < oZipApp.NameSpace(sZipFile).Items.Count
                    Wscript.Sleep(100)
                Loop
                On Error GoTo 0
          End If
        End Function

        Sub NewZip(sNewZip)
          Set oNewZipFSO  = CreateObject("Scripting.FileSystemObject")
          Set oNewZipFile = oNewZipFSO.CreateTextFile(sNewZip)
          oNewZipFile.Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0)
          oNewZipFile.Close
          Set oNewZipFSO = Nothing
          Wscript.Sleep(500)
        End Sub


        jormeno

          Topic Starter


          Rookie

          Thanked: 1
          • Experience: Familiar
          • OS: Windows 7
          Re: Zip.vbs script
          « Reply #22 on: May 29, 2013, 05:10:59 AM »
          Here is a script I found on the web and adapted.

          Usage:
          cscript //nologo Scriptname.vbs "file to zip.ext" "zip file name.zip"
          cscript //nologo Scriptname.vbs "folder to zip" "zip file name.zip"


          (use quotes in either parameter if they have spaces)

          examples

          create a zip and add a file to it
          cscript //nologo MyZip.vbs "u:\Text-to-Phone\Bus Girona-Aeropuerto.txt" d:\testme.zip

          add another file to an existing zip
          cscript //nologo MyZip.vbs "u:\Text-to-Phone\2-Bus Girona-Aeropuerto.txt" d:\testme.zip

          create a zip and add a folder (and sub folder tree) to it
          cscript //nologo MyZip.vbs  "D:\Virtual Machines\ST62K\caches\GuestAppsCache" "s:\Folder with spaces\test.zip"

          Notes:
          No error checking; will fail silently if the zip already contains a top level object the same name (i.e. files with the same name must be in different folders)
          Tested on Windows 7 Professional 64-bit and XP Professional 32-bit
          Files are compressed

          Set oFSO = CreateObject("Scripting.FileSystemObject")
          ToZip    = oFSO.GetAbsolutePathName(WScript.Arguments.Item(0))
          ZipName  = oFSO.GetAbsolutePathName(WScript.Arguments.Item(1))

          d=WindowsZip(ToZip, ZipName)

          Function WindowsZip(sFile, sZipFile)
            Set oZipShell = CreateObject("WScript.Shell")
            Set oZipFSO   = CreateObject("Scripting.FileSystemObject")
            If Not oZipFSO.FileExists(sZipFile) Then
              NewZip(sZipFile)
            End If
            Set oZipApp = CreateObject("Shell.Application")
            sZipFileCount = oZipApp.NameSpace(sZipFile).items.Count
                aFileName = Split(sFile, "\")
                sFileName = (aFileName(Ubound(aFileName)))
                    sDupe = False
            For Each sFileNameInZip In oZipApp.NameSpace(sZipFile).items
              If LCase(sFileName) = LCase(sFileNameInZip) Then
                  sDupe = True
                  Exit For
              End If
            Next
            If Not sDupe Then
                  wscript.echo "Adding " & sfile
                  oZipApp.NameSpace(sZipFile).Copyhere sFile
                  On Error Resume Next
                  Do Until sZipFileCount < oZipApp.NameSpace(sZipFile).Items.Count
                      Wscript.Sleep(100)
                  Loop
                  On Error GoTo 0
            End If
          End Function

          Sub NewZip(sNewZip)
            Set oNewZipFSO  = CreateObject("Scripting.FileSystemObject")
            Set oNewZipFile = oNewZipFSO.CreateTextFile(sNewZip)
            oNewZipFile.Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0)
            oNewZipFile.Close
            Set oNewZipFSO = Nothing
            Wscript.Sleep(500)
          End Sub



          This is what I was looking for thank you. Thank you to as well sidewinder and oldun I appreciate you guys taking the time to assist me.
          You won't know unless you try.