KrisReynolds
KrisReynolds

Reputation: 151

.vbs script gets stuck when run as Scheduled Task

I have a VBS file that was provided to me, and I have made small changes to. The script runs fine when I double click it, however when it's set to run as a scheduled task, the status remains as "Running"; usually the task is completed in a matter of seconds.

Can anyone suggest why this may be?

Thanks

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)

Upvotes: 1

Views: 5714

Answers (2)

StevenC
StevenC

Reputation: 75

I had a similar problem.

My scheduled task was running under a specific user. Using the component services snap in for MMC I needed to give the user 'Launch and activation' permissions. Once this was done, the scheduled task ran correctly.

The line 'CreateObject("Scripting.FileSystemObject")' will require these permissions I believe.

Upvotes: 0

CodeWhisperer
CodeWhisperer

Reputation: 186

wscript.echo in an unattended VBS can cause issues, as the script will be waiting for someone to hit the 'ok' button. Remove them or comment them out.

Upvotes: 1

Related Questions