• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 444
  • 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
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

 
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
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Improved Protection from Phishing Attacks

WatchGuard DNSWatch reduces malware infections by detecting and blocking malicious DNS requests, improving your ability to protect employees from phishing attacks. Learn more about our newest service included in Total Security Suite today!

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