0

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)

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

That can actually be the point. I'll try this as soon as user is online. However it should be "\\" not "\".
Visual Basic strings are not like Java strings, for example the only char that needs escaping is " and you escape it as "" in the string
I'm not sure about VB, yet, according to MSDN there have to be double backslashes (e.g. "C:\\myfolder\\") in file paths, not single ones (WSH throws an exception when you try to use paths with single backslasges ).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.