Code:
var regs = {'E':/[e]/g};//in real code here are actual regular expressions
var fso = new ActiveXObject("Scripting.FileSystemObject");
var objShell = new ActiveXObject("Shell.Application");
var lib, new_file;
var cur_path = WScript.ScriptFullName.substring(0, WScript.ScriptFullName.length - WScript.ScriptName.length);
in_path = cur_path+'input';
out_path = cur_path+'output/';
lib = objShell.NameSpace(in_path);
items = lib.Items()
n=0;
for (i=0;i<items.Count;i++)
{
fitem = items.Item(i);
cur_file = fso.OpenTextFile(in_path + '/' + fitem.Name, 1);
new_file = fso.CreateTextFile(out_path + fitem.Name, true);
while (cur_file.AtEndOfStream == false) {
var line = cur_file.ReadLine();
for (key in regs) {
line = line.replace(regs[key], key );
}
new_file.WriteLine(line);
}
cur_file.Close();
new_file.Close();
n++;
}
WScript.Echo("Total files found/converted:" + i + "/" + n);
The folder with script contains input
and output
folders with some samples in input
folder.
I need it to work in WinXP.
Script works perfectly in Win7, yet the user claims that in Windows XP the exception "file not found" (or something similar) is thrown and says "in line 22". The 22nd line in the script is the empty one between "cur_file=..." and "new_file=...".
Can anyone tell me what's wrong with this? Is there any difference between OpenAsTextStream
and OpenTextFile
methods for XP (except for caller)?
My guess is that it has something messed up with CreateTextFile
or OpenTextFile
method like another proper method name in WinXP or different pathing in WinXP or something else. Unfortunately I have no WinXP and can't test it properly.
UPD: Just noticed I have a missing semicolon in line 15. Can this be a reason for such behaviour? (I doubt it)
Try replacing the slashes with backslashes, for example:
cur_file = fso.OpenTextFile(in_path + '\' + fitem.Name, 1);
If it works, Win7 probably normalizes file paths automatically.