Jump to content

Possible AutoIt bug in Windows Startup


Recommended Posts

Hi All,

I have an AutoIt script placed in the windows startup. The script writes a text file onto the disk. The script works fine in the editor as well as when compiled and started via command line/double click. But when I place the script in windows startup, it does not create the text file (both via startup dir and via registry). The script just executes and exits with 0 as exit code - no errors at all. But the same, when I double click, creates the text file!!! Any idea what goes wrong? The following is the part of code that writes the file. I have tried it on multiple machines all yielded the same result too.

Func WriteFile($path, $contents)

$h_file = FileOpen($path, 2)

if $h_file = -1 Then

MakeLog("Error: Failed to open handle for the file " & $path)

Return False

EndIf

FileWriteLine($h_file, $contents)

FileClose($h_file)

if FileExists($path) Then

Return True

Else

Return False

EndIf

EndFunc

Edited by JohnSmith
Link to comment
Share on other sites

Can't see a key part of the code, it's evidently outside the function.

Anyways ... where is the path of $path?

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

The script is placed as C:\ProgramFiles\AFolder\MyScript.exe, so the path is C:\ProgramFiles\AFolder\. A shortcut to the script is placed in windows startup directory as well. when i start windows, it should execute the the script and write the file onto the application directory itself. Unfortunately, it does not create the file - only when launched from startup

The script is called from main autoit script body as follows:

If WriteFile("MyDataFile.txt") = False Then

Exit 1

else

Exit 0

EndIf

Edited by JohnSmith
Link to comment
Share on other sites

It's usually good practice to post all relevant code.

It's hard to make determinations otherwise.

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

It's usually good practice to post all relevant code.

It's hard to make determinations otherwise.

In fact, I left out the rest of the code for brevity. What the whole thing does is this:

The script gets the contents of a listview control from another application. the contents are passed to the WriteLC file. The function saves the content to a text file.

Even, please write a simple script that creates an empty txt file and try placing the script in windows startup folder. it will not create the text file on windows startup...!!!!!!

Edited by JohnSmith
Link to comment
Share on other sites

This works for me from the startup:

Local $sPath = @ScriptDir & '\SomeLog.txt'
Local $sContents = 'Some text for contents'

WriteFile($sPath, $sContents)

Func WriteFile($path, $contents)
    Local $h_file = FileOpen($path, 2)

    If $h_file = -1 Then
        MakeLog("Error: Failed to open handle for the file " & $path)
        Return False
    EndIf

    FileWriteLine($h_file, $contents)

    FileClose($h_file)

    If FileExists($path) Then
        Return True
    Else
        Return False
    EndIf
EndFunc

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

This works for me from the startup:

Local $sPath = @ScriptDir & '\SomeLog.txt'
Local $sContents = 'Some text for contents'

WriteFile($sPath, $sContents)

Func WriteFile($path, $contents)
    Local $h_file = FileOpen($path, 2)

    If $h_file = -1 Then
        MakeLog("Error: Failed to open handle for the file " & $path)
        Return False
    EndIf

    FileWriteLine($h_file, $contents)

    FileClose($h_file)

    If FileExists($path) Then
        Return True
    Else
        Return False
    EndIf
EndFunc

Thank u for the quick reply. Let me try it now..... Have you really tried the above code on your system?

Edited by JohnSmith
Link to comment
Share on other sites

In fact, I left out the rest of the code for brevity. What the whole thing does is this:

The script gets the contents of a listview control from another application. the contents are passed to the WriteLC file. The function saves the content to a text file.

Even, please write a simple script that creates an empty txt file and try placing the script in windows startup folder. it will not create the text file on windows startup...!!!!!!

How certain are you that this other application is running at the time your script starts?

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Good catch JohnOne. I gotta run - catch ya'll later.

No JohnOne it's not like that. I will describe the problem more: I have a .net program that is installed into program files directory. the application path contains an autoit script too. the .net program calls the script. the script reads the list view control of another application and writes the contents into a txt file onto the application path. the .net app reads this txt file. the actual scraping starts only after windows startup, maybe several minutes, hour, etc after the startup process. but the problem is if my .net program is started via windows startup, it kicks off the script, but the script cannot make a txt file. otherwise if the program is started manually via double clicking, it works fine. i have tried on numerous machines, all same result.

so to diagnose the problem, i tried creating a small program in autoit that creates a simple txt file, and placed it in startup... ah.. it does create when started manually but not when started from windows startup. pls try the following code. compile it into exe and try from your startup. does not create the txt file?

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****

#AutoIt3Wrapper_outfile=TestScript.exe

#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

Local $h_file = FileOpen("testfile.txt", 2)

if $h_file = -1 Then

MsgBox(0,"","Failed to create file")

exit 1

EndIf

FileWriteLine($h_file, "test content")

FileClose($h_file)

MsgBox(0,"","File created")

Edited by JohnSmith
Link to comment
Share on other sites

At last I got the issue fixed.

The issue was this: If you do not include @ScriptDir in file path to be created, it will not create the file when script is kicked off from windows startup.

Previously, I just used file name (without full path) assuming that the path will automatically be taken by autoit as the current directory. But AutoIt does this only after the windows session is fully up, but not while startup. When I prefixed the @ScriptDir with the file name, it worked even from startup....

Thank you, ripdad and JohnOne, for helping me diagnose the problem.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...