Reputation: 18237
I am having a file where Xcopy commands were written. Now i need to move them to xml file format so that i can execute with them in Msbuild.
Following are my file contents
xcopy File1 Destinationfolder1
Xcopy File2 DestinationFolder2
Xcopy File3 DestinationFolder1
I would like to make it as xml file as follows
<Xcopy include="File1">
<Destination> DestinationFolder1 </Destination>
</Xcopy>
<Xcopy Include="File2">
<Destination> DestinationFolder2 </Destination>
</Xcopy>
and so on..
Is there any powershell script or some other easy way to achieve this?
Upvotes: 0
Views: 3167
Reputation: 13483
How about this:
clear
$res = Get-Content c:\PST\1.txt
$xml = [xml] "<root namespace=`"namespace`"></root>"
foreach($line in $res)
{
# If words are separated by more than one whitespace
$line=$line -replace '\s+', ' '
$values = $line.Split(" ")
$insert = [xml] [string]::Format("<Xcopy include=`"{0}`"> <Destination>{1}</Destination> </Xcopy>", $values[1], $values[2])
$importNode = $xml.ImportNode($insert.DocumentElement, $true)
$xml.root.AppendChild($importNode) |Out-Null
}
$xml.Save("c:\PST\result.xml")
It is working, but I'm pretty sure PowerShell gurus can make it much simpler
Upvotes: 1
Reputation: 2722
The following cmd batch file gives exactly the output you are looking for:
@echo off
rem Syntax: conv inputfile resultfile
set input=%1
set result=%2
copy nul %result%
for /F "tokens=2,3*" %%G in (%input%) do (
echo ^<Xcopy include="%%G"^> >> %result%
echo ^<Destination^> %%H ^</Destination^> >> %result%
echo ^</Xcopy^> >> %result%
echo. >> %result%
)
Upvotes: 2