none
Task Scheduler runs script to D/L data from web - but no results RRS feed

  • Question

  • I use a script to download minutewise stock data history from Google, which permits me to access (at most) the most recent 15 days of minutewise data. I run it weekly, to avoid mistaken gaps. When I run it manually, it works fine (in about 6 seconds or less). However, after it is supposed to run in Windows Scheduler, it shows the task still running, with no outputs. I've put in code to create a dummy file just to show the program has started, but still nothing appears. This happens whether I run as Administrator or regular User.

    I have Set-ExecutionPolicy set to "Unrestricted", as both user accounts and HKLM. I'm at my wit's end here; nothing on the web seems to help.

    Code follows.

    Option Explicit
    
        Const m_MINUTEWISE_DATA_SUBPATH = "\Money\Invest\Historical Data\Minutewise"
        ' Variables.
        Dim strDefaultPath
        Dim strSymbol
        
            Dim objFSO
            Dim objShell
            Dim objNetwork
            Set objFSO = CreateObject("Scripting.FileSystemObject")
            Set objShell = WScript.CreateObject("WScript.Shell")
    	    Set objNetwork = CreateObject("WScript.Network")
    	
        strDefaultPath = "D:\" & m_MINUTEWISE_DATA_SUBPATH
    
        GetMarketDataFromGoogle "SPY",   60, 15
    
    
    Sub GetMarketDataFromGoogle( _
    	strSymbol, _
    	lngInterval_Sec, _
    	lngNumDays)
        
        ' WAS PRIOR TO 9/11/2017: www.google.com/finance/getprices?q=.INX&x=INDEXSP&i=60&p=20d&f=d,c,v,k,o,h,l&df=cpct&auto=0&ei=Ef6XUYDfCqSTiAKEMg
        ' www.google.com/finance/getprices?q=.INX&x=INDEXSP&i=60&p=15d&f=d,c,v,k,o,h,l&df=cpct&auto=0&ei=Ef6XUYDfCqSTiAKEMg
        '
        ' Output is saved as:
        '      [DATE] UPRO 15d from Google.csv
        
        Dim strURL
        Dim strMsg
        Dim strOutputFolder
        Dim strOutputPath 
        Dim strFileName
        Dim strSubPath 
        Dim objHTTP
        Dim objFile
        Dim strDate 
    	
    	' 1 <= lngNumDays <= 20? 15?
    	If (lngNumDays>15) Then
    		lngnumDays = 15
    	End If
    	' 60 <= lngInterval_Sec <= 36400
    	If (lngInterval_Sec<60) Then
    		lngInterval_Sec = 60
    	End If
    	
    	' MSXML2.XMLHTTP can cause permission problems.
    	Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")
        
        ' Build the URL.
        ' Google will only provide minutewise stock data for the last 15 market
        ' days, at most.
        ' Also, I don't seem to be able to control the order of the 
        ' attributes. Regardless of the URL, they come out:
        '     UDate,Close,Low,High,Open,Volume
        ' where UDate = "a" & UnixDate + 7000000000 
        ' and UnixDate = (local-time seconds since 1/1/1970) + (4to5)*60*60 
        ' Subsequent lines are offset by one minute = 60 in UnixTime.
    	strURL="http://finance.google.com/finance/getprices?q=" _
            & strSymbol _
            & "&i=" & lngInterval_Sec _
            & "&p=" & lngNumDays & "d" _
            & "&f=d,c,h,l,o,v"
        
        ' Retrieve the web data as a string.
        objHTTP.open "GET", strURL, False
    
        objHTTP.send
        
        ' Build the output file name.
        strDate = FormatDateTime(Now(), 1)
        strFileName = strDate _
            & " " & strSymbol _
            & " 15d m'wise from Google.csv"
        
        ' Save the file.
        Set objFile = objFSO.CreateTextFile(strDefaultPath & "\" & strFileName, 2)
        
        objFile.Write objHTTP.ResponseText
        objFile.Close
        
    End Sub
    

    • Moved by Bill_Stewart Wednesday, November 29, 2017 6:46 PM This is not "scripts on demand"
    Sunday, October 15, 2017 2:47 PM

All replies

  • Try running this version in a new scheduled task.  I think it will fix your issue.

    GetMarketDataFromGoogle "SPY",   60, 15
    
    Sub GetMarketDataFromGoogle(strSymbol,lngInterval_Sec,lngNumDays)
       	If lngNumDays > 15 Then lngnumDays = 15
    	If lngInterval_Sec < 60 Then lngInterval_Sec = 60
    
    	Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")
        
    	strURL="http://finance.google.com/finance/getprices?q=" _
            & strSymbol _
            & "&i=" & lngInterval_Sec _
            & "&p=" & lngNumDays & "d" _
            & "&f=d,c,h,l,o,v"
        
        objHTTP.open "GET", strURL, False
        objHTTP.send
    
        strDate = FormatDateTime(Now(), 1)
        strFileName = strDate & " " & strSymbol & " 15d m'wise from Google.csv"
        strDefaultPath = "D:\Money\Invest\Historical Data\Minutewise"" & m_MINUTEWISE_DATA_SUBPATH
    
    	Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objFile = objFSO.CreateTextFile(strDefaultPath & "\" & strFileName, 2)
        
        objFile.Write objHTTP.ResponseText
        objFile.Close
        
    End Sub


    \_(ツ)_/

    Sunday, October 15, 2017 3:19 PM
  • Nope. 

    Weirdly, we both made typos in the same string. My (much-reduced) code had a typo that would produce the path with an extra backslash after D:

    "D:\\Money\Invest\Historical Data\Minutewise"

    while your code created the string extra double-quote mark at the end:

    "D:\Money\Invest\Historical Data\Minutewise""

    Corrected, running your code under Task Scheduler brought up the prompt "How do you want to open this file?", and a list of useless options (Chrome, Internet Explorer, IrfanView...). It's either interpreting the VBS file as a webpage, or it's reacting to the http call (most likely) in a useless way, or it's interpreting the output file request as a web-call.

    Regardless, if I answer the prompt with a choice of IE, it simply defaults to opening the default homepage, with no output file resulting. Manually, both versions of the code work the same... so it's even weirder that your code produced a different, albeit still useless, result.  AFAICT, all you did was move the object assignments for the FSO inside the function call, instead of using it as a global variable.

    • Edited by IAmBroom Sunday, October 15, 2017 4:21 PM
    Sunday, October 15, 2017 4:18 PM
  • In tasks how are you defining this?  What account is it running under?


    \_(ツ)_/

    Sunday, October 15, 2017 4:38 PM
  • Use Admin account

    Run only when user is logged on (for yours)
    Mine: Run whether user is logged on or not, DO store password

    Mine: Run with highest privileges
    (Yours: not checked)

    Triggers: (one minute from when I get ready to test the schedule), starting 10/15/2017

    Actions: Start a program
    Program/script: "D:\Documents\Google Drive\My Code\GetStockData\GetIntradayStockHistoryFromGoogle.vbs"
    Add arguments (optional): [blank]
    Start in (optional): Yours=[blank],
    Mine now="D:\Documents\Google Drive\My Code\GetStockData"

    The quote marks above are literally in the text response boxes.


    • Edited by IAmBroom Sunday, October 15, 2017 4:46 PM
    Sunday, October 15, 2017 4:45 PM
  • You were using "CreateTextFile" with the arguments for "OpenTextFile" .  This should fix it.

    GetMarketDataFromGoogle "SPY",   60, 15
    
    Sub GetMarketDataFromGoogle(strSymbol,lngInterval_Sec,lngNumDays)
       	If lngNumDays > 15 Then lngnumDays = 15
    	If lngInterval_Sec < 60 Then lngInterval_Sec = 60
    
    	Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")
        
    	strURL="http://finance.google.com/finance/getprices?q=" _
            & strSymbol _
            & "&i=" & lngInterval_Sec _
            & "&p=" & lngNumDays & "d" _
            & "&f=d,c,h,l,o,v"
        
        objHTTP.open "GET", strURL, False
        objHTTP.send]
        
        strDate = FormatDateTime(Now(), 1)
        strDefaultPath = "D:\Money\Invest\Historical Data\Minutewise\"
        strFileName = strDefaultPath & strDate & " " & strSymbol & " 15d m'wise from Google.csv"
    
    	Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objFile = objFSO.OpenTextFile(strFileName,8,True)
        
        objFile.Write objHTTP.ResponseText
        objFile.Close
        
    End Sub

    I also ran it as a task and it works as expected.


    \_(ツ)_/


    • Edited by jrv Sunday, October 15, 2017 5:04 PM
    Sunday, October 15, 2017 5:03 PM
  • The program to run has to be CSCRIPT or you can get hangs.

    \_(ツ)_/

    Sunday, October 15, 2017 5:06 PM
  • Thank you. It finally ran on schedule - with one more tweak: The "Start in" parameter for the Program to be run tab in Task Scheduler has to be blank. If I put in the script directory, it won't run.

    I hate Task Scheduler so much. It's so inscrutable. No error messages, and it has its own, very peculiar syntax that varies ever-so-slightly from command windows and other script environments. I don't know why those incorrect parameters WORKED manually, but not with Task Scheduler.

    But thank you for staying with me on this, until we found the magical combination.
    Sunday, October 15, 2017 6:10 PM
  • Which is why you should no longer be using VBScript.

    The TS is not the problem.  It works like almost any scheduler every built. 

    Without error checking in the script you can never get results from TS.  Also the logger needs to be enabled and the results and actions will show on the history page.

    Tasks can be quickly run and defined from a PowerShell prompt.  PS Jobs are even easier and only require a script block to initialize.

    Why don't you join us in the 21st century.


    \_(ツ)_/

    Sunday, October 15, 2017 6:19 PM
  • Thanks for the insults. That's very helpful. 
    Sunday, October 22, 2017 3:58 PM
  • Thanks for the insults. That's very helpful. 

    Only help full to 21st century propel.  20th century people don't have this available.  It is like trying to fly to Cancun in 1898.  No flight s available.

    ;)


    \_(ツ)_/

    Sunday, October 22, 2017 4:48 PM