使用 for 循环将下载部分添加到 powershell 脚本
use for loop to add downloading sections to a powershell script
我想构建一个 powershell 脚本来下载一组特定的文件。该集合会定期更改,因此 for
循环是更新流程的最简洁方法。
每个文件的 URLs 每天都会更新日期和唯一序列号,但我已经有了一个提取该信息并将其保存到文件的过程。我还有一种构建 PS 脚本以从 URL:
下载特定文件的方法
set /p file_info=<"<path to file with info>"
set file_dir=<known page directories>
set DL=C:\Users\%USERNAME%\Downloads\test.ps1
echo $url = "http://<domain>.com/%file_dir%/%file_info%" > "%DL%"
echo $output = "C:\Users\%USERNAME%\Downloads\%file_info%" >> "%DL%"
echo (New-Object System.Net.WebClient).DownloadFile($url, $output) >> "%DL%"
我还可以循环添加到 PS 脚本:
setlocal enabledelayedexpansion
set /p obj=<"<path to file with list of objects>"
set /p file_info=<"<path to file with info>"
set DL=C:\Users\%USERNAME%\Downloads\test.ps1
:: restarting PS script
echo #new > "%DL%"
for %%f in (%obj%) do (
set a=%%f
echo !a!!file_info! >> "!DL!"
)
但是,当我用第一个脚本中的 echo
行替换 echo !a!!file_info! >> "!DL!"
时(以相同的方式改编),PS 脚本不再使用循环内容更新(请注意,我现在使用 PS 用户名以避免延迟扩展的任何问题):
setlocal enabledelayedexpansion
set /p obj=<"<path to file with list of objects>"
set /p file_info=<"<path to file with info>"
set file_dir=<known page directories>
set DL=C:\Users\%USERNAME%\Downloads\test.ps1
:: restarting PS script
echo #new > "%DL%"
for %%f in (%obj%) do (
set a=%%f
echo $url = "http://<domain>.com/!file_dir!/!a!/!file_info!" >> "!DL!"
echo $output = "C:\Users$env:UserName\Downloads\!file_info!" >> "!DL!"
echo (New-Object System.Net.WebClient).DownloadFile($url, $output) >> "!DL!"
)
::alternative that doesn't work either:
for %%f in (%obj%) do (
set a=%%f
call echo $url = "http://<domain>.com/!file_dir!/!a!/!file_info!" >> "!DL!"
call echo $output = "C:\Users$env:UserName\Downloads\!file_info!" >> "!DL!"
call echo (New-Object System.Net.WebClient).DownloadFile($url, $output) >> "!DL!"
)
为什么使用 for
循环将 URL 下载部分添加到 PS 脚本会出现问题?有没有办法避免这些问题?
您的代码的主要问题是您的 echo
ed 右括号过早地关闭了 for
循环括号。在我下面的示例中,我用插入符号 ^
.
转义了那些
示例:
@Echo Off
SetLocal EnableExtensions
Set /P "obj=" 0< "<path to file with list of objects>"
Set /P "file_info=" 0< "<path to file with info>"
Set "file_dir=<known page directories>"
Set "DL=C:\Users\%USERNAME%\Downloads\test.ps1"
( For %%G In (%obj%) Do (
Echo $url = "http://<domain>.com/%file_dir%/%%G/%file_info%"
Echo $output = "C:\Users$Env:UserName\Downloads\%file_info%"
Echo (New-Object System.Net.WebClient^).DownloadFile($url, $output^)
)
) 1> "%DL%"
我想构建一个 powershell 脚本来下载一组特定的文件。该集合会定期更改,因此 for
循环是更新流程的最简洁方法。
每个文件的 URLs 每天都会更新日期和唯一序列号,但我已经有了一个提取该信息并将其保存到文件的过程。我还有一种构建 PS 脚本以从 URL:
下载特定文件的方法set /p file_info=<"<path to file with info>"
set file_dir=<known page directories>
set DL=C:\Users\%USERNAME%\Downloads\test.ps1
echo $url = "http://<domain>.com/%file_dir%/%file_info%" > "%DL%"
echo $output = "C:\Users\%USERNAME%\Downloads\%file_info%" >> "%DL%"
echo (New-Object System.Net.WebClient).DownloadFile($url, $output) >> "%DL%"
我还可以循环添加到 PS 脚本:
setlocal enabledelayedexpansion
set /p obj=<"<path to file with list of objects>"
set /p file_info=<"<path to file with info>"
set DL=C:\Users\%USERNAME%\Downloads\test.ps1
:: restarting PS script
echo #new > "%DL%"
for %%f in (%obj%) do (
set a=%%f
echo !a!!file_info! >> "!DL!"
)
但是,当我用第一个脚本中的 echo
行替换 echo !a!!file_info! >> "!DL!"
时(以相同的方式改编),PS 脚本不再使用循环内容更新(请注意,我现在使用 PS 用户名以避免延迟扩展的任何问题):
setlocal enabledelayedexpansion
set /p obj=<"<path to file with list of objects>"
set /p file_info=<"<path to file with info>"
set file_dir=<known page directories>
set DL=C:\Users\%USERNAME%\Downloads\test.ps1
:: restarting PS script
echo #new > "%DL%"
for %%f in (%obj%) do (
set a=%%f
echo $url = "http://<domain>.com/!file_dir!/!a!/!file_info!" >> "!DL!"
echo $output = "C:\Users$env:UserName\Downloads\!file_info!" >> "!DL!"
echo (New-Object System.Net.WebClient).DownloadFile($url, $output) >> "!DL!"
)
::alternative that doesn't work either:
for %%f in (%obj%) do (
set a=%%f
call echo $url = "http://<domain>.com/!file_dir!/!a!/!file_info!" >> "!DL!"
call echo $output = "C:\Users$env:UserName\Downloads\!file_info!" >> "!DL!"
call echo (New-Object System.Net.WebClient).DownloadFile($url, $output) >> "!DL!"
)
为什么使用 for
循环将 URL 下载部分添加到 PS 脚本会出现问题?有没有办法避免这些问题?
您的代码的主要问题是您的 echo
ed 右括号过早地关闭了 for
循环括号。在我下面的示例中,我用插入符号 ^
.
示例:
@Echo Off
SetLocal EnableExtensions
Set /P "obj=" 0< "<path to file with list of objects>"
Set /P "file_info=" 0< "<path to file with info>"
Set "file_dir=<known page directories>"
Set "DL=C:\Users\%USERNAME%\Downloads\test.ps1"
( For %%G In (%obj%) Do (
Echo $url = "http://<domain>.com/%file_dir%/%%G/%file_info%"
Echo $output = "C:\Users$Env:UserName\Downloads\%file_info%"
Echo (New-Object System.Net.WebClient^).DownloadFile($url, $output^)
)
) 1> "%DL%"