简体   繁体   English

批处理文件逐个下载并运行多个文件?

[英]Batch file download and run multiple files, one by one?

I'm trying to create a batch file that can download and run three files one at a time. 我正在尝试创建一个批处理文件,可以一次下载并运行三个文件。 Two of them are .bat files and one an .exe file. 其中两个是.bat文件,另一个是.exe文件。 I'm trying to use powershell commands for this within a .bat file as it needs to be executable. 我正在尝试在.bat文件中使用powershell命令,因为它需要是可执行的。 I'm struggling with this as I'm unfamiliar with both batch files and powershell. 我正在努力解决这个问题,因为我不熟悉批处理文件和PowerShell。

PowerShell.exe -Command "$url = "https://ufile.io/wqtvx""
PowerShell.exe -Command "$outpath = "$PSScriptRoot/downloadtest.bat""
PowerShell.exe -Command "Invoke-WebRequest -Uri $url -OutFile $outpath"
PowerShell.exe -Command "$wc = New-Object System.Net.WebClient"
PowerShell.exe -Command "$wc.DownloadFile($url, $outpath)"
PowerShell.exe -Command "$args = @("Comma","Separated","Arguments")"
PowerShell.exe -Command "Start-Process -Filepath "$PSScriptRoot/downloadtest.bat" -ArgumentList $args"

The above code will be repeated three times for each file I'd like to download. 对于我要下载的每个文件,上面的代码将重复三次。 I am not very sure where to store the file I'd like to download online so I uploaded it to a free file storage website. 我不太确定在哪里存储我想在线下载的文件,所以我将其上传到免费的文件存储网站。 If anyone can help me get this working I'd appreciate it. 如果有人能帮助我开展这项工作,我会很感激。

The main problem with this script is that the powershell.exe -c command will only run the code after it on one iteration of powershell. 这个脚本的主要问题是powershell.exe -c命令只会在powershell.exe -c一次迭代后运行代码。 Once it's finished running the code, it "Closes" that powershell window (Trying to explain it simple as possible). 一旦它运行完代码,它就会“关闭”powershell窗口(试着尽可能地解释它)。

Powershell has the ability to link commands on a simple command line, similar to batches & symbol, powershell has the ; Powershell能够在简单的命令行上链接命令,类似于批处理&符号,powershell具有; for this job. 为了这份工作。 Furthermore, your code still would not have worked as stated by James C. - The link isn't direct to the file, it requires you to 'click' a button to initializes the download. 此外,您的代码仍然无法按照James C.的说明工作。 - 链接不直接指向文件,它要求您“单击”按钮以初始化下载。

Not to advertise but I suggest GitHub or MediaFire as you can get direct download links to each file. 不要做广告,但我建议使用GitHub或MediaFire,因为您可以获得每个文件的直接下载链接。 No javascript interaction needed on the webpage to download them. 网页上无需进行javascript交互即可下载。


This script will put most of your commands on a single line, thus allowing the download from powershell. 此脚本将大部分命令放在一行,从而允许从powershell下载。 This particular command will work on all vs 1.0+ of powershells. 这个特殊的命令适用于所有vs 1.0+的powershells。 A simple start Batchfile.bat takes care of the rest or you can go on to add onto the command by adding ; Start-Process -Filepath $PSScriptRoot/downloadtest.bat -ArgumentList exc 一个简单的start Batchfile.bat负责其余的工作,或者你可以通过添加继续添加到命令; Start-Process -Filepath $PSScriptRoot/downloadtest.bat -ArgumentList exc ; Start-Process -Filepath $PSScriptRoot/downloadtest.bat -ArgumentList exc ... ; Start-Process -Filepath $PSScriptRoot/downloadtest.bat -ArgumentList exc ...

Powershell.exe -Command "(New-Object Net.WebClient).DownloadFile('http://Site/Batchfile.bat', '%~dp0downloadtest.bat')"

