[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 440
  • Last Modified:

Powershell output multiple files

Can powershell parse a document and save multiple files based of of data within document for instance

<item>
 <value> A</value>
 <value> B </value>
 <value> C</value>
 <value> D</value>

</item>

#and output to 4 different files?
0
benpal2476
Asked:
benpal2476
  • 5
  • 4
1 Solution
 
QlemoC++ DeveloperCommented:
In general - yes. In particular - it depends. Of course you can documents, collect data based on conditions, and write the collected data to different targets. XML files as source are no different from CSV.
0
 
benpal2476Author Commented:
[xml]$xml = (Get-Content "C:\Users\S.xml")

foreach ($value in $xml){

$a = $xml.SelectNodes.$item.$value.Innerxml


1..4 | % { New-Item -Path c:\scripts -Name "$_.txt" -Value $a -ItemType file -force}
    }
   
#this outputs multiple files but the files are blank.
#can someone help me re code this?
0
 
QlemoC++ DeveloperCommented:
Your $a is wrong. $value already contains the XML, so you just want to use
$a = $value.InnerXML

Open in new window

and stuff that into the New-Item cmdlet, or since it is short enough, just use the expression directly:
foreach ($value in [xml] (Get-Content "C:\Users\S.xml")){
  1..4 | % { New-Item -Path c:\scripts -Name "$_.txt" -Value $value.InnerXML -ItemType file -force}
    }

Open in new window

0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
benpal2476Author Commented:
Thanks the fix you posted does pull the data,  but the files have the same data in them, i'm looking to have each look ;like this.
 
file1 "A"
file2 "B"
file3 "C"
 and so on......
0
 
QlemoC++ DeveloperCommented:
I really don't get what you are after. Do just want to have each line in a new file, so you will get (# of value lines) files?
0
 
benpal2476Author Commented:
Example:
<item>
 <value> A</value>
 <value> B </value>
 <value> C</value>
 <value> D</value>

</item>

file1  contains value "A"
file2  contains next value "B"
file3  contains  next  value "C"
until it gets all values in the  xml

so 1..3

[xml]$xml = (Get-Content "C:\Users\S.xml")


foreach ($value in $xml.item){

  % { New-Item -Path c:\fso -Name "$_.txt" -Value $value.innerxml -ItemType file }


    }
# when I use this it shows the files being written but then when i check folder there is 1      #file.
0
 
QlemoC++ DeveloperCommented:
Like this?
$fno = 1
foreach ($value in [xml] (Get-Content "C:\Users\S.xml")){
  New-Item -Path c:\scripts -Name "$fno.txt" -Value $value.InnerXML -ItemType file -force
  if ($fno -lt 3) {$fno++}
}

Open in new window

0
 
benpal2476Author Commented:
I had an additional question to this code

$fno = 1
foreach ($value in [xml] (Get-Content "C:\Users\S.xml")){
  New-Item -Path c:\scripts -Name "$fno.txt" -Value $value.InnerXML -ItemType file -force
  if ($fno -lt 3) {$fno++}
}

# for "-Value" $value.InnerXML lets say I wanted to add <IMPORT_FILE xmlns:val="http://www.w3.org/TR/html4/">  to the beggining  or the header of each .txt as well as </IMPORT_FILE> at the end or footer how would i do that?
0
 
QlemoC++ DeveloperCommented:
You can add it in that command directly. I'll include a new line for pretty-printing reasons, but that's no requirement:
$fno = 1
foreach ($value in [xml] (Get-Content "C:\Users\S.xml")){
  New-Item -Path c:\scripts -Name "$fno.txt" -Value @"
<IMPORT_FILE xmlns:val="http://www.w3.org/TR/html4/">
$($value.InnerXML)
</IMPORT_FILE>
"@  -ItemType file -force
  if ($fno -lt 3) {$fno++}
}

Open in new window

BTW, if accepting a comment, please make sure you accept the one best-fitting. In most cases one of the very last comments is appropriate. It does not matter much, but should other members seek for solutions, they encounter a less-important comment to be marked as solution.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now