1

When running the VBS unzip code batch in Win 10, I'm getting the error below. What can be a reason for this, given it was reported working before by others? Changing target directories or explicitly setting them doesn't affect the error.

@echo off
setlocal enabledelayedexpansion
cd /d %~dp0
set vbs="%temp%\_.vbs"
call :UnZipFile "E:\Temp\" "%USERPROFILE%\Downloads\archive.zip\"
exit /b

:UnZipFile <ExtractTo> <newzipfile>
if exist %vbs% del /f /q %vbs%
>%vbs%  echo Set fso = CreateObject("Scripting.FileSystemObject")
>>%vbs% echo If NOT fso.FolderExists(%1) Then
>>%vbs% echo fso.CreateFolder(%1)
>>%vbs% echo End If
>>%vbs% echo set objShell = CreateObject("Shell.Application")
>>%vbs% echo set FilesInZip=objShell.NameSpace(%2).items
>>%vbs% echo objShell.NameSpace(%1).CopyHere(FilesInZip)
>>%vbs% echo Set fso = Nothing
>>%vbs% echo Set objShell = Nothing
cscript //nologo %vbs%
if exist %vbs% del /f /q %vbs%

::error
VBScript runtime error: Object required: 'objShell.NameSpace(...)'

EDIT: changed .zip file location, now the script works. The error reason was user Download folder moved to a different volume compare to default %USERPROFILE%\Downloads. The script seems to have issues working properly with system folder symlinks.

Community
  • 1
  • 1
sambul35
  • 1,000
  • 11
  • 21

2 Answers2

1
Set objShell = CreateObject("Shell.Application")
Set Ag=Wscript.Arguments
set WshShell = WScript.CreateObject("WScript.Shell")

Set DestFldr=objShell.NameSpace(Ag(1))
Set SrcFldr=objShell.NameSpace(Ag(0))
Set FldrItems=SrcFldr.Items
DestFldr.CopyHere FldrItems, &H214
Msgbox "Finished"

This code unzips.

Microsoft Windows [Version 10.0.10240]
(c) 2015 Microsoft Corporation. All rights reserved.

C:\Users\David Candy>"C:\Users\David Candy\Desktop\David\Documents\Assorted\Scripts\UnZip.vbs" "C:\Users\David Candy\Desktop\42 - ProcessExplorer.zip" "C:\Users\David Candy\Desktop\New Folder (3)"

Here's a debugging version. Run it with CScript.

On Error Resume Next

Set objShell = CreateObject("Shell.Application")
Set Ag=Wscript.Arguments
Set fso = CreateObject("Scripting.FileSystemObject")

wscript.echo Ag(0) & "   Is Drive " & fso.DriveExists(Ag(0)) & "   Is Folder " & fso.FolderExists(Ag(0)) & "   Is File " & fso.FileExists(Ag(0))
wscript.echo Ag(1) & "   Is Drive " & fso.DriveExists(Ag(1)) & "   Is Folder " & fso.FolderExists(Ag(1)) & "   Is File " & fso.FileExists(Ag(1))

wscript.echo ""

If fso.FileExists(Ag(1)) = False Then
    Wscript.echo "Zip doesn't exist so trying to Create"
    Set ts = fso.OpenTextFile(Ag(0), 8, vbtrue)
    If Err.Number <> 0 Then
        Wscript.echo "Opening Dest " & Err.number & " " & Err.Description & " " & Err.Source
        Err.Clear
    End If

    BlankZip = "PK" & Chr(5) & Chr(6)
    For x = 0 to 17
        BlankZip = BlankZip & Chr(0)
    Next
    ts.Write BlankZip
    If Err.Number <> 0 Then
        Wscript.echo "Writing Dest " & Err.number & " " & Err.Description & " " & Err.Source
        Err.Clear
    End If
End If

Set DestFldr=objShell.NameSpace(Ag(1))
If Err.Number <> 0 Then
    Wscript.echo "Creating Dest " & Err.number & " " & Err.Description & " " & Err.Source
    Err.Clear
End If

Set SrcFldr=objShell.NameSpace(Ag(0))
If Err.Number <> 0 Then
    Wscript.echo "Creating Source " & Err.number & " " & Err.Description & " " & Err.Source
    Err.Clear
End If


wscript.echo DestFldr.Self.Name & " " & DestFldr.Self.Type & " " & DestFldr.Self.IsBrowsable & " " & DestFldr.Self.IsFileSystem
If Err.Number <> 0 Then
    Wscript.echo Err.number & " " & Err.Description & " " & Err.Source
    Err.Clear
End If

