Saturday, 28 November 2015

Button to Convert a List to Comma Separated String

Something I’ve had to do a surprising amount...

Save the following code as say ListToCSV.BAT and double-click to run. Paste in your list of items, and press carriage return after the last item, and a text document with the comma separated string version appears.

Image: Example of ListToCSV.BAT in action
Image: The output of ListToCSV.BAT
The Code

powershell.exe [System.Array]$Outfile = @(); Foreach( $line in (get-content ListToCSV.bat) ){ If($Line.StartsWith('REM ')){ $Outfile += $Line.replace('REM ','') }else{ $Outfile += ('# ' + $Line) } }; Out-File -filepath ListToCSV.ps1 -InputObject $OutFile -Encoding Default; .\ListToCSV.ps1
REM Remove-Item ListToCSV.PS1 -Force -Confirm:$False
REM FUNCTION ListToCSV {PARAM([Parameter(Mandatory=$True)][System.Array]$InArr)
REM   [String]$OutStr = ""; $InArr | Foreach{ $OutStr += $_.trim() + "," }
REM   $OutStr.Substring(0,$OutStr.Length-1)
REM Write-Host "`nPaste list at the prompt" -ForegroundColor GREEN
REM [String]$ListToCSV = ListToCSV
REM $ListToCSV > "ListToCSV.TXT"
REM Notepad "ListToCSV.TXT"; Sleep 1
REM Remove-Item "ListToCSV.TXT" -Force -Confirm:$False

No comments:

Post a Comment

Note: only a member of this blog may post a comment.