I am writing a script right now which makes use of the File System Object to read a text file and later delete it. The part of the code in which this occurs I have written as a function. I have tested this function, along with some other functions which it depends on, and it works fine. But in the final product it is just one part of a script with 16 different functions/subroutines. After incorporating it into the final product, this file fails to work. Or, at least Windows script host tells me that it doesn't.
I have tested using Echo statements to track down the issue, since the line that WSH references should have executed properly, based on the fact that code after the line in question works when I run the script. I get an Echo when I echo after the function call (I have written a simple function to make the script wait before calling more functions, and the echo statement is after that, so the function definitely completes execution before the error occurs). I have also tried inserting an echo statement into the beginning of the next function to be called (literally directly after I instantiate the second function), and then I don't get an echo.
Here is the function in question. The line the error references is the one with Set objRECID.
Function PullRecords
Set objWSH = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
strIP = "192.168.1.185"
strOutFileCMD = ".output OUT" & strIP & ".txt"
strOutFile = "C:\Users\" & GetLoggedOnUsername & "\Documents\SQLite3\" & "OUT" & strIP & ".txt" 'Output of each element grab
strCommand = "FROM ZCDataField WHERE RECORD_ID="
strRECIDPath = "C:\Users\" & GetLoggedOnUsername & "\Documents\SQLite3\" & "RECID" & strIP & ".txt" 'Output of GetRECID
strFinalCSVPath = "C:\Users\" & GetLoggedOnUsername & "\Documents\SQLite3\" & strIP & ".csv"
strSQLite3Path = "C:\Users\" & GetLoggedOnUsername & "\Documents\SQLite3\"
strProcName = "SQLite3.exe"
'Element ID's (for reference)
'intpt_item = "275426222"
'intpt_description = "275466434"
'intpt_custitem = "275466431"
'intpt_qty = "275466713"
'intpt_name = "275466710"
'intpt_date = "275435183"
'intpt_unixmil = "275578832"
'Options for text files
intForReading = 1
intForWriting = 2
intForAppending = 8
boolOverwriteTrue = True
boolOverwriteFalse = False
Set objCSV = objFSO.CreateTextFile(strFinalCSVPath,boolOverwriteTrue) 'Creates final CSV to be written to/
Set objRECID = objFSO.OpenTextFile(strRECIDPath,intForReading)
'Cycles through records
Do Until objRECID.AtEndOfStream
strRECID = objRECID.ReadLine
Set objELID = objFSO.OpenTextFile(strSQLite3Path & "Element IDs.txt",intForReading) 'Need this here so that script restarts at beginning of file each time.
'Cycles through elements
Do Until objELID.AtEndOfStream
Set objBAT = objFSO.CreateTextFile(strSQLite3Path & strIP & dblUnixT & ".bat",boolOverwriteTrue)
objBAT.Write ("cd /D C:\Users\" & GetLoggedOnUsername & "\Documents\SQLite3")
objBAT.WriteLine
objBAT.Write ("sqlite3 " & strIP & ".db < " & strIP & ".txt")
objBAT.Close
Set objBAT = Nothing
Set objCMDS = objFSO.CreateTextFile(strSQLite3Path & strIP & ".txt",boolOverwriteTrue)
objCMDS.Write (".mode tabs")
objCMDS.WriteLine
objCMDS.Write (strOutFileCMD)
objCMDS.WriteLine
strELID = objELID.ReadLine 'Gets ELEMENT_ID
objCMDS.Write ("SELECT VALUE_TXT,VALUE_NUM " & strCommand & strRECID & " AND ELEMENT_ID=" & strELID & ";") 'Gets element
objCMDS.WriteLine
objCMDS.Write (".exit") 'Exits SQLite3
objCMDS.Close
Set objCMDS = Nothing
objWSH.Run(strSQLite3Path & strIP & dblUnixT & ".bat")
'Makes script wait until BAT is done.
intRunning = 1
Do Until intRunning = 0
intRunning = IsRunning(strProcName)
Loop
Wscript.Sleep(500)
Set objOut = objFSO.OpenTextFile(strOutFile)
strOut = objOut.ReadLine
objOut.Close
Set objOut = Nothing
objCSV.Write strOut & ","
Loop
'Make string of line and then delete 1 character on right to get rid of ,. Then replace line.
objCSV.Write Chr(8)
objCSV.WriteLine
objELID.Close
Set objELID = Nothing
Loop
objCSV.Close
objRECID.Close
Set objCSV = Nothing
Set objRECID = Nothing
objFSO.DeleteFile strRECIDPath
'objFSO.DeleteFile strOutFile
InvertText(strFinalCSVPath)
PullRecords = strFinalCSVPath
Call FuncSubEndToken
End Function
In case it is useful, here is the FuncSubEndToken function that I call.
Function FuncSubEndToken
intFuncSubEndToken = 1
End Function
And here is how I am using it when I call the PullRecords Function.
Call PullRecords
'FuncSubEndTokenWait
Do Until intFuncSubEndToken = 1
Loop
intFuncSubEndToken = 0
Any help would be greatly appreciated.
Edit: I have tried using On Error Resume Next and I just get wscript hanging after the parts that I posted. It doesn't even enter the next function.
Edit 2: I have tried commenting out the line that deletes the file that is needed for the PullRecords function, even though that line is executed after the function is done with the file. It now is apparent that is calling this function twice and another function twice (and then it errors out part way through that). I have looked and both of these functions are only ever called once (and they are not in loops, so that is definitely not the issue). I do call a function and pass it the output of both of these functions, in the order that they seem to be running. But I am not here calling the functions themselves. But perhaps wscript thinks that I am?
Edit 3: I have deleted the function that uses the return of the other two functions (well, commented it out), and I still get the same issue.
So, I have now figured out the issue. It would seem that I was somewhat misinformed on how functions work when trying to get their output. I thought that you called a function and then the function name was the output. But, it would seem that you do not need to use a call statement to execute a function. So, while trying to use the output of my functions I have been inadvertently calling them.