开发者

Windows XP and Up: JavaScript instead of VBScript?

I asked how to check modify t开发者_如何学JAVAimestamps with BAT files and launch a command based on an if statement and Wimmel asked if I could use VBScript instead of Batch Files. I think this is a grand idea. This leads to another question

Can I access the VBScript functionality with JavaScript, while still being compatible Windows XP to Current? (specifically checking file modify timestamp and running a command depending on how recently modified)


Not sure that it is a good idea, but yes, you can use JavaScript (actually, JScript) instead of VBScript. Just use ActiveXObject class instead of CreateObject function that is used in VBScript to create objects.

Here is a code that reads the file modify timestamp using Windows Scripting and JScript:

var o = new ActiveXObject("Scripting.FileSystemObject");
var file = o.GetFile("c:\\temp\\test.js");
WScript.Echo(file.DateLastModified);

For more information, see JScript documentation and Windows Script Host documentation


Even though there are probably easier ways to achieve what you want to do, I had a go at trying the more theoretical part of your question, and apparently all the things we need are there.

Here is what I tried:

test.js:

WshShell = WScript.CreateObject("WScript.Shell");
var result = WshShell.Run("test.vbs", 0, true);
WSH.Echo(result);

test.vbs:

WSH.Echo "test.vbs"
WSH.Quit 5
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