【问题标题】:Foreach loop is not working in PowerShellForeach 循环在 PowerShell 中不起作用
【发布时间】:2020-05-27 11:35:07
【问题描述】:

我正在寻找有关 Powershell 中 foreach 循环的帮助。所以目标是根据用户请求的数量在 PowerShell 中创建新文件,因此如果用户请求创建 4 个相同的文件,它应该在请求的文件夹中创建四个文件。我似乎无法弄清楚为什么我的 foreach 循环没有制作多个文件。它每次只制作一个文件。我附上了部分代码。

$FileName = Read-Host "Enter a file name"
$FilePath = Read-Host "Enter a file path"
$FileNumber = Read-Host "How many files need to be created?"
$num = 1

if ($FileName -gt 0) { 

    foreach($num in 4) {
    New-Item -Path $FilePath -Name $filepath
    #Write-Host $number $FileNumber
    }

}

【问题讨论】:

  • [1] 您在 if 测试中将 $FileName0 进行比较。这种比较不太可能像您预期的那样起作用。 [grin] ///// [2] 您的New-Item 调用使用$FilePath 作为位置和文件名。 ///// [3] 你打算如何处理精细的名称冲突?你只提供了一个名字......但你似乎有编写四个文件的编码指令。 ///// [4] 你的foreach 是......根本不可能工作。 $Num in 4 到底是什么意思? [grin] ///// [5] 您询问要制作的文件数,但您从不使用该值。
  • 我在尝试排除故障时不小心输入了foreach($num in 4) {
  • 哈!我 - 当然 - 从来没有做过任何形式的“miztaak”。 [grin] 很高兴看到您有两个巧妙的解决方案!

标签: powershell loops for-loop scripting


【解决方案1】:

$FileName 是字符串,所以虽然 If($FileName -gt 0) 会返回 true,但这不是一个好习惯。

数字 4 中也没有任何内容。In 的右侧应该是一个对象集合,它在脚本之前的任何地方都没有创建。然后你有一个错字,你为 name 和 path 参数指定了 $FileName。无论如何,我认为您正在寻找这样的东西:

$FileName = Read-Host "Enter a file name"
$FilePath = Read-Host "Enter a file path"
$FileNumber = Read-Host "How many files need to be created?"
$num = 1

if ($FileName) { 
$num..$FileNumber |
    ForEach-Object{
        $NewFile = $FileName + "_" + $_
        New-Item -Path $FilePath -Name $NewFile -ItemType File
    }
}

if 逻辑适用于您的目的。然后,我将向 ForEach-Object cmdlet 的管道发送一系列数字。注意:这与您使用的 ForEach 关键字不同。无论如何,将数字连接起来,在本例中为 $_ 与先前输入的基本文件名,然后继续创建项目。

希望有帮助,请告诉我...

【讨论】:

    【解决方案2】:

    详细说明 Lee_Dailey 所说的内容,我编辑了您的代码。见我在代码内嵌的 cmets 中的注释。

    $FileName = Read-Host "Enter a file name"
    $FilePath = Read-Host "Enter a file path"
    $FileNumber = Read-Host "How many files need to be created?"
    # this next line shouldn't be needed here so I commented it out
    # $num = 1
    
    # You don't need "-gt 0".  Just use "if ($FileName)". which basically means:
    # "if there is a value for $Filename then execute the block"
    if ($FileName) {         
        # "foreach" really isn't the right approach here
        # use a for loop instead.  This one means:
        # start counting at $i (which is 1), if $i is less than or
        # equal to $FileNumber then execute the code block
        # and then increment $i at the end, making $i = 2
        # on the second trip through the loop
        for ($i=1; $i -le $FileNumber; $i++){
            # if you are making multiple files you can't give them all the
            # same name.  So you need to add a number at the end of each file
            # you create.  In this case $i
            $thisFileName = "$FileName" + "$i"
            New-Item -Path $FilePath -Name $thisFileName
            #Write-Host $number $FileNumber
        }
    
    }
    

    【讨论】:

    • 谢谢,这对我的解释很有帮助。我实际上使用了 if 语句,因为我想创建一个错误输出。再次感谢您和 Lee_Dailey。
    猜你喜欢
    • 1970-01-01
    • 2014-04-28
    • 1970-01-01
    • 1970-01-01
    • 2017-05-28
    • 1970-01-01
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多