You are starting a separate Powershell process on each line, so each line is completely unaffected by the other. 您正在每条线上开始单独的Powershell过程,因此每条线完全不受另一条线的影响。 This won't work, since you're defining variables and assigning values to them on one line, then trying to use them later in an entirely separate process which has no such variable. 这是行不通的,因为你在一行中定义变量并为它们赋值,然后尝试在一个完全独立的过程中使用它们,而这个过程没有这样的变量。 The simplest answer is to separate all of your statements by semicolons as mentioned by double-beep . 最简单的答案是用双哔声提到的用分号分隔所有语句。

PowerShell.exe -Command "$url = 'https://ufile.io/wqtvx'; $outpath = '$PSScriptRoot/downloadtest.bat'; Invoke-WebRequest -Uri $url -OutFile $outpath; $wc = New-Object System.Net.WebClient; $wc.DownloadFile($url, $outpath); $args = @('Comma','Separated','Arguments'); Start-Process -Filepath '$PSScriptRoot/downloadtest.bat' -ArgumentList $args"

Note you have misused quotation marks in your code. 请注意,您的代码中使用了引号。 Look at the syntax highlighting for an idea as to what's going on in your first line: 查看关于第一行中发生了什么的想法的语法突出显示:

PowerShell.exe -Command "$url = "https://ufile.io/wqtvx""

Powershell will interpret your command as the first string, "$url = ", which will return an error because it's not valid Powershell syntax. Powershell会将您的命令解释为第一个字符串“$ url =”,它将返回错误,因为它不是有效的Powershell语法。 Then https://ufile.io/wqtvx is outside of any quotes, and Powershell will again have no idea how to interpret this and throw an error. 然后https://ufile.io/wqtvx超出任何引号,Powershell将再次不知道如何解释这个并抛出错误。 Finally, you have the empty string "" tacked onto the end, which adds no meaning whatsoever to the statement. 最后,你将空字符串“”添加到结尾,这对语句没有任何意义。 Use single quotes for quotes within quotes as shown in my example above. 在引号中使用单引号,如上例所示。

Generally, this much code is too much for a one liner. 一般来说,对于一个班轮来说,这么多代码太多了。 It's difficult to read and troubleshoot. 它很难阅读和排除故障。 I would suggest creating a Powershell script, which is just a text file with a .ps1 file extension, and naming it something like "DownloadFile.ps1". 我建议创建一个Powershell脚本,它只是一个带有.ps1文件扩展名的文本文件,并命名为“DownloadFile.ps1”。 In that file, put the following code: 在该文件中,输入以下代码:

$url = "https://ufile.io/wqtvx"
$outpath = "$PSScriptRoot/downloadtest.bat"
Invoke-WebRequest -Uri $url -OutFile $outpath
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($url, $outpath)
$args = @("Comma","Separated","Arguments")
Start-Process -Filepath "$PSScriptRoot/downloadtest.bat" -ArgumentList $args

Then call that from your batch file with something like: 然后从批处理文件中调用以下内容:

Powershell.exe -ExecutionPolicy Bypass -Command "C:\Path\To\DownloadFile.ps1"

Each powershell process is separate and know what has been done in another, you need to either include all your code in a single -Command param, or use the -File and pass it a multi line powershell script. 每个PowerShell进程是分开并了解已在其他已经完成,你需要或者包含在一个单一的所有代码-Command PARAM,或使用-File并将它传递一个多行PowerShell脚本。

Also your download will fail as that link isn't direct to the file, it requires you to 'click' a button to initialise the download. 此外,您的下载将失败,因为该链接不直接指向该文件,它要求您“单击”按钮以初始化下载。

Because of this you will get a downloadtest.bat that contains the HTML source for the download page itself . 因此,您将获得一个downloadtest.bat ,其中包含下载页面本身的HTML源代码。


Once you have a filehost with a direct URL link, you can use a semicolon ; 一旦你有一个带有直接URL链接的文件主机,就可以使用分号; between the two commands Invoke-WebRequest and Start-Process so they are on a single line: 在两个命令Invoke-WebRequestStart-Process ,它们在一行上:

PowerShell.exe -Command "Invoke-WebRequest -Uri https://filehost.com/directlink/downloadtest.bat -OutFile $PSScriptRoot/downloadtest.bat ; Start-Process -Filepath $PSScriptRoot/downloadtest.bat -ArgumentList @('Comma','Separated','Arguments')"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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