As you all probably know, currently I’m writing some scripts in Jscript. Those can be runned using Script Host built in Windows. I prefer JScript more than Visual Basic. I hate Visual Basic at all.
So the script, I can just share with you is very simple and just converts one delimited file format to another. In my case it changes tab-separated to semicolon-separated fields.
var sourceDelimiter = '\t',
targetDelimiter = ';';
while (!WScript.StdIn.AtEndOfLine) {
var line = WScript.StdIn.ReadLine();
var fields = line.split(sourceDelimiter);
WScript.Echo(fields.join(targetDelimiter));
}
It’s very simple – isn’t it? I don’t use script parameter for two reasons. It is very hard to pass tab character using windows console. It makes definitely more work than just small change in variables. The second is just to shorten the execution of the script.
You should execute this like any other unix-like script, by passing and capturing standard input and output. In windows command line you can use something like this
cscript //nologo dconv.js < datafile.txt > output.csv
And inside the PowerShell you must use Get-Contents and pass the content using pipe like this:
get-content datafile.txt | cscript //nologo dconv.js > output.csv
By the way, //nologo parameter tells cscript to don't show information lines. We don't need those lines in our output file.
That’s all for now. Have fun with Windows Script Host programming.
No comments:
Post a Comment