Computer Hope

Software => Computer programming => Topic started by: jormeno on May 22, 2013, 01:51:17 PM

Title: Zip.vbs script
Post by: jormeno 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!
Title: Re: Zip.vbs script
Post by: Sidewinder 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)
Title: Re: Zip.vbs script
Post by: jormeno 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.
Title: Re: Zip.vbs script
Post by: Sidewinder 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)
Title: Re: Zip.vbs script
Post by: jormeno 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.
Title: Re: Zip.vbs script
Post by: Sidewinder 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)
Title: Re: Zip.vbs script
Post by: jormeno 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.
Title: Re: Zip.vbs script
Post by: Sidewinder 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)




Title: Re: Zip.vbs script
Post by: jormeno 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
Title: Re: Zip.vbs script
Post by: Sidewinder 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?
Title: Re: Zip.vbs script
Post by: jormeno 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
Title: Re: Zip.vbs script
Post by: jormeno 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'
Title: Re: Zip.vbs script
Post by: Salmon Trout 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?
Title: Re: Zip.vbs script
Post by: jormeno 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
Title: Re: Zip.vbs script
Post by: Sidewinder 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)
Title: Re: Zip.vbs script
Post by: jormeno 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
Title: Re: Zip.vbs script
Post by: oldun 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%"
Title: Re: Zip.vbs script
Post by: jormeno 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.
Title: Re: Zip.vbs script
Post by: Sidewinder 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)

Title: Re: Zip.vbs script
Post by: jormeno 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.
Title: Re: Zip.vbs script
Post by: Sidewinder 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?

Title: Re: Zip.vbs script
Post by: Salmon Trout 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

Title: Re: Zip.vbs script
Post by: jormeno 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.