windows batch: télécharger le fichier dans le dossier actuel

Je voudrais télécharger un fichier à partir d’un script .bat dans Windows 7 et le placer dans le dossier actuel (où est le script).

J’ai essayé de cette façon, sans (bons) résultats:

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

J’ai eu ceci:

  Unable to add file - 0x80070005 

Pourquoi? Je suppose que le concat de chaîne échoue

(Je sais que bitsadmin est obsolète dans Win7)

Merci d’avance!

Votre problème était les espaces de chaque côté du signe égal. Ils sont inclus dans le nom de la variable et dans le contenu de la variable – plus le manque de barre oblique inverse que vous avez trouvé.

Testez ceci:

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

EDIT: Si vous exécutez ceci en tant qu’administrateur, essayez le code modifié ci-dessus.
car lors de l’exécution avec des permissions élevées, le répertoire de travail change.

S’il vous plaît signaler tous les messages d’erreur lorsque vous dites que quelque chose ne fonctionne pas, donc le problème peut être résolu.

PowerShell

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

de cmd env

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

référence

J’ai résolu moi-même, sans utiliser la variable FILENAME:

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

EDITComment puis-je télécharger un fichier avec un fichier batch sans utiliser d’outils externes?

Je pense que Bitsadmin ne fonctionne pas avec les chemins relatifs et que vous devez append le nom complet au fichier local.

Vous pouvez vérifier aussi mon script bitsadmin qui passe beaucoup de travail – il accepte deux arguments url et le chemin du fichier local (et un numéro de délai d’expiration – la valeur par défaut est 5). au nom du fichier local:

  @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 /ssortingpchart /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 

Vous pouvez également consulter cet hybride auto-compilé jscript.net (enregistrez-le sous .bat):

 @if (@X)==(@Y) @end /****** jscript comment ****** @echo off :::::::::::::::::::::::::::::::::::: ::: comstack 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 comstackr 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:Ssortingng[] = 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); } 

Utilisez simplement Powershell, ce ne sera pas un problème.

 @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 

Cela peut prendre jusqu’à 20 secondes, plus ou moins selon la taille du fichier.

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

Celui-ci fonctionne. Changez la ligne

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

à

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