1

I am required to extract a zip folder into another location and after all the files have been transferred from the source zip folder, I am to edit the destination folder's name to include the string: _complete.

eg: Source = abc.zip Destination = abc_complete [once all files have been transferred]
I have a code in vb but i am not too sure as to how i can enhance it further to meet my requirement. I also need to run it as a batch file.

'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

Greatly appreciate a response!

Thank you!

rojo
  • 24,000
  • 5
  • 55
  • 101

1 Answers1

0

Well, firstly, you can rename a folder in VBscript simply by moving it. I would use a RegExp replacement on ExtractTo to change the extension to _working while the extraction is in progress, then replace _working with _complete at the end.

You also mentioned that you need to run this as a .bat file. There is a way to include VBScript within a .bat script without having to echo out to a temporary file. This is done by exploiting a feature in cscript that lets you evaluate the .bat file as though it had a .wsf extension.

Syntax of the following script is something like extract.bat C:\Test.zip C:\Test, or just extract.bat C:\Test.zip if you want the directory containing the extracted files to live in the same directory as the zip file.

<!-- : 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>
rojo
  • 24,000
  • 5
  • 55
  • 101