windows windows批处理:将文件下载到当前文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25732307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:42:11  来源:igfitidea点击:

windows batch: download file into current folder

windowsbatch-file

提问by dar0x

I would to dowload a file from a .bat script in Windows 7, and put it into current folder (where the script is).

我想从 Windows 7 中的 .bat 脚本中下载一个文件,并将其放入当前文件夹(脚本所在的位置)。

I tried in this way, with no (good) results:

我以这种方式尝试过,没有(好的)结果:

    SET FILENAME = "name.ext"
    bitsadmin.exe /transfer "JobName" http://www.someurl.com/file.ext %cd%%FILENAME%

I got this:

我懂了:

    Unable to add file - 0x80070005

Why? I suppose that string concat fails

为什么?我想那个字符串 concat 失败了

(I know that bitsadmin is deprecated in Win7)

(我知道在 Win7 中不推荐使用 bitsadmin)

Thank you in advance!

先感谢您!

回答by foxidrive

Your problem was the spaces on either side of the equals sign. They get included in both the variable name and in the contents of the variable - plus the lack of the backslash that you found.

你的问题是等号两边的空格。它们包含在变量名称和变量的内容中 - 加上您发现的反斜杠的缺失。

Test this:

测试这个:

@echo off
SET "FILENAME=%~dp0\name.ext"
bitsadmin.exe /transfer "JobName" http://www.someurl.com/file.ext "%FILENAME%"

EDIT:In case you are executing this as admin, try the edited code above.
because when running with elevated permissions then the working directory changes.

编辑:如果您以管理员身份执行此操作,请尝试上面编辑过的代码。
因为当以提升的权限运行时,工作目录会发生变化。

Please report all error messages when you say that something doesn't work, so the problem can be worked out.

当您说某些东西不起作用时,请报告所有错误消息,以便解决问题。

回答by Valen

powershell

电源外壳

start-bitstransfer -source protocol://url.host/url.path -destination C:\destination\file.ext

from cmd env

从 cmd 环境

powershell -command "start-bitstransfer -source protocol://url.host/url.path -destination C:\destination\file.ext"

reference

参考

回答by npocmaka

EDIT- How can I download a file with batch file without using any external tools?

编辑-如何在不使用任何外部工具的情况下下载带有批处理文件的文件?

I think Bitsadmin does not work with relative paths and you need to add full name to the local file.

我认为 Bitsadmin 不适用于相对路径,您需要将全名添加到本地文件。

You can check also my bitsadmin script thats spends a lot of dirty work - it accepts two arguments url and the path to the local file (and a timeout number - default is 5).As you can see the it pre-pend %CD%to the local file name:

您还可以检查我的 bitsadmin 脚本,该脚本花费了大量繁琐的工作 - 它接受两个参数 url 和本地文件的路径(以及一个超时数 - 默认为 5)。正如您所看到的,它预先挂接%CD%到本地文档名称:

   @echo off
    setlocal
   :download

    if "%2" equ "" (
      call :help
      exit /b 5
   )

   if "%1" equ "" (
      call :help
      exit /b 6
   )
    set url=%~1
    set file=%~2
    rem ----
    if "%~3" NEQ "" (
        set /A timeout=%~3
    ) else (
        set timeout=5
    )

    bitsadmin /cancel download >nul
    bitsadmin /create /download download >nul 
    call bitsadmin /addfile download "%url%" "%CD%\%file%" >nul
    bitsadmin /resume download >nul 
    bitsadmin /setproxysettings download AUTODETECT >nul

    set /a attempts=0
    :repeat
    set /a attempts +=1
    if "%attempts%" EQU "10" (
        echo TIMED OUT
        endlocal
        exit /b 1
    )
    bitsadmin /info download /verbose | find  "STATE: ERROR"  >nul 2>&1 && endlocal &&  bitsadmin /cancel download && echo SOME KIND OF ERROR && exit /b 2
    bitsadmin /info download /verbose | find  "STATE: SUSPENDED" >nul 2>&1 && endlocal &&  bitsadmin /cancel download &&echo FILE WAS NOT ADDED && exit /b 3
    bitsadmin /info download /verbose | find  "STATE: TRANSIENT_ERROR" >nul 2>&1 && endlocal &&  bitsadmin /cancel download &&echo TRANSIENT ERROR && exit /b 4
    bitsadmin /info download /verbose | find  "STATE: TRANSFERRED" >nul 2>&1 && goto :finishing 

   w32tm /stripchart /computer:localhost /period:1 /dataonly /samples:%timeout%  >nul 2>&1
    goto :repeat
    :finishing 
    bitsadmin /complete download >nul
    echo download finished
    endlocal
   goto :eof

   :help
   echo %~n0 url file [timeout]
   echo.
   echo  url - the source for download
   echo  file - file name in local directory where the file will be stored
   echo  timeout - number in seconds between each check if download is complete (attempts are 10)
   echo.
   goto :eof

