我需要为WinXP编写一些脚本,以支持Big Financial Corp这里的一些分析师。我需要决定哪种类型的Windows脚本最适合我的需求。
对我来说,我的需求似乎非常简单:
(当前危机是需要运行一个给定的应用程序,传递几个日期参数)。
一旦当前危机解决,将会有更多类似的请求。
对我来说,我的需求似乎非常简单:
- 在WinXP Pro SP2(版本2002)上运行
- 不要求用户安装任何东西(因此PowerShell是不行的。同样,Perl、Python和其他在Stack Overflow上这类问题的常见建议也不行)
- 使用非编译语言编写(这样用户将有机会在未来修改它们)
- 具有相当完整的语言特性(尤其是日期/时间操作函数。我还想拥有现代概念,如子例程、递归等)
- 能够启动和控制其他程序(在命令行中)
- VBScript
- WScript
- JScript
(当前危机是需要运行一个给定的应用程序,传递几个日期参数)。
一旦当前危机解决,将会有更多类似的请求。
编辑
我的当前技能包括Perl、JavaScript和Java,因此我最喜欢使用类似这些语言的东西编辑
好吧。我会尝试用JScript编写一个WSH文件。一旦事情在这里稍微平静下来,我就会告诉你它的效果如何(并找出如何接受答案)。编辑
最终一切都解决了。感谢大家快速的回应。这是我给我的用户的内容:<job id="main">
<script language="JScript">
// ----- Do not change anything above this line ----- //
var template = "c:\\path\\to\\program -##PARAM## --start ##date1## --end ##date2## --output F:\\path\\to\\whereever\\ouput_file_##date1##.mdb";
// Handle dates
// first, figure out what they should be
dt = new Date();
var date1 = stringFromDate(dt, 1);
var date2 = stringFromDate(dt, 2);
// then insert them into the template
template = template.replace(new RegExp("##date1##", "g"), date1);
template = template.replace(new RegExp("##date2##", "g"), date2);
// This application needs to run twice, the only difference is a single parameter
var params = ["r", "i"]; // here are the params.
// set up a shell object to run the command for us
var shellObj = new ActiveXObject("WScript.Shell");
// now run the program once for each of the above parameters
for ( var index in params )
{
var runString = template; // set up the string we'll pass to the wondows console
runString = runString.replace(new RegExp("##PARAM##", "g"), params[index]); // replace the parameter
WScript.Echo(runString);
var execObj = shellObj.Exec( runString );
while( execObj.Status == 0 )
{
WScript.Sleep(1000); //time in milliseconds
}
WScript.Echo("Finished with status: " + execObj.Status + "\n");
}
// ----- supporting functions ----- //
// Given a date, return a string of that date in the format yyyy-m-d
// If given an offset, it first adjusts the date by that number of days
function stringFromDate(dateObj, offsetDays){
if (typeof(offsetDays) == "undefined"){
offsetDays = 0;
}
dateObj.setDate( dateObj.getDate() + offsetDays );
var s = dateObj.getYear() + "-"; //Year
s += (dateObj.getMonth() + 1) + "-"; //Month (zero-based)
s += dateObj.getDate(); //Day
return(s);
}
// ----- Do not change anything below this line ----- //
</script>
</job>
显然它可以更好...但它完成了工作,而且对我的用户来说易于理解和自行扩展。