1

I'm trying to get this code to print to a file and it wont, can anyone help? It's a powershell script.

$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'
}

I'm trying to take the data from one file then separate it to then save it to another file. There is more that will be doing but this is my first step.

Edit:

My end goal is to make this script take the list of types from the list a mod creator made to list the vehicles in his dayz mod, and add the proper cfg code to it. This way I don’t have to go through all 50+ vehicles on the list and add their respective part so they can spawn fully. For some reason the creator didn’t supply this file.

This will be the end result of the script:

<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>

for right now I'm testing for it to print to a file. I'll move on to the rest after.

Remp2012
  • 31
  • 3
  • 5
    How do you populate the variable `$Matches` you use in your code? ;-) – Olaf Jun 02 '23 at 07:16
  • 5
    Note also, that `$Matches` is an [automatic variable](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7.3). – vonPryz Jun 02 '23 at 08:26
  • As an aside: Extending arrays in a loop with `+=` is inefficient, because a _new_ array must be created behind the scenes _in every iteration_, given that arrays are of fixed size; a much more efficient approach is to use a `foreach` loop as an _expression_ and let PowerShell itself collect the outputs in an array: `[array] $outputs = foreach (...) { ... }` - see [this answer](https://stackoverflow.com/a/60708579/45375). In case you need to create arrays manually, e.g. to create _multiple_ ones, use an efficiently extensible list type - see [here](https://stackoverflow.com/a/60029146/45375). – mklement0 Jun 02 '23 at 14:18
  • You lost me. I’m very new to powershell and only using it because someone in discord suggested to use it for what I was doing. – Remp2012 Jun 02 '23 at 15:12
  • It looks like it's missing some code. Where did you get this? You may update your question and tell "*the whole story*" and share some sample data and the expected output. This way we may be able to help you further. ;-) – Olaf Jun 02 '23 at 18:14
  • I am more or less starting from here to show it out put then I will edit the output to put the vehicle name and then the corresponding parts in their proper for may for the DayZ file. There is just 50+ cars and every car can have 5-10 attachments. I’m trying to streamline it. – Remp2012 Jun 02 '23 at 21:07
  • I don't know what you're talking about. Please add the needed information to your question - not as comments - along with some sample data and the expected output. – Olaf Jun 02 '23 at 21:30
  • I just added what my end result of the code will be. – Remp2012 Jun 03 '23 at 00:22
  • OK, that looks like XML. I'd recommend not to use *string acrobatics* to tinker XML files. And without a sample input file it's nearly impossible to recommend something meaningful. `¯\_(ツ)_/¯` – Olaf Jun 03 '23 at 01:03
  • That is a sample of what I want to output file to look like. I will be copying what and pasting it in the used file. The file is an xml. I got it to output everything in the original file, not what I wanted but a start. I just need to do what I want at the moment. which is just the cars. – Remp2012 Jun 03 '23 at 01:28

1 Answers1

0

For a starter the two most obvious:

 $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 
        }
Dom
  • 168
  • 8