0

I am struggling and wondered if someone could help me...

I have many folders and each one will have a large xml file in it. I would to recurse through my folders and find my xml file. Once I have this xml I would like to pull some values from the xml and create a folder on a web server with a version subfolder and then copy a jpg file to it from the parent folder. So far I can get a single xml and return all the values of a field but cannot get it to create the folders based on the value. Would be nice if the script ignores if the folder exists. My xml would look something like this...

DATA

  • ASSET

    • PART
      • Version "1"
      • partnumber "ABC123"
      • CAR
        • make "FORD"
        • colour "BLACK.JPG"
      • CAR
        • make "FERRARI"
        • colour "RED.JPG"

so the result should be folder structure like this

ABC123\

   1\
     FORD\
          black.jpg
     FERRARI\
           red.jpg

Apologies for the poor quality of the data in the post, first time posting and I couldn't get it to work

1 Answer 1

1

Here's on possible approach:

$xml = [xml]@'
<Data>
  <Asset>
    <Part version="1" partnumber="ABC123">
      <Car make="Ford" colour="Black.jpg"/>
      <Car make="Ferrari" colour="Red.jpg"/>
    </Part>
  </Asset>
</Data>
'@

$xmlPath = "C:\foo"
$pathRoot = "\\server\share\"
foreach ($part in $xml.Data.Asset.Part)
{
    $dir = $part.partnumber
    mkdir $pathRoot\$dir -WhatIf
    foreach ($car in $part.Car)
    {
        $dir = Join-Path $dir $car.make
        mkdir $pathRoot\$dir -WhatIf
        $colour = $car.colour
        Copy-Item $xmlPath\$dir\$colour $pathRoot\$dir\$colour -WhatIf
    }
}

$xmlPath would point to the directory containing the xml file. I assume that is where you could be copying the jpg files from.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks Keith, can you explain why you put the @' with the xml content after it? I also wanted it to look recursively for xml files. This looks really good so far. Thanks for all the effort :)
That is called a here string and it used to create strings that span multiple lines. Recursively finding xml files is easy using Get-ChildItem e.g. Get-ChildItem <root-path> -recurse *.xml.
thanks for getting back to me, do I need to add this bit to my script $xml = [xml]@' <Data> <Asset> <Part version="1" partnumber="ABC123"> <Car make="Ford" colour="Black.jpg"/> <Car make="Ferrari" colour="Red.jpg"/> </Part> </Asset> </Data> '@
No. That is just a sample of what I think the contents of your XML file look like. You need to read in your actual XML file like so $xml = [xml](Get-Content <path-to-xml-file>).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.