如何从zip文件夹中提取文件,然后在VB脚本中更改目标文件夹的文件夹名称

时间:2017-05-24 01:46:51

标签: file batch-file vbscript zip directory

我需要将zip文件夹解压缩到另一个位置,并且在从源zip文件夹传输完所有文件后,我将编辑目标文件夹的名称以包含字符串:_complete

例如:来源= abc.zip目的地= abc_complete [所有文件都已转移]
我在vb中有一个代码,但我不太确定如何进一步增强它以满足我的要求。我还需要将其作为批处理文件运行。

'The location of the zip file.
ZipFile="C:\Test.Zip"
'The folder the contents should be extracted to.
ExtractTo="C:\Test\"

'If the extraction location does not exist create it.
Set fso = CreateObject("Scripting.FileSystemObject")
If NOT fso.FolderExists(ExtractTo) Then
    fso.CreateFolder(ExtractTo)
End If

'Extract the contants of the zip file.
set objShell = CreateObject("Shell.Application")
set FilesInZip=objShell.NameSpace(ZipFile).items
objShell.NameSpace(ExtractTo).CopyHere(FilesInZip)
Set fso = Nothing
Set objShell = Nothing

Dim tmpPath = "\ifp\images\715"
Dim newPath = "\ifp\images\V14"

If FS.FolderExists(sCurPath & tmpPath) Then
    Response.Write("The folder exists.")
    FS.MoveFolder sCurPath & tmpPath, sCurPath & newPath
Else
    Response.Write("The folder " & sCurPath & tmpPath & " does not exist.")
End If

非常感谢您的回复!

谢谢!

1 个答案:

答案 0 :(得分:0)

首先,您只需移动它就可以在VBscript中重命名文件夹。我会在ExtractTo上使用RegExp替换,在提取过程中将扩展名更改为_working,然后在最后将_working替换为_complete

您还提到需要将其作为.bat文件运行。 There is a way在.bat脚本中包含VBScript,而不必回显到临时文件。这是通过利用cscript中的一项功能来完成的,该功能可让您评估.bat文件as though it had a .wsf extension

如果您希望包含解压缩文件的目录与zip文件位于同一目录中,则以下脚本的语法类似extract.bat C:\Test.zip C:\Test,或仅extract.bat C:\Test.zip

<!-- : Begin batch script

@echo off & setlocal

if "%~1"=="" goto usage
if not exist "%~1" goto usage

rem // create destination folder if it doesn't exist
if not "%~2"=="" (
    if not exist "%~2" md "%~2"
    set "dest=%~f2"
)

rem // temporarily rename source file to .zip in case of .docx or similar
if /I not "%~x1"==".zip" ren "%~f1" "%~n1.zip"
pushd "%~dp1"

rem // execute VBScript job
cscript //nologo "%~f0?.wsf" "%~n1.zip" "%dest%"

rem // undo temporary rename of source file
if /I not "%~nx1"=="%~n1.zip" ren "%~n1.zip" "%~nx1"

exit /b

:usage
echo Usage: %~nx0 zipfile.zip [destination_dir]
exit /b

----- Begin wsf script --->
<job><script language="VBScript">

    Set fso = CreateObject("Scripting.FileSystemObject")
    set objShell = CreateObject("Shell.Application")
    Set rxp = CreateObject("VBScript.RegExp")
    rxp.IgnoreCase = True
    rxp.Pattern = "\.[^\.]+$"

    'The location of the zip file.
    ZipFile=fso.GetAbsolutePathName(WSH.Arguments(0))
    ExtractTo=rxp.Replace(ZipFile, "_working")

    'If destination specified, replace ExtractTo path
    if WSH.Arguments(1) <> "" Then
        ExtractTo=Replace(ExtractTo, fso.GetFile(ZipFile).ParentFolder,_
            fso.GetAbsolutePathName(WSH.Arguments(1)))
    End If

    'If the extraction location does not exist create it.
    If NOT fso.FolderExists(ExtractTo) Then
        fso.CreateFolder(ExtractTo)
    End If

    'Extract the contants of the zip file.
    set FilesInZip=objShell.NameSpace(ZipFile).Items()
    objShell.NameSpace(ExtractTo).CopyHere(FilesInZip)

    'Extraction complete.  Rename destination folder.
    rxp.Pattern = "_working$"
    fso.MoveFolder ExtractTo, rxp.Replace(ExtractTo, "_complete")

    Set fso = Nothing
    Set objShell = Nothing
    set rxp = Nothing

</script></job>