benpal2476
asked on
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?
<item>
<value> A</value>
<value> B </value>
<value> C</value>
<value> D</value>
</item>
#and output to 4 different files?
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.
ASKER
[xml]$xml = (Get-Content "C:\Users\S.xml")
foreach ($value in $xml){
$a = $xml.SelectNodes.$item.$va lue.Innerx ml
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?
foreach ($value in $xml){
$a = $xml.SelectNodes.$item.$va
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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......
file1 "A"
file2 "B"
file3 "C"
and so on......
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?
ASKER
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.
<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.
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++}
}
ASKER
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?
$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?
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++}
}
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.