deeeeerek

help with fileopen loop

7 posts in this topic

I'm fairly new to autoit and programming. i'm having some trouble w/ loops.  

trying to create a script that reads an existing txt file that has a url but i only want it to launch if a certain txt file exists. i need a 10min pause between filereads and for it to continuously loop. here's what i have so far. sorry!! i'm new :/

Func launch()
$file = FileOpen("c:\url.txt", 0)
    $FileContent = FileRead($file)
Run(@ProgramFilesDir&"\Google\Chrome\Application\chrome.exe "&$FileContent&"")
EndFunc 

if FileExists("c:\go.txt") Then
   launch()
EndIf 
 

Share this post


Link to post
Share on other sites



Trap your Launch function in an infinite while loop with a 10 minute (or however long you want) sleep at the end.

While (True)
    launch()
    Sleep(10 * 60000)   ; 10 minutes
WEnd

Func launch()
    Local $sFileContent = ""
    Local $hFile = FileOpen("c:\url.txt", 0)
    ; If the file opens, it will fail if the file doesn't exist so no need to check if it exists first
    If ($hFile) Then
        
        FileClose($hFile)   ; Be sure to close the file when you're done
    EndIf
EndFunc   ;==>launch

 

1 person likes this

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Maybe good:
 

Opt("TrayAutoPause", 0)
Global $sFileURL = "C:\URL.txt"
Global $sWebApp = @ProgramFilesDir & '\Google\Chrome\Application\chrome.exe'

While 1
    If FileExists($sFileURL) Then
        Launch()
        Sleep(1000 * 60 * 10) ; 10 minutes
    Else
        Sleep(1000) ; 1s
    EndIf
WEnd

Func Launch()
    Local $sURL = FileRead($sFileURL)
    If StringStripWS($sURL, 8) <> "" Then
        If FileExists($sWebApp) Then
            ShellExecute($sWebApp, $sURL)
        Else
            ShellExecute($sURL, Null, Null, "open")
        EndIf
    EndIf
EndFunc   ;==>Launch

 

Or:

Opt("TrayAutoPause", 0)
Global $sFileURL = "C:\URL.txt"
Global $sURL, $sWebApp = @ProgramFilesDir & '\Google\Chrome\Application\chrome.exe'

While 1
    Sleep(Launch())
WEnd

Func Launch()
    If FileExists($sFileURL) Then
        $sURL = FileRead($sFileURL)
        If StringStripWS($sURL, 8) <> "" Then
            If FileExists($sWebApp) Then
                ShellExecute($sWebApp, $sURL)
            Else
                ShellExecute($sURL, Null, Null, "open")
            EndIf
            Return 1000 * 60 * 10 ; 10 minutes
        EndIf
    EndIf
    Return 1000 ; 1 second
EndFunc   ;==>Launch

 

Edited by VIP
(:

Regards,
 

Share this post


Link to post
Share on other sites
3 hours ago, InunoTaishou said:

Trap your Launch function in an infinite while loop with a 10 minute (or however long you want) sleep at the end.

While (True)
    launch()
    Sleep(10 * 60000)   ; 10 minutes
WEnd

Func launch()
    Local $sFileContent = ""
    Local $hFile = FileOpen("c:\url.txt", 0)
    ; If the file opens, it will fail if the file doesn't exist so no need to check if it exists first
    If ($hFile) Then
        
        FileClose($hFile)   ; Be sure to close the file when you're done
    EndIf
EndFunc   ;==>launch

 

awesome! out of curiousity how come the $sFileContent is left undefined? ty so much!

Share this post


Link to post
Share on other sites
2 hours ago, VIP said:

Maybe good:
 

Opt("TrayAutoPause", 0)
Global $sFileURL = "C:\URL.txt"
Global $sWebApp = @ProgramFilesDir & '\Google\Chrome\Application\chrome.exe'
If Not FileExists($sWebApp) Then $sWebApp = @WindowsDir & "\explorer.exe" ;Exit (MsgBox(48, "Error", "Google Chrome is not Exists!"))

While 1
    If FileExists($sFileURL) Then
        Launch()
        Sleep(10 * 60000) ; 10 minutes
    Else
        Sleep(1000) ; 1s
    EndIf
WEnd

Func Launch()
    Local $sURL = FileRead($sFileURL)
    If StringStripWS($sURL, 8) <> "" Then ShellExecute($sWebApp, $sURL)
EndFunc   ;==>Launch

 

Or:

Opt("TrayAutoPause", 0)
Global $sFileURL = "C:\URL.txt"
Global $sWebApp = @ProgramFilesDir & '\Google\Chrome\Application\chrome.exe'
If Not FileExists($sWebApp) Then $sWebApp = @WindowsDir & "\explorer.exe" ;Exit (MsgBox(48, "Error", "Google Chrome is not Exists!"))

While 1
    If FileExists($sFileURL) Then Sleep(Launch())
WEnd

Func Launch()
    Local $sURL = FileRead($sFileURL)
    If StringStripWS($sURL, 8) <> "" Then
        ShellExecute($sWebApp, $sURL)
        Return 1000 * 600 ; 10 minutes
    EndIf
    Return 1000 ; 1 second
EndFunc   ;==>Launch

 

first worked perfect!!! it randomly launched a ton of windows after idle. not sure if it was just a glitch but ran it several times and it worked like a charm. i will try the 2nd one in a bit. much appreciated! 

Share this post


Link to post
Share on other sites

I left it undefined because I didn't finish the code. If $hFile is a valid handle to a file (anything other than 0) it will enter the if statement, from there you would read the file and, ideally, check that the string it reads is a valid url. (Hopefully, not using magic numbers :))

There's a function called

_WinAPI_UrlIs

That will test that the string given is a valid url. If it is valid then you can just use ShellExecute(Valid Url) and that will open up the url in the system's preferred internet browser.

Something I could have easily done for you but it seemed like a simple script and it's better that you test/figure out how to do something than I just give it to you. (I don't always do that but you did specifically state you were new to programming)

Share this post


Link to post
Share on other sites

i def appreciate it. i had a good time figuring it out :D

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now