简体   繁体   中英

How to create a .BAT file to download and unpack a zip file?

How to create a .BAT file to download and unpack a zip file from HTTP server?

We have links like http://example.com/folder.zip and absolute folder link like C:\\Users\\UserName\\Some mixed Русский English Adress\\

if files from zip exist in directory owerrite them.

using only native windows (xp vista win7 etc) BAT functions and files.

Could you add code example, please.

Try this hybrid bat/vbs script

@echo off
 > %temp%\~tmp.vbs echo sUrl = "http://www.unicontsoft.com/file.zip"
>> %temp%\~tmp.vbs echo sFolder = "c:\temp\unzip"
>> %temp%\~tmp.vbs (findstr "'--VBS" "%0" | findstr /v "findstr")
cscript //nologo %temp%\~tmp.vbs
del /q %temp%\~tmp.vbs
goto :eof

'--- figure out temp file & folder
With CreateObject("WScript.Shell")  '--VBS
    sTempFile = .Environment("Process").Item("TEMP") & "\file.zip"  '--VBS 
    sTempFolder = .Environment("Process").Item("TEMP") & "\file.zip.extracted"  '--VBS
End With    '--VBS

'--- download
WiTh CreateObject("MSXML2.XMLHTTP") '--VBS
    .Open "GET", sUrl, false    '--VBS
    .Send() '--VBS
    If .Status = 200 Then   '--VBS
        ResponseBody = .ResponseBody    '--VBS
        With Createobject("Scripting.FileSystemObject") '--VBS
            If .FileExists(sTempFile) Then  '--VBS
                .DeleteFile sTempFile   '--VBS
            End If  '--VBS
        End With    '--VBS
        With CreateObject("ADODB.Stream")   '--VBS
            .Open   '--VBS
            .Type = 1 ' adTypeBinary    '--VBS
            .Write ResponseBody '--VBS
            .Position = 0   '--VBS
            .SaveToFile sTempFile   '--VBS
        End With    '--VBS
    End If  '--VBS
End With    '--VBS

'--- extract
With CreateObject("Scripting.FileSystemObject") '--VBS
    On Error Resume Next    '--VBS
    .CreateFolder sFolder   '--VBS
    .DeleteFolder sTempFolder, True '--VBS
    .CreateFolder sTempFolder   '--VBS
    On Error GoTo 0 '--VBS
    With CreateObject("Shell.Application")  '--VBS
        .NameSpace(sTempFolder).CopyHere .NameSpace(sTempFile).Items    '--VBS
    End With    '--VBS
    .CopyFolder sTempFolder, sFolder, True  '--VBS
    .DeleteFolder sTempFile, True   '--VBS
    .DeleteFile sTempFile, True '--VBS
End With    '--VBS

If you really want to use a bat-file you may have a look at: http://www.chami.com/tips/windows/062598W.html

The batch file will use a command-line-tools called: URL2File

EDIT: Your batch file should look something like (you need to have pkunzip or another cmd-line tool (7-zip fe) installed for that)

@echo off
c:
cd\files
URL2File http://www.server.com/file1.zip file1.zip

for %%f in (file1.zip) do pkunzip %%f c:\user\unziped_files\%%f\

You can use curl to download the files.

The manual includes several examples

Since Windows 7 includes Powershell IMHO, you could use this powershell script: http://bwain-dump.blogspot.com/2009/01/powershell-script-to-unzip-many-files.html

If not powershell then, I think, there is no native way of doing it. You may go for a zip utility that provides command line eg 7-zip

download_and_unzip.bat:

powershell -command "Start-BitsTransfer -Source http://example.com/folder.zip -Destination folder.zip"
powershell -command "Expand-Archive folder.zip folder/to/extract"

Download folder.zip to the current dir (or any other - must exist). Extract folder.zip to folder/to/extract (created automatically).

If your PC is like any other windows PC it should have powershell installed. if you are trying to run it from a cmd line or batch script, no problem, you can put the word powershell in front of any command to have it run its through the cmd prompt console! first you should upload the file you want people to be downloading to drop box. then get a sharable link, replace the www.dropbox.com with dl.dropboxusercontent.com to create a direct link that wont require people to click a download button. then make a script like this:

start /MAX *drop box link*
timeout 3 >nul
powershell Expand-Archive C:\Users\%USERNAME%\Downloads\*file name* C:/

this will download the file and unzip it to the C:/ drive it is simple and does exactly what it needs to do, does not work for .RAR files. I hope this solves your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM