Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How do I get jscript code processing to run?

var a, ad, f, gf, so;
var so = new ActiveXObject("Scripting.FileSystemObject");
var gf = so.GetFolder(so.GetParentFolderName(WScript.ScriptFullName)); 


// StreamTypeEnum
var adTypeBinary = 1; // binary
var adTypeText = 2; // text

// 読み込み方法
// StreamReadEnum
var adReadAll = -1; // all lines
var adReadLine = -2; // per line

// 書き込み方法
// StreamWriteEnum
var adWriteChar = 0; // paragraph
var adWriteLine = 1; // not paragraph

// how to save 
// SaveOptionsEnum 
var adSaveCreateNotExist = 1; // make new one
var adSaveCreateOverWrite = 2; // overwrite

//var cwd = fso.GetParentFolderName(WScript.ScriptFullName);
var cwd = so.GetParentFolderName(WScript.ScriptFullName);
//var wscPath = fso.BuildPath(cwd, "vbsWrapperformoodle.wsc");
var wscPath = so.BuildPath(cwd, "vbsWrapperformoodle.wsc");

var vbs = GetObject("script:" + wscPath);
var InputBox = vbs.method_InputBox;


//user enter
var strRepl;
var strWith;

//using InputBox with one WSC file
strRepl = vbs.InputBox("Type letters before change.", "Inputbox");
if (!(strRepl)) {
	WScript.Quit;
}

strWith = InputBox("Type letters after change", "Inputbox");
if (!(strWith)) {
	WScript.Quit;
}

WScript.Echo(gf);
WScript.Echo(gf.Files.Count);
for (fileobj in gf.Files)
{
	
        // read
		var sr = new ActiveXObject("ADODB.Stream");
		sr.Type = adTypeText;
		sr.charset = "utf-8"; //Shift_JIS
		sr.Open();
	    sr.LoadFromFile(fileobj);
		var temp = sr.ReadText(adReadAll);
		sr.Close();

		// write
		var sw = new ActiveXObject("ADODB.Stream");
		sw.Type = adTypeText;
		sw.charset = "Shift_JIS"; ///utf-8
		sw.Open();
		sw.WriteText(
			temp.replace(new RegExp(strRepl, "g"), strWith)
			,
			adWriteLine
		);
	    sw.SaveToFile(fileobj, adSaveCreateOverWrite);
		sw.Close();
	

}
gf = "";
so = "";
WScript.Echo("Done.");


The for statement "for (fileobj in gf.Files){}"does not pass the process.
"WScript.Echo(gf. Files.Count);" displays the number of files in the folder and only the "Done" message is displayed.
Even if I try to change the code "for (fileobj in gf.Files)" like "for (gf.Files.Item in gf.Files)", it doesn't work. Can someone please tell me?

What I have tried:

I tried "gf.Files.Item" instead of "filename".
JScript doesn't have a "foreach" statement, so I don't think it's wrong in the form for(a in b){}.
But no success.
I'm in trouble because I don't have anyone around to ask.
Please help me.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900