为什么我的PowerShell脚本没有将字符串打印到文件?

t98cgbkg  于 2023-06-29  发布在  Shell
关注(0)|答案(1)|浏览(162)

我试图让这段代码打印到一个文件,它不会,有人可以帮助吗?这是一个PowerShell脚本。

$itemsofCars =@()
$cars = @()

$typefile = Get-Content "C:\Newfolder\cars_not_formatted.txt"

foreach ($line in $typefile)
{
    if($Matches.Count -gt 1)
    {
        $thisItem = $Matches[1]
        $Matches.Clear()
        
        if(
           $thisItem.Contains("_Wheel") -or
           $thisItem.Contains("_Doors_") -or
           $thisItem.Contains("_trunk_") -or
           $thisItem.Contains("_hood_") -or
           $thisItem.Contains("_coDriver_") -or
           $thisItem.Contains("_driver")
          )
        {
            $itemsofCars += $thisItem
        }
        else
        {
            $cars += $thisItem
        }               
    }
}
#$itemsofCars
#$cars
foreach ($car in $cars)
{
    $car | Out-file 'C:\Newfolder\test.txt'
}

我试图从一个文件中提取数据,然后将其分离,然后将其保存到另一个文件中。还有更多的事情要做,但这是我的第一步。
编辑:
我的最终目标是让这个脚本从一个mod创建者在他的dayz mod中列出车辆的列表中获取类型列表,并添加适当的cfg代码。这样我就不必通过所有50+车辆的名单上,并添加各自的一部分,使他们可以完全产卵。由于某种原因,创建者没有提供此文件。
这将是脚本的最终结果:

<type name="Audi_RS6">
        <attachments chance="1">
            <item name="Audi_RS6_Doors_cargo1"
                  chance="1"/>
        </attachments>
        <attachments chance="1">
            <item name="Audi_RS6_Wheel"
                  chance="1"/>
        </attachments>
        <attachments chance="1">
            <item name="Audi_RS6_Wheel"
                  chance="1"/>
        </attachments>
        <attachments chance="1">
            <item name="Audi_RS6_Wheel"
                  chance="1"/>
        </attachments>
        <attachments chance="1">
            <item name="Audi_RS6_Wheel"
                  chance="1"/>
        </attachments>
        <attachments chance="1">
            <item name="Audi_RS6_Wheel"
                  chance="1"/>
        </attachments>
        <attachments chance="1">
            <item name="Audi_RS6_Doors_cargo2"
                  chance="1"/>
        </attachments>
        <attachments chance="1">
            <item name="Audi_RS6_Doors_coDriver"
                  chance="1"/>
        </attachments>
        <attachments chance="1">
            <item name="Audi_RS6_Doors_Driver"
                  chance="1"/>
        </attachments>
        <attachments chance="1">
            <item name="Audi_RS6_Doors_Hood"
                  chance="1"/>
        </attachments>
        <attachments chance="1">
            <item name="SparkPlug"
                  chance="1"/>
        </attachments>
        <attachments chance="1">
            <item name="CarBattery"
                  chance="1"/>
        </attachments>
        <attachments chance="1">
            <item name="CarRadiator"
                  chance="1"/>
        </attachments>
        <attachments chance="1">
            <item name="HeadlightH7"
                  chance="1"/>
        </attachments>
        <attachments chance="1">
            <item name="HeadlightH7"
                  chance="1"/>
        </attachments>
</type>

现在我正在测试打印到文件。剩下的我等会儿再看。

qhhrdooz

qhhrdooz1#

对于初学者来说,两个最明显的:

$itemsofCars =@()
        $cars = @()
        
        $typefile = Get-Content "C:\Newfolder\cars_not_formatted.txt"
        
        foreach ($line in $typefile)
        {
            if($Matches.Count -gt 1) 
            { <#$matches has not been declared or asigned, therefore this if branch will never be executed#>
                $thisItem = $Matches[1]
                $Matches.Clear()
                write-host "Text u never get to see"
                if(
                   $thisItem.Contains("_Wheel") -or
                   $thisItem.Contains("_Doors_") -or
                   $thisItem.Contains("_trunk_") -or
                   $thisItem.Contains("_hood_") -or
                   $thisItem.Contains("_coDriver_") -or
                   $thisItem.Contains("_driver")
                  )
                {
                    $itemsofCars += $thisItem
                }
                else
                {
                    $cars += $thisItem
                }               
            }
        }
        #$itemsofCars
        #$cars
        foreach ($car in $cars)
        {
            <#$car | Out-file 'C:\Newfolder\test.txt' this will override the whole file, use this instead to add a line to file: #>
              Add-Content 'C:\Newfolder\test.txt'  $car 
        }

相关问题