2013-07-17 68 views
1

我有一个提供给我的VBS文件,并且我对其进行了小的更改。当我双击它时脚本运行正常,但是当它设置为按计划任务运行时,状态保持为“正在运行”;通常任务在几秒钟内完成。.vbs脚本在按预定任务运行时卡住了

任何人都可以提出为什么这可能是?

由于在无人看管的VBS

wscript.echo "VBScript Create_TaxiCheck_File" 

Const InputFile = "C:\TaxiCheckLive\TaxiCheck_Data.txt" 
Const OutputFile = "C:\TaxiCheckLive\TaxiCheck_Formatted.txt" 
Const CSVFile = "C:\TaxiCheckLive\ChelmsfordExtract.csv" 
Const ForReading = 1 
Const ForWriting = 2 
Const ForAppending = 8 
Dim fso 
Dim I 
Dim IF1 
Dim OF1 
Dim InputLine 
Dim Outputline 
Dim comma 

Set fso = CreateObject("Scripting.FileSystemObject") 

'check input file exists 
if (fso.fileexists(InputFile)) then 
    wscript.echo "Input file exists: " & InputFile 
    'Write to log file (File exists) 
    Set LogFile = fso.OpenTextFile("LogFile.txt", ForAppending) 
    LogFile.WriteLine DateInfo & Now & " - OK, Input file exists" 
    LogFile.close 
Else 
     'Write to log file (File does not exist) 
    Set LogFile = fso.OpenTextFile("LogFile.txt", ForAppending) 
    LogFile.WriteLine DateInfo & Now & " - Error, input file does not exist, database export has not run!" & VbCrLf 
    LogFile.close 
    wscript.Quit(9) 
end if 

'if exists then delete output file 
if (fso.fileexists(OutputFile)) then 
    wscript.echo "Deleting file: " & OutputFile 
    Set OF1 = fso.GetFile(OutputFile) 
    OF1.Delete 
end if 

'create output file 
wscript.echo "Creating output file: " & OutputFile 
Set OF1 = fso.CreateTextFile(OutputFile, True) 
OF1.Close 

'if exists then delete CSV file 
if (fso.fileexists(CSVFile)) then 
    wscript.echo "Deleting file: " & CSVFile 
    Set OF1 = fso.GetFile(CSVFile) 
    OF1.Delete 
end if 

'create formated output file. 
wscript.echo "Create formated output file." 

Set IF1 = fso.OpenTextFile(InputFile, ForReading) 
Set OF1 = fso.OpenTextFile(OutputFile, ForWriting) 

Outputline = "MODE,VEH_REG_NO_NO_SPACES,VEH_MAKE,VEH_MODEL,VEH_COLOUR,LIC_NUMBER" 
OF1.WriteLine Outputline 
Outputline = "D,*" 
OF1.WriteLine Outputline 
Outputline = "" 

Do While Not IF1.AtEndOfStream 
    InputLine = IF1.ReadLine 

    Outputline = "I," + InputLine 

    OF1.WriteLine Outputline 
    Outputline = "" 

Loop 

'copy output file to CSV file 
fso.CopyFile OutputFile, CSVFile 

'close input and output files 
IF1.Close 
OF1.Close 

'delete input and output files 
Set OF2 = fso.GetFile(InputFile) 
OF2.Delete 
Set OF3 = fso.GetFile(OutputFile) 
OF3.Delete 

'ftp file to firmstep ftp site 
Set WshShell = WScript.CreateObject("WScript.Shell") 
WshShell.Run("%windir%\system32\ftp.exe -s:C:\TaxiCheckLive\ftpcommands.txt") 
wscript.echo "VBScript Create_TaxiCheck_File Ended Successfully" 

'Write to log file (complete) 
Set LogFile = fso.OpenTextFile("LogFile.txt", ForAppending) 
LogFile.WriteLine DateInfo & Now & " - Script completed running" & VbCrLf 
LogFile.close 

wscript.Quit(1) 
+2

通常这意味着资源没有关闭,但是,它看起来像关闭了所有文件和进程......您是否看到LogFile.txt中的“Script completed running”? – Papasmile

+0

我不会在按预定任务运行的脚本中使用'WScript.Echo'。除非出现错误,否则我还建议不要使用非零退出代码退出。你的日志文件说什么?当在LogFile.Close和wscript.Quit(1)之间添加一行'WshShell.LogEvent 4','Task complete。''时,您是否在应用程序事件日志中看到信息事件“Task complete.'? –

回答

1

wscript.echo可能会导致问题,因为该脚本将被等待有人来打“确定”按钮。删除它们或将它们评论出来。

0

我有一个类似的问题。

我的计划任务正在特定用户下运行。使用MMC的组件服务管理单元我需要为用户提供“启动和激活”权限。一旦完成,计划任务就可以正常运行。

行'CreateObject(“Scripting.FileSystemObject”)'将需要我相信这些权限。

+1

哪个组件服务需要更改? –