![](/img/trans.png)
[英]Windows Batch file, get the current folder the recursive function is in
[英]windows batch: download file into current folder
我想从Windows 7中的.bat脚本下载文件,并将其放入当前文件夹(脚本所在的位置)。
我以这种方式尝试,但没有(好)结果:
SET FILENAME = "name.ext"
bitsadmin.exe /transfer "JobName" http://www.someurl.com/file.ext %cd%%FILENAME%
我懂了:
Unable to add file - 0x80070005
为什么? 我想字符串concat失败
(我知道在Win7中不推荐使用bitsadmin)
先感谢您!
您的问题是等号两边的空格。 它们同时包含在变量名称和变量内容中-加上缺少的反斜杠。
测试一下:
@echo off
SET "FILENAME=%~dp0\name.ext"
bitsadmin.exe /transfer "JobName" http://www.someurl.com/file.ext "%FILENAME%"
编辑:如果您以管理员身份执行此操作,请尝试上面的已编辑代码。
因为以提升的权限运行时,工作目录会更改。
当您说某事不起作用时,请报告所有错误消息,以便可以解决问题。
电源外壳
start-bitstransfer -source protocol://url.host/url.path -destination C:\destination\file.ext
来自cmd env
powershell -command "start-bitstransfer -source protocol://url.host/url.path -destination C:\destination\file.ext"
编辑 - 如何在不使用任何外部工具的情况下下载带有批处理文件的文件?
我认为Bitsadmin无法使用相对路径,因此您需要在本地文件中添加全名。
您还可以检查我的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
您还可以检查此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);
}
我自己解决了,没有使用FILENAME变量:
bitsadmin.exe /transfer "JobName" http://www.someurl.com/file.ext "%cd%\name.ext"
只需使用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
最多可能需要20秒,或多或少取决于文件大小。
@echo off
SET "FILENAME=%~dp0\name.ext"
bitsadmin.exe /transfer "JobName" http://www.someurl.com/file.ext "%FILENAME%"
pause
这个作品。 换线
SET "FILENAME=%~dp0\name.ext"
至
SET "FILENAME=%~dp0\Your File Name"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.