如何使用if else退出脚本

时间:2012-04-20 07:37:30

标签: if-statement exit wsh jscript

当我使用if语句时,请告诉我如何退出javascript(我在硬盘上运行)。以下代码不起作用:

if (fso.FileExists("C:\\EXT\\file.txt") ) //check if there file in the folder
            {
    log.WriteLine(file_exist_time + " - file exists C:\\EXT\\ "); 
            }
    else 
            {  
            log.WriteLine("There is no file in C:\\EXT\\"); 

                             function exit ()
                                    {
                                throw ('Script Exit');
                                    }

        }

非常感谢!

3 个答案:

答案 0 :(得分:1)

为什么在这里使用函数定义?你甚至没有调用这个功能。你可以这样做:

if (fso.FileExists("C:\\EXT\\file.txt")){ //check if there file in the folder
  log.WriteLine(file_exist_time + " - file exists C:\\EXT\\ ");
}
else{
  log.WriteLine("There is no file in C:\\EXT\\");
  throw('Script Exit');
}

但是,throw单独不会停止执行功能,只会将其移至catch - 语句。详细了解exception handling

答案 1 :(得分:1)

使用WScript.Quit完全退出脚本。

答案 2 :(得分:0)

怎么样:

if (fso.FileExists("C:\\EXT\\file.txt") ) //check if there file in the folder
{
    log.WriteLine(file_exist_time + " - file exists C:\\EXT\\ "); 
}
else 
{  
    log.WriteLine("There is no file in C:\\EXT\\"); 
    return;
    // Or possibly throw("Exit");
}

换句话说,不要将throwreturn放在单独的函数中,特别是如果您不调用该函数。