Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.
Keywords: Powershell Convert CSV XML System.Management.Automation System.Management.Automation.PowerShell Import-Csv Export-Clixml Import-Clixml Export-Csv -Path -encoding "UTF8" Converts PowerShell Convertto-xml cmdlet
;*************************************************************************** ;** Convert CSV to XML ;** ;** Purpose: Converts a CSV file to an XML file using PowerShell convertto-xml cmdlet ;** Inputs: CSV file ;** Outputs: Results in an XML file ;** Reference: ;** REQUIRES WinBatch 2013A or newer ;** ;** Developer: Deana Falk 2013.05.28 ;*************************************************************************** If Version( )< '2013A' Pause('Notice', 'Need 2013A or Newer Version of WinBatch') Exit EndIf strCSVFile = DirScript():"products.csv" ; Input file strXMLFile = DirScript():"products.xml" ; Output file If !FileExist(strCSVFile) Pause('Notice', 'Input CSV file does not exist.') Exit EndIf ; Powershell Code cScript = `$obj = Import-Csv -Path "`:strCSVFile:`";` cScript = cScript: `convertto-xml -NoTypeInformation -InputObject $obj -As 'string' | Out-File -Encoding ascii "`:strXMLFile:`"` BoxOpen("Please Wait","Processing... ") ObjectClrOption("use", "System.Management.Automation,version=1.0.0.0,publicKeyToken=31bf3856ad364e35,culture=neutral") objAutoPs = ObjectClrNew("System.Management.Automation.PowerShell") oPshell = objAutoPs.Create() oScope = ObjectType("BOOL",@TRUE) oPshell.AddScript(cScript,oScope) objAsync = oPshell.BeginInvoke() oPShell.EndInvoke(objAsync) oPshell.Dispose() oPshell=0 BoxShut() If FileExist(strXMLFile) RunWait("Notepad",strXMLFile) Else Display(2,"File Not Created",strXMLFile) EndIf Exit
;///////////////////////////////////////////////////////////////////////////////////////////////////////// ;Winbatch 2013A - Use CLR/Powershell to quickly convert csv to xml ; Just a quick test - no error handling ; NOTE: output can be in several encodings ; ;Stan Littlefield, May 8, 2013 ;///////////////////////////////////////////////////////////////////////////////////////////////////////// If Version( )< '2013A' Pause('Notice', 'Need 2013A or Newer Version of WinBatch') Exit EndIf cFile=Dirscript():"products.csv" If ! FileExist(cFile) Then Terminate(@TRUE,"Cannot Continue",cFile:" not found") cFile1=Dirscript():"products.xml" If FileExist(cFile1) Then FileDelete(cFile1) ;generic Powershell Code (should work with both 2.0 and 3.0) ;encoding can be ASCII, UTF8, UTF7, UTF32, Unicode, BigEndianUnicode, Default, and OEM cScript = '$csv = "':cFile:'";' cScript = cScript:'$xml = "':cFile1:'";' cScript = cScript:'Import-Csv -Path $csv | Export-Clixml -Path $xml -encoding "UTF8";' ;reverse the process Import-Clixml -Path [file] | Export-Csv -Path [file] BoxOpen("Please Wait","Processing... ") ObjectClrOption("use", "System.Management.Automation,version=1.0.0.0,publicKeyToken=31bf3856ad364e35,culture=neutral") objAutoPs = ObjectClrNew("System.Management.Automation.PowerShell") oPshell = objAutoPs.Create() oScope = ObjectType("BOOL",@TRUE) oPshell.AddScript(cScript,oScope) objAsync = oPshell.BeginInvoke() oPShell.EndInvoke(objAsync) oPshell.Dispose() oPshell=0 BoxShut() If FileExist(cFile1) runwait("Notepad",cFile1) Else Display(2,"File Not Created",cFile1) Endif Exit ;//////////////////////////////////////////////////////////////////////////////////////////////////////
Article ID: W17828
Filename: Convert CSV to XML.txt
File Created: 2013:05:28:08:49:38
Last Updated: 2013:05:28:08:49:38