如何创建 .BAT 文件来下载并解压 zip 文件?

发布于 2024-09-02 09:45:56 字数 283 浏览 4 评论 0原文

如何创建 .BAT 文件以从 HTTP 服务器下载并解压 zip 文件?

我们有像 http://example.com/folder.zip 这样的链接和像 C:\Users\UserName\Some mix Русский English Adress\ 这样的绝对文件夹链接,

如果文件来自zip 存在于目录中,重写它们。

仅使用本机 Windows(xp vista win7 等)BAT 函数和文件。

您可以添加代码示例吗?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

温柔嚣张 2024-09-09 09:45:56

尝试这个混合 bat/vbs 脚本

@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

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
花之痕靓丽 2024-09-09 09:45:56

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"

folder.zip 下载到当前目录(或任何其他目录 - 必须存在)。将 folder.zip 解压到 folder/to/extract (自动创建)。

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

入怼 2024-09-09 09:45:56

如果您确实想使用bat文件,您可以看看: http://www.chami.com/tips/windows/062598W.html

批处理文件将使用名为:URL2File 的命令行工具

编辑:你的批处理文件应该看起来像这样(你需要安装 pkunzip 或另一个命令行工具(7-zip fe))

@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\

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 f.e.) 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\
傲性难收 2024-09-09 09:45:56

您可以使用 curl 下载文件。

手册包含几个示例

You can use curl to download the files.

The manual includes several examples

坚持沉默 2024-09-09 09:45:56

由于 Windows 7 包含 Powershell 恕我直言,您可以使用此 powershell 脚本: http://bwain-dump.blogspot.com/2009/01/powershell-script-to-unzip-many-files.html

如果不是powershell的话,我想,没有本机的做法。您可以使用提供命令行的 zip 实用程序,例如 7-zip

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

枉心 2024-09-09 09:45:56

如果您的 PC 与任何其他 Windows PC 一样,则应该安装 powershell。如果您尝试从 cmd 行或批处理脚本运行它,没问题,您可以将 powershell 一词放在任何命令前面,让它通过 cmd 提示符控制台运行!首先,您应该将您希望人们下载的文件上传到投递箱。然后获取可共享链接,将 www.dropbox.com 替换为 dl.dropboxusercontent.com 以创建不需要人们单击下载按钮的直接链接。然后创建一个像这样的脚本:

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

这将下载文件并将其解压缩到 C:/ 驱动器
它很简单,并且完全按照其需要执行的操作,不适用于 .RAR 文件。我希望这能解决您的问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文