A little .vbs help please

I am trying to use this .vbs script in Realstudio, but I am getting a namespace(…) error at line 16

Dim fso, winShell, MyTarget, MySource, file
Set fso = CreateObject(“Scripting.FileSystemObject”)
Set winShell = createObject(“shell.application”)

MyTarget = “C:\Users\Presario User\Desktop\ My Folder.zip”
MySource = “C:\Users\Presario User\Desktop\ My Folder”

Wscript.Echo "Adding " & MySource & " to " & MyTarget

'create a new clean zip archive
Set file = fso.CreateTextFile(MyTarget, True)
file.write(“PK” & chr(5) & chr(6) & string(18,chr(0)))
file.close

winShell.NameSpace(MyTarget).CopyHere winShell.NameSpace(MySource).ItemsMySource — namespace(…) error here

do until winShell.namespace(MyTarget).items.count = winShell.namespace(MySource).items.count
wscript.sleep 1000
loop

Set winShell = Nothing
Set fso = Nothing

Can someone help me here?

Thanks.

Lennox

There isn’t such method/property : ItemsMySource

Thanks Rick,

I modified from
http://stackoverflow.com/questions/30211/can-windows-built-in-zip-compression-be-scripted

I replaced winShell.NameSpace(MyTarget).CopyHere winShell.NameSpace(MySource).ItemsMySource — namespace(…) error here

with

winShell.NameSpace(MyTarget).CopyHere winShell.NameSpace(MySource).Items

and I am still getting the Object Required: "nameSpace(…) Error

Any suggestions?

Thanks.

Lennox

Debug it.

Try breaking the big problem in smaller pieces and see what happens, like:

dim oft, ofs

set oft = winShell.NameSpace(MyTarget)
set ofs = winShell.NameSpace(MySource)
 
if oft is nothing or ofs is nothing then
            WScript.Echo "Fail !!!"
            exit
end if

oft.CopyHere(ofs.Items)
 
set oft = nothing
set ofs = nothing

Thanks Rick,

Used your suggestion and found
MyTarget = “C:\Users\Presario User\Desktop\ My Folder.zip”
MySource = “C:\Users\Presario User\Desktop\ My Folder”

should be
MyTarget = “C:\Users\Presario User\Desktop\My Folder.zip”
MySource = “C:\Users\Presario User\Desktop\My Folder”

but still does not work

Any other suggestions?

Thanks.

Lennox

Show current code, which line it stops and the error accused, please. Know that I am not on a Windows Machine to test, just trying to figure out the problem.

Hi Rick,

Thanks for your patience, time and assistance.

’ This is the error - Microsoft VBScript runtime error (31, 1) : Object doesn’t support this property or method

This is the current script…
Dim fso, winShell, MyTarget, MySource, file
Set fso = CreateObject(“Scripting.FileSystemObject”)
Set winShell = createObject(“shell.application”)

MyTarget = “C:\Users\Presario User\Desktop\My Folder.zip”
MySource = “C:\Users\Presario User\Desktop\My Folder”

Wscript.Echo "Adding " & MySource & " to " & MyTarget

'create a new clean zip archive
Set file = fso.CreateTextFile(MyTarget, True)
file.write(“PK” & chr(5) & chr(6) & string(18,chr(0)))
file.Close

dim oft, ofs
set oft = winShell.NameSpace(MyTarget)
set ofs = winShell.NameSpace(MySource)

if oft is nothing Then WScript.Echo “oft Fail !!!”
if ofs is nothing Then WScript.Echo “ofs Fail !!!”

winShell.NameSpace(MyTarget).CopyHere winShell.NameSpace(MySource)

do until oft.items.count = winShell .ofs.items.Count ’ This is the error - Microsoft VBScript runtime error (31, 1) : Object doesn’t support this property or method
wscript.sleep 1000
loop
file.Close

Set winShell = Nothing
set oft = nothing
set ofs = nothing
Set fso = Nothing

Thanks again.

Lennox

Hi Rick,

do until oft.items.count = winShell .ofs.items.Count
If that is used although the error shows up the zip file is still created in the debugger (Vbsedit)

If use this
do until oft.items.count = ofs.items.Count
then the debugger stops, the zip file is not created.

Lennox

Try this:

Dim fso, winShell, MyTarget, MySource, file
Set fso = CreateObject(“Scripting.FileSystemObject”)
Set winShell = createObject(“shell.application”)

MyTarget = “C:\Users\Presario User\Desktop\My Folder.zip”
MySource = “C:\Users\Presario User\Desktop\My Folder”

Wscript.Echo "Adding " & MySource & " to " & MyTarget

'create a new clean zip archive
Set file = fso.CreateTextFile(MyTarget, True)
file.write(“PK” & chr(5) & chr(6) & string(18,chr(0)))
file.Close
set file = nothing
set fso = nothing

dim oft, ofs
set oft = winShell.NameSpace(MyTarget)
set ofs = winShell.NameSpace(MySource)

if oft is nothing Then WScript.Echo “oft Fail !!!”
if ofs is nothing Then WScript.Echo “ofs Fail !!!”
oft.CopyHere ofs.Items

do until oft.items.Count >= ofs.items.Count
WScript.sleep 1000
loop

set oft = nothing
set ofs = nothing
Set winShell = Nothing

Hi Rick,

Nothing seems to be working… I have tried several combinations.

These are my findings and latest code:

Dim fso, winShell, MyTarget, MySource, file
Set fso = CreateObject(“Scripting.FileSystemObject”)
Set winShell = createObject(“shell.application”)

MyTarget = “C:\Users\Presario User\Desktop\My Folder.zip”
MySource = “C:\Users\Presario User\Desktop\My Folder”

'Wscript.Echo "Adding " & MySource & " to " & MyTarget

'create a new clean zip archive
Set file = fso.CreateTextFile(MyTarget, True)
file.write(“PK” & chr(5) & chr(6) & string(18,chr(0)))
file.Close
set file = nothing
set fso = nothing

dim oft, ofs
set oft = winShell.NameSpace(MyTarget)
set ofs = winShell.NameSpace(MySource)

if oft is nothing Then WScript.Echo “oft Fail !!!”
if ofs is nothing Then WScript.Echo “ofs Fail !!!”
oft.CopyHere ofs.Items
'winShell.NameSpace(MyTarget).CopyHere winShell.NameSpace(MySource)

'On Error Resume Next - No improvement
'On Error goto 0 ’ Disable error handling - No improvement
'These will create the complete zip - files And folders included but With the error
'do until oft.items.count >= winShell.ofs.items.Count ’ Microsoft VBScript runtime error: Object doesn’t support this property or method
'do until NameSpace(MyTarget).items.count >= winShell.NameSpace(MySource).items.Count 'Type mismatch: ‘NameSpace’
do until “C:\Users\Presario User\Desktop\My Folder.zip”.items.count >= “C:\Users\Presario User\Desktop\My Folder”.items.Count 'Object required: '[string: “C:\Users\Presario Us”]

'These three do the same thing - create the zip files with files only, folders are not included
'do until winShell.NameSpace(MyTarget).items.count >= winShell.NameSpace(MySource).items.Count
'do until oft.items.count >= winShell.NameSpace(MySource).items.Count
'do until oft.items.count >= ofs.items.Count

'None will run as an independent .vbs file

wscript.sleep 1000
loop

set oft = nothing
set ofs = nothing
Set winShell = Nothing

Lennox

@Lennox Jacob
Just out of curiosity, in your original code, you had:

do until oft.items.count >= winShell.ofs.items.Count

Did you ever try:

do until WinShell.oft.items.count >= winShell.ofs.items.Count

Tested on Windows 8, and works:

Sub ZipIt (source, dest) '** VBS Script to zip files from folder [source] to zip file [dest] **

  Dim fso, winShell, of
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set winShell = createObject("shell.application")

  Set of = fso.CreateTextFile(dest, True)
  of.write("PK" & chr(5) & chr(6) & string(18,chr(0))) '**Make an empty Zip File
  of.Close
  Set of = nothing
  Set fso = nothing

  Dim oft, ofs
  Set oft = winShell.NameSpace(dest)
  Set ofs = winShell.NameSpace(source)

  if oft is nothing Then WScript.Echo "Dest Fail !!!"
  if ofs is nothing Then WScript.Echo "Source Fail !!!"
  oft.CopyHere ofs.Items  '** Copy files

  Do until oft.items.Count >= ofs.items.Count '** Wait copy to finish
    WScript.sleep 1000
  Loop

  Set oft = nothing
  Set ofs = nothing
  Set winShell = Nothing

End Sub

'*** Test ***

ZipIt "C:\\Users\\Admin\\Desktop\\The Files", "C:\\Users\\Admin\\Desktop\\thePack.zip"

Hi Greg Olone,
I did not try that in the original because the original did not have oft and ifs, but it was tried in the last posted version and what happened is this…

In the compiler - Vbsedit - it will create the complete zip - files And folders included but With the error

Lennox

Hi Rick,

Thanks again, but…

I am using Windows 7
This version is the only one that worked as a standalone .vbs file, some of the others only worked and with an error message in the compiler - Vbsedit - but not as a standalone .vbs file.

The problem with this version is that only files in the folder to be zipped are zipped, folders are not zipped.

Please try it on Windows with files and folder in “C:\Users\Admin\Desktop\The Files” and let me know.

Thanks again for your patience, time and assistance. I really do appreciate it.

Lennox

Well… This VBS is how Microsoft expects and works. I used notepad. Not using a compiler here, just copy that code to zipit.vbs, edit the line [ ZipIt “C:\Users\Admin\Desktop\The Files”, “C:\Users\Admin\Desktop\thePack.zip” ] to a proper one for your case.

Yes. My test was done with a folder with files and folders inside. Works perfectly.

My user had administrative authority. (Admin).

Hi Rick,

Thanks again.

I did exactly what you said. I modified the line,
ZipIt “C:\Users\Admin\Desktop\The Files”, “C:\Users\Admin\Desktop\thePack.zip”
to
ZipIt “C:\Users\ Presario User\Desktop\The Files”, “C:\Users\ Presario User\Desktop\thePack.zip” ’ Presario User is Administrator

This is what happens…
I am using Windows 7, I will try it on Windows 8 this evening since I do not have access to Windows 8 presently.

Double-clicking the .vbs file works in the sense that there are no messages and a zip file is created.
The folder to be zipped has 3 files and 5 folders that have files in them. - total 60.1 MB

Sometimes one file alone is in the zipped file, sometimes two and sometimes three, at no time do any of the folders appear in the zipped file.

What I also find is that I have to relaunch the desktop to see the zipped file.
I create a shortcut to the desktop, Desktop shortcut, and put it on the desktop, so when i want to open the desktop folder I double-click the shortcut and then I see the zipped file there.

I do not know if something is wrong with my computer but I will do a clean install and try it again - This is not a problem, I use Acronis to do it and it does it in a few minutes.

Thanks again.

Lennox

Well… When I tested I used very small files, maybe this way I can’t see your problem.

What are you trying to accomplish? Maybe there is some easier/safer way to do what you need. I feel you need a way to call a compressor from a Xojo program under MS Windows and pack a directory in a compressed file. Is this what you want?
Why you don’t just grab a 7zip, command line version, and pack with your program?

http://www.7-zip.org/download.html

Hi Rick A.,

I just tried your last version on Windows 8 and it works perfectly… thanks for all your efforts, I do appreciate it.

I have not done the clean install on my Windows7 as yet but I will do that shortly, now that I have it working on Windows 8, and I will let you know.

Regarding your last post, yes, that is what I want but the only hassle now is getting it to work on Windows 7. The rest I can do.

If I cannot get it to work on Windows 7 I will use your 7.zip suggestion.

Even if it works on Windows 7, I will still try out the 7.zip way as you suggested just for the practice.

Thanks again Rick and I really do appreciate your time, effort and assistance.

Lennox

Hi Rick A,

I did the clean install (Backup) on my Windows7 and it works fine, sorry about that, it was my computer that was having the problem.

Thanks again.

Lennox

Hi Rick,

One last thing…

This is my current code

Sub ZipIt (source, dest) '** VBS Script to zip files from folder [source] to zip file [dest] **

Dim fso, winShell, of
Set fso = CreateObject(“Scripting.FileSystemObject”)
Set winShell = createObject(“shell.application”)
Set of = fso.CreateTextFile(dest, True)

of.write(“PK” & chr(5) & chr(6) & string(18,chr(0))) '**Make an empty Zip File
of.Close
Set of = nothing
Set fso = nothing

Dim oft, ofs
Set oft = winShell.NameSpace(dest)
Set ofs = winShell.NameSpace(source)

if oft is nothing Then WScript.Echo “Dest Fail !!!”
if ofs is nothing Then WScript.Echo “Source Fail !!!”
oft.CopyHere ofs.Items '** Copy files

Do until oft.items.Count >= ofs.items.Count '** Wait copy to finish
WScript.sleep 1000
Loop

Wscript.Echo “Zip completed”
Set oft = nothing
Set ofs = nothing
Set winShell = Nothing

End Sub

ZipIt “C:\Users\PRESAR~1\DOCUME~1\JONESQ~1”, “C:\Users\PRESAR~1\DOCUME~1\JONESQ~1.zip”

What I am doing is creating a zip file from my RealStudio program via a .vbs script

So, in my app I will create the .vbs script then launch it.
In creating the .vbs script I compose the line
ZipIt “C:\Users\PRESAR~1\DOCUME~1\JONESQ~1”, “C:\Users\PRESAR~1\DOCUME~1\JONESQ~1.zip”
like this
"ZipIt " + chr(34) + app.gMyVeryLongNameFolder.Shellpath + chr(34) + ", " + chr(34) + app.gMyVeryLongNameFolder.Shellpath + “.zip” + chr(34)

Everything is fine so far. The problem is this
The source folder, app.gMyVeryLongNameFolder, has a very long name, so in the created .vbs script it is this
ZipIt “C:\Users\PRESAR~1\DOCUME~1\JONESQ~1”, “C:\Users\PRESAR~1\DOCUME~1\JONESQ~1.zip”

PRESAR~1 is suppose to be Presario User
DOCUME~1 is suppose to be Documents
JONESQ~1 is a very long name which will vary from user to user

As you notice the zip file on disk is named JONESQ~1.zip and that is my problem.

I would like it to have its original very long name. How can I achieve that?

I have a thought but I do not know how to do it, my thought is this…

If this line
oft.CopyHere ofs.Items '** Copy files
could be modified to create another folder with whatever name is OK, and put the Zip in that folder.

Something like this in PseudoCode

Dim MyEnclosingFolder as folder item
MyEnclosingFolder = oft.Parent.Child(“Whatever name is suitable”)

MyEnclosingFolder.CopyHere ofs.Items

Just my thought.

Lennox