【问题标题】:How to do a Wget with powershell and output to a text file如何使用 powershell 执行 Wget 并输出到文本文件
【发布时间】:2014-08-28 12:13:19
【问题描述】:

我正在尝试使用 powershell 执行 WGET 并将输出转到文本文件。

到目前为止,所有尝试都失败了。

PS C:\Program Files (x86)\GnuWin32\bin> For ($i=1; $i -lt 5; $i++) 
 {wget.exe  www.zillabunny.com | out-file C:\temp\wget.txt}

留下一个空的文本文件

和

For ($i=1; $i -lt 5; $i++)  {wget.exe  www.zillabunny.com} > C:\temp\wget.txt 

给我

:术语“>”未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。检查 名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。 在行:1 字符:60 + For ($i=1; $i -lt 5; $i++) {wget.exe www.zillabunny.com} > wget.txt + ~ + CategoryInfo : ObjectNotFound: (>:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

【问题讨论】:

    标签: powershell


    【解决方案1】:

    一种方法是将for 循环包含在脚本块中并一次性输出。例如:

    & {
      for ( $i = 1; $i -le 5; $i++ ) {
        wget.exe -O - www.zillabunny.com
      }
    } | out-file sample.txt
    

    这将运行命令 wget.exe www.zillabunny.com 5 次并将输出写入 sample.txt。

    如果你只想说“做某事x次”,你也可以这样写,省去额外的脚本块:

    1..5 | foreach-object {
      wget.exe -O - www.zillabunny.com
    } | out-file sample.txt
    

    【讨论】:

    • 非常感谢!这也解释了为什么有时我在测试时得到一个文本文件,上面写着“wget.exe www.zillabunny.com”4 次
    • hm 我觉得我说得太早了,第一个代码块只是创建了一个空的 txt 文件。
    • 第二个代码块也只是创建一个空白文本文件?
    • 这取决于wget.exe www.zillabunny.com 的输出。首先将wget.exe www.zillabunny.com 替换为"Hello, world"(带引号的字符串),您会更清楚地看到效果。
    • 同时使用这些代码块,我注意到我在屏幕上得到了输出。
    猜你喜欢
    • 2023-03-07
    • 1970-01-01
    • 2018-12-22
    • 1970-01-01
    • 2020-07-02
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多