wscript.echo SrcFldr.Self.Name & " " & SrcFldr.Self.Type & " " & SrcFldr.Self.IsBrowsable & " " & SrcFldr.Self.IsFileSystem
If Err.Number <> 0 Then
    Wscript.echo Err.number & " " & Err.Description & " " & Err.Source
    Err.Clear
End If


Set FldrItems=SrcFldr.Items
For Each Itm in FldrItems
    wscript.echo Itm.Name & " " & Itm.IsBrowsable & " " & Itm.IsFileSystem
Next


DestFldr.CopyHere FldrItems, &H10001210 
Msgbox "Finished"

This is the output

C:\Users\User>cscript //nologo "C:\Users\User\Desktop\David\Documents\Assorted\Scripts\UnZip - Copy.vbs" C:\symbols c:\file1.zip
C:\symbols   Is Drive False   Is Folder True   Is File False
c:\file1.zip   Is Drive False   Is Folder False   Is File False

Zip doesn't exist so trying to Create
Opening Dest 70 Permission denied Microsoft VBScript runtime error
Writing Dest 424 Object required Microsoft VBScript runtime error
424 Object required Microsoft VBScript runtime error
symbols File folder False True
wkernel32.pdb False True
wntdll.pdb False True

Showing I didn't have permission to create it.

C:\Users\User>cscript //nologo "C:\Users\User\Desktop\David\Documents\Assorted\Scripts\UnZip - Copy.vbs" C:\symbols c:\file2.zip
C:\symbols   Is Drive False   Is Folder True   Is File False
c:\file2.zip   Is Drive False   Is Folder False   Is File False

424 Object required Microsoft VBScript runtime error
symbols File folder False True
wkernel32.pdb False True
wntdll.pdb False True

Showing the file didn't exist.

This is when it works.

C:\Windows\system32>cscript //nologo "C:\Users\User\Desktop\David\Documents\Assorted\Scripts\UnZip - Copy.vbs" C: \symbols c:\file.zip C:\symbols Is Drive False Is Folder True Is File False c:\file.zip Is Drive False Is Folder False Is File True

file.zip Compressed (zipped) Folder True True symbols File folder False True wkernel32.pdb False True wntdll.pdb False True

I've change options to not recurse. See The list here.

https://msdn.microsoft.com/en-us/library/windows/desktop/bb775799%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396

  • Not for me when used as a batch function above. `\_.vbs(7, 30) Microsoft VBScript compilation error: Syntax error`. And &H214 is missing in the file, since & is a batch command separator. But even if I add it manually to the .vbs file, another error: `Subscript out of range`. Can you post working unzipping batch, since unzipping is only one of my larger batch several functions. – sambul35 Jul 26 '16 at 22:46
  • Calling from batch `cscript //nologo %vbs% "C:\Users\user\Downloads\archive.zip\" "C:\Temp\"` I got `_.vbs(7, 1) Microsoft VBScript runtime error: Object required: 'DestFldr'` – sambul35 Jul 26 '16 at 23:55
  • Added blank line in the file, removed backslash after .zip in Ag(0). Now error `Object required: 'Source folder'`. – sambul35 Jul 27 '16 at 00:10
  • Be aware that the names are opposite. SrcFolder is the destination folder and DestFolder is the zip file. That's because I swapped around Ag(0) and Ag(1) to change a ZIP program to an Unzip program. –  Jul 27 '16 at 00:28
  • I swapped the args in the cscript string, now the error is `Object required: 'DestFldr'` :) – sambul35 Jul 27 '16 at 00:34
  • Look at my answer and do it as says. –  Jul 27 '16 at 01:03
  • I didn't touch your script at all. And placed the arguments in the order like in your above example. But it gives the above error on my PC. You're free to stand by your assumption though. I upvoted your answer as a gratitude, as it may indeed work for you. – sambul35 Jul 27 '16 at 01:45
  • `script.vbs "C:\Users\user\Downloads\archive.zip" "%Temp%"` –  Jul 27 '16 at 01:53
  • Well, if you follow the link to the code I posted, it was upvoted 21 times, and accepted as an answer. I used an exact copy of it. I run Cmd as Admin, and the above folder definitely exists by default on all Windows PCs. Unfortunately, %Temp% that points to the session user's Temp folder instead, doesn't work as well for me. Let me try to re-login or reboot the PC. Thanks again for your feedback, I think negative result is a normal outcome of any test. Don't take it personally. Note, my question is about a batch file, not stand alone VBS code, – sambul35 Jul 27 '16 at 02:15
  • Temp is not on standard windows. C:\windows\temp is the system tmp directory. Batch file can't unzip. So you need to use another language. –  Jul 27 '16 at 02:22
  • Unzipping is just one of several tasks I need to run from one batch. This [answer](http://stackoverflow.com/questions/21704041/creating-batch-script-to-unzip-a-file-without-additional-zip-tools) says, batch file can unzip. I rebooted the PC, and still get the same error `VBScript runtime error: Object required: 'DestFldr'`. – sambul35 Jul 27 '16 at 02:41
  • I just run your VBS script standalone, and it gave exact same error. This means, my VBS install may be damaged, or different. Did you test your script on Win 10 64-bit PC? If I swap the args when calling the script, it runs without an error, but doesn't unzip the file. Instead it creates a new folder archive.zip in Downloads folder, and copies the entire %temp% current content to it. I know it sounds crazy. Will try running it on a different PC, or from another dir. :) – sambul35 Jul 27 '16 at 02:54
  • That's because you swapped something around earlier. Go back to my scpipt in the answer. –  Jul 27 '16 at 03:57
  • Are you serious? :) Will check tomorrow on another PC. Here's my .vbs content: `Set objShell = CreateObject("Shell.Application") Set Ag=Wscript.Arguments set WshShell = WScript.CreateObject("WScript.Shell") Set DestFldr=objShell.NameSpace(Ag(1)) Set SrcFldr=objShell.NameSpace(Ag(0)) Set FldrItems=SrcFldr.Items DestFldr.CopyHere FldrItems, &H214 Msgbox "Finished"` – sambul35 Jul 27 '16 at 05:05
  • Just a reminder, you called a "gobblygook" the code written by an experienced coder with 5K rep, and now you say I can't copy 5 lines of codes. Don't you think there may be another explanation, like VBS methods don't work in Win10 the same way they did in WinXP or 7? I think we need to move on. – sambul35 Jul 27 '16 at 05:15
  • Here is the command line: `_.vbs "%USERPROFILE%\Downloads\archive.zip" "%temp%"` I also tried to swap the arguments. – sambul35 Jul 27 '16 at 05:24
  • I finally figured out the problem. The source .zip file was saved in global Downloads folder (can't what user it belongs), while the argument pointed to %userprofile%\Downloads folder. I copied the file, and now your standalone script works. Will make a batch hybrid based on it. Thanks again! :) – sambul35 Jul 27 '16 at 05:38
  • It is a documented API for accessing Windows folders as if they were explorer windows. If it doesn't work then it whoever is giving the data is giving wrong data. That's how proper programming works. By following the documentation. Always blame yourself. –  Jul 27 '16 at 05:43
  • Would you agree that documentation is often obsolete, and don't catch up with OS development? On the flip side, MS usually tries to maintain backwards compatibility. – sambul35 Jul 27 '16 at 05:48
  • For some reason, on my PC "any" VBS unzip code doesn't work if "source folder" is located in a moved system folder like Downloads. It requires .zip file presence in the same name folder in %userprofile%, which usually remains empty in case of such move, despite the special symlink to the moved Downloads folder is also present in %userprofile% folder. It might be, the VBS CopyHere method doesn't work with symlinks or such. – sambul35 Jul 27 '16 at 11:54
  • It the Window's Shell CopyHere not VBS's. You are not working with files but with the Shell's COM objects. It is the same as opening it in Explorer. Every COM language (VB/JS/VBA/VBS/C++/and others) can use COM objects. There are no rules that you suggest. –  Jul 27 '16 at 22:12
  • Explorer handles symlinks properly, and knows what system folders current paths are. Is there a way to edit your script so it can follow current system folder path, like Downloads? – sambul35 Jul 27 '16 at 23:05
  • It asks Explorer to copy from one folder, any folder, to another folder, any folder. So you need to provide two explorer folders as parameters. –  Jul 27 '16 at 23:16
0

This batch hybrid is based on the VBS script suggested here by Noodles. It does file unzipping job fairly well, while having the advantage of not requiring a standalone VBS file compare to the original batch, or abandoning the batch script completely in favor of VBS. It does require a target directory for unzipped files to exist.

<!-- : Begin batch script
@echo off
set "dir=%temp%\Unzip" & set "file=%userprofile%\Downloads\archive.zip"
cscript //nologo "%~f0?.wsf" "%file%" "%dir%"
exit /b

----- Begin wsf script --->
<job><script language="VBScript">
Set objShell = CreateObject("Shell.Application")
Set Ag=Wscript.Arguments
set WshShell = WScript.CreateObject("WScript.Shell")

Set DestFldr=objShell.NameSpace(Ag(1))
Set SrcFldr=objShell.NameSpace(Ag(0))
Set FldrItems=SrcFldr.Items
DestFldr.CopyHere FldrItems, &H214
</script></job>

The VBS method seems to have issues with symlinks or locating a current folder path, if you moved the user Downloads folder from its default location. Use absolute paths in cscript arguments in this case instead of environmental variables.

sambul35
  • 1,000
  • 11
  • 21