You can check also this jscript.net self compiled hybrid (save it as .bat):

你也可以检查这个 jscript.net 自编译混合(另存为 .bat):

@if (@X)==(@Y) @end /****** jscript comment ******

@echo off
::::::::::::::::::::::::::::::::::::
:::       compile the script    ::::
::::::::::::::::::::::::::::::::::::
setlocal
if exist simpledownloader.exe goto :skip_compilation

set "frm=%SystemRoot%\Microsoft.NET\Framework\"
:: searching the latest installed .net framework
for /f "tokens=* delims=" %%v in ('dir /b /s /a:d /o:-n "%SystemRoot%\Microsoft.NET\Framework\v*"') do (
    if exist "%%v\jsc.exe" (
        rem :: the javascript.net compiler
        set "jsc=%%~dpsnfxv\jsc.exe"
        goto :break_loop
    )
)
echo jsc.exe not found && exit /b 0
:break_loop


call %jsc% /nologo /out:"simpledownloader.exe" "%~dpsfnx0"
::::::::::::::::::::::::::::::::::::
:::       end of compilation    ::::
::::::::::::::::::::::::::::::::::::
:skip_compilation

:: download the file


::
::::::::::
 simpledownloader.exe "%~1" "%~2"
::::::::
::

exit /b 0


****** end of jscript comment ******/

import System;
var arguments:String[] = Environment.GetCommandLineArgs();
var webClient:System.Net.WebClient = new System.Net.WebClient();
print("Downloading " + arguments[1] + " to " + arguments[2]);
try {
    webClient.DownloadFile(arguments[1], arguments[2]);
} catch (e) {

        Console.BackgroundColor = ConsoleColor.Green;
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine("\n\nProblem with downloading " + arguments[1] + " to " + arguments[2] + "Check if the internet address is valid");
        Console.ResetColor();
        Environment.Exit(5);
}

回答by dar0x

I solved on my own, without using FILENAME variable:

我自己解决了,没有使用 FILENAME 变量:

    bitsadmin.exe /transfer "JobName" http://www.someurl.com/file.ext "%cd%\name.ext"

回答by scriptmastere02

Just use Powershell it wont be a problem.

使用 Powershell 不会有问题。

@echo off
setlocal enabledelayedexpansion
set _wdraft=%~dp0file.txt
set _dlwebf=http://df291a01bce923.sampledl.com/d/file.txt
Powershell -Command "(New-Object Net.WebClient).DownloadFile('%_dlwebf%','%_wdraft%')"
endlocal

This may take up to 20 seconds, more or less depending on the file size.

这可能需要长达 20 秒的时间,或多或少取决于文件大小。

回答by Ultra Gamer Ph

@echo off
SET "FILENAME=%~dp0\name.ext"
bitsadmin.exe /transfer "JobName" http://www.someurl.com/file.ext "%FILENAME%"
pause

This one works. Just change the line

这个有效。换线就好

SET "FILENAME=%~dp0\name.ext" 

to

SET "FILENAME=%~dp0\Your File Name"