Unzip file error when running VBS script from a ba

2020-03-30 06:55发布

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.

2条回答
Rolldiameter
2楼-- · 2020-03-30 07:27

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.

查看更多
唯我独甜
3楼-- · 2020-03-30 07:34
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

查看更多
登录 后发表回答