在Windows 10中从批处理运行VBS脚本时解压缩文件错误

时间:2016-07-26 19:24:15

标签: batch-file vbscript cmd

在Win 10中运行VBS unzip code批处理时,我收到以下错误消息。可能是什么原因,因为据报道其他人之前工作过?更改目标目录或明确设置它们不会影响错误。

@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(...)'

编辑:更改.zip文件位置,现在脚本正常工作。错误原因是用户下载文件夹移动到不同的卷,与默认的%USERPROFILE%\ Downloads相比。该脚本似乎在系统文件夹符号链接方面有问题。

2 个答案:

答案 0 :(得分: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"

此代码解压缩。

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)"

这是一个调试版本。用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"

这是输出

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

显示我无权创建它。

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

显示该文件不存在。

这是它的工作原理。

C:\ Windows \ system32&gt; cscript // nologo“C:\ Users \ User \ Desktop \ David \ Documents \ Assorted \ Scripts \ UnZip - Copy.vbs”C: \ symbols c:\ file.zip C:\ symbols是驱动器错误文件夹为真是文件错误 c:\ file.zip驱动器错误文件夹是否为文件为真

file.zip Compressed(zipped)Folder True True symbols文件夹False True wkernel32.pdb False True wntdll.pdb False True

我改变选项以不递归。请参阅此处的列表。

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

答案 1 :(得分:0)

此批处理混合基于 Noodles 建议的VBS脚本。它确实可以很好地解压缩文件,同时具有不需要独立VBS文件与原始批处理相比的优点,或完全放弃批处理脚本以支持VBS。它确实需要一个目标目录来存在解压缩文件。

<!-- : 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>

如果您将用户下载文件夹从其默认位置移动,则VBS方法似乎存在符号链接或查找当前文件夹路径的问题。在这种情况下,使用cscript参数中的绝对路径而不是环境变量。