ReneGe
asked on
Javascript - output csv file
Hi there,
I need a Javascript that will be called from a batch file and output the content of a csv file; but only the requested columns defined by an argument; it the order defined in the argument.
I need to define the output columns defined as an argument in the command line because the needed columns will change.
Examples:
"dataToRead.csv" CONTENT:
serial,content,url,date,nb ,city,wher e
12,"wine, cheese",http://www.website.com/posting.do?page=138&d=50&fet=2,2016-11-20,6545565,Montréal,sky
COMMAND LINE:
cscript exportcsv.js "dataToRead.csv" "C:\Data\dataOutput.csv" "1,2,6"
EXPECTED OUTPUT to "C:\Data\dataOutput.csv":
"12","wine, cheese","Montréal"
COMMAND LINE:
cscript exportcsv.js "dataToRead.csv" "C:\Data\dataOutput.csv" "1,2,6,7"
EXPECTED OUTPUT to "C:\Data\dataOutput.csv":
"12","wine, cheese","Montréal",sky
COMMAND LINE:
cscript exportcsv.js "dataToRead.csv" "C:\Data\dataOutput.csv" "4,3"
EXPECTED OUTPUT to "C:\Data\dataOutput.csv":
"2016-11-20","http://www.website.com/posting.do?page=138&d=50&fet=2"
Thanks for your help,
Rene
I need a Javascript that will be called from a batch file and output the content of a csv file; but only the requested columns defined by an argument; it the order defined in the argument.
I need to define the output columns defined as an argument in the command line because the needed columns will change.
Examples:
"dataToRead.csv" CONTENT:
serial,content,url,date,nb
12,"wine, cheese",http://www.website.com/posting.do?page=138&d=50&fet=2,2016-11-20,6545565,Montréal,sky
COMMAND LINE:
cscript exportcsv.js "dataToRead.csv" "C:\Data\dataOutput.csv" "1,2,6"
EXPECTED OUTPUT to "C:\Data\dataOutput.csv":
"12","wine, cheese","Montréal"
COMMAND LINE:
cscript exportcsv.js "dataToRead.csv" "C:\Data\dataOutput.csv" "1,2,6,7"
EXPECTED OUTPUT to "C:\Data\dataOutput.csv":
"12","wine, cheese","Montréal",sky
COMMAND LINE:
cscript exportcsv.js "dataToRead.csv" "C:\Data\dataOutput.csv" "4,3"
EXPECTED OUTPUT to "C:\Data\dataOutput.csv":
"2016-11-20","http://www.website.com/posting.do?page=138&d=50&fet=2"
Thanks for your help,
Rene
why javascript ? what is your OS ?
ASKER
Because i need to run it from a Windows batch file.
and your OS is ?
ASKER
Windows 8.1 Pro
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 for your solution.
By right clicking on it then clicking on "Run with PowerShell" it works.
1) I'm unable to run it from a batch file:
Command line: PowerShell.exe -Command OutputCSV.ps1
Error message: OutputCSV.ps1 : The term 'OutputCSV.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program.
2) Is there a way to exclude data rows when a field is empty? For example, in 'serial','content','city', if 'city' is empty, do not output this data row.
Thanks and cheers,
Rene
:)
By right clicking on it then clicking on "Run with PowerShell" it works.
1) I'm unable to run it from a batch file:
Command line: PowerShell.exe -Command OutputCSV.ps1
Error message: OutputCSV.ps1 : The term 'OutputCSV.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program.
2) Is there a way to exclude data rows when a field is empty? For example, in 'serial','content','city',
Thanks and cheers,
Rene
:)
in your batch, set path before running the command else it don't find the ps1
for your second question, no
ASKER
Its working :)
Is there a way I can output it in my batch file windows instrad to a file? This way, I'll clean up its content in a FOR /F command.
Is there a way I can output it in my batch file windows instrad to a file? This way, I'll clean up its content in a FOR /F command.
ASKER
The following works and is output to my shell window. But i need it to be formatted as csv.
Import-Csv 'C:\BatchFiles\E.E\OutputC SV.csv' | Select $columns
Import-Csv 'C:\BatchFiles\E.E\OutputC
aha you want your batch modify itself ?
forget it.
just use del command if needed...
you can do lot more on this with powershell, be sure to create any ne question on this zone
forget it.
just use del command if needed...
you can do lot more on this with powershell, be sure to create any ne question on this zone
you omit the second part of the original commande, the export-csv part
ASKER
No, that not what I meant
Forget about the batch file. How can I output "Import-Csv 'C:\BatchFiles\E.E\OutputC SV.csv' | Select $columns" in the shell window and in csv format?
Cheers mate :)
Forget about the batch file. How can I output "Import-Csv 'C:\BatchFiles\E.E\OutputC
Cheers mate :)
ASKER
Import-Csv 'C:\BatchFiles\E.E\OutputC SV.csv' | Select $columns | Export-Csv
I get the following error message:
Export-Csv : You must specify either the -Path or -LiteralPath parameters, but not both.
I get the following error message:
Export-Csv : You must specify either the -Path or -LiteralPath parameters, but not both.
Check my original post, scroll to the rigth, you miss this part
ASKER
But your original post exports to a file, not the shell window!
Am I missing something?
Am I missing something?
ASKER
Here is the final Powershell command line that works, by also expluding the rows where "city" is empty.
Import-Csv 'dataToRead.csv' | Select $columns | Where-Object {$_.city -ne ""} | Export-Csv -Encoding utf8 –NoTypeInformation -Path 'result.csv'
Thanks you sooo much for your help :)
Import-Csv 'dataToRead.csv' | Select $columns | Where-Object {$_.city -ne ""} | Export-Csv -Encoding utf8 –NoTypeInformation -Path 'result.csv'
Thanks you sooo much for your help :)
great!