Jump to content

Compiled Script doesn't work


Recommended Posts

Hi,

If I run the script out of SciTE (Go) the following code does work just fine, but when I try running the compiled exe, I get the message (MsgBox) that the programm couldn't create the .txt file. I checked the explorer while running the script, it looks like the "error" message is shown before the file appears in the explorer, but it definitly can be created. I also noticed that the code following the MsgBox gets executed even though I thought the MsgBox (return) exits the "main" function.

#include <FileConstants.au3>
#include <File.au3>

Local Const $permissionsFilePath = "C:\temp\perms.txt"
Local Const $rootKey = "HKLM\...."
Local Const $permissions = "[ 1 ]"

main()

Func main()
    If Not CreateFile($permissionsFilePath, $rootKey & " " & $permissions & @CRLF) Then Return MsgBox(1, "Error", "The File " & $permissionsFilePath & " couldn't be created.")

    ...
EndFunc

Func CreateFile($sFilePath, $sString)
    Local $fReturn = True
    DeleteFile($sFilePath)
    $fReturn = FileWrite($sFilePath, $sString) = 1
    Return $fReturn
EndFunc

Func DeleteFile($sFilePath)
    If FileExists($sFilePath) = 1 Then
        FileDelete($sFilePath)
    EndIf
EndFunc

Additional Information:

User: Admin

Autoit Version: 3.4.1

The rest of the script writes Regkeys + permissions to "perms.txt", which then I use to set permissions with RegIni.exe.

thanks for you help.

Edited by Kenavr
Link to comment
Share on other sites

@1: I removed the following code and replaced it with "...", I thought it's not important for the question, but I could add it, if you think it helps.

@2: I installed 3.4.1 today

@3 Tried it, didn't help.

I also tried it on another computer and the issue ist the same. It seems like a timing problem, because I can see the file inside the explorer after the MsgBox gets shown. 

Link to comment
Share on other sites

#include <FileConstants.au3>
#include <File.au3>

Local Const $permissionsFilePath = "C:\temp\perms.txt"
Local Const $rootKey = "HKLM\...."
Local Const $permissions = "[ 1 ]"

Main()

Func Main()
    If Not CreateFile($permissionsFilePath, $rootKey & " " & $permissions & @CRLF) Then
        Return MsgBox(1, "Error", "The File " & $permissionsFilePath & " couldn't be created.")
    EndIf
EndFunc   ;==>main

Func CreateFile($sFilePath, $sString)
    Local $fReturn; Don't need to make = True because a variable is always = True
    FileDelete($sFilePath) ; is ovb it delete the file if exist, you can also use FileOpen with 2 like parameter instead
    $fReturn = FileWrite($sFilePath, $sString) ; True if success, False if not, check Return Value
    Return $fReturn ; better to check error
EndFunc   ;==>CreateFile

;~ Func DeleteFile($sFilePath)
;~     If FileExists($sFilePath) = 1 Then
;~         FileDelete($sFilePath)
;~     EndIf
;~ EndFunc

Edited by MyEarth
Link to comment
Share on other sites

  • Moderators

MyEarth,

JohnOne's comments are quite justified - those 2 statements are indeed complete nonsense. :)

And I also note that you have not provided any code to help either, so criticising others seems a little OTT. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I need to apologize, the missing code seems to be relevant. I added two MsgBoxes to Output the @error code and the return value. The odd thing was that the MsgBoxes appeared two times before the actual error MsgBox showed up. Then I removed everything else and it worked, after adding one line after another the problem seems to be: 

RunWait(@Comspec & " /c " & "REGINI.EXE "& $permissionsFilePath,"",@SW_HIDE)

After adding this line the second MsgBox Outputs appeared, this time the return value was false. Following is the entire code.

#RequireAdmin
#include <FileConstants.au3>
#include <File.au3>

Local Const $permissionsFilePath = "C:\temp\perms.txt"
Local Const $rootKey = "HKEY_LOCAL_MACHINE\..."
Local Const $permissions = "[ 1 ]"



Func main()
    If Not CreateFile($permissionsFilePath, $rootKey & " " & $permissions & @CRLF) Then Return MsgBox(1, "Error", "The File " & $permissionsFilePath & " couldn't be created.")

    Local $fileOpen = FileOpen($permissionsFilePath, $FO_APPEND)
    If $fileOpen = -1 Then
        MsgBox(1, "", "Error while reading - "& $permissionsFilePath &".")
        Return False
    EndIf

    writeKeyToFile($fileOpen, $rootKey)
    RunWait(@Comspec & " /c " & "REGINI.EXE "&$permissionsFilePath,"",@SW_HIDE)
    DeleteFile($permissionsFilePath)
EndFunc

Func CreateFile($sFilePath, $sString)
    Local $fReturn = True
    DeleteFile($sFilePath)
    $fReturn = FileWrite($sFilePath, $sString) = 1
    ConsoleWrite(@error)
    MsgBox(1, "", @error)
    MsgBox(1, "", $fReturn)
    Return $fReturn
EndFunc

Func DeleteFile($sFilePath)
    If FileExists($sFilePath) = 1 Then
        FileDelete($sFilePath)
    EndIf
EndFunc


Func writeKeyToFile($fileopen, $currentKey)
    Dim $sub, $i
    while 1
      $i += 1
      $sub=RegEnumKey($currentKey,$i)
      If @error <> 0 then ExitLoop
      FileWrite($fileOpen, $currentKey & "\" & $sub & " " & $permissions & @CRLF)
      writeKeyToFile($fileOpen, $currentKey & "\" & $sub)
    Wend
EndFunc

main()

To me The "RunWait" Statement and the "CreateFile" function seem unrelated, I don't understand how this line brings me back to "CreateFile" function.

Edited by Kenavr
Link to comment
Share on other sites

You need to understand the flow of your code.

CreateFile is called first

In CreateFile, DeleteFile is called

Thene Native FileWrite, ConsoleWrite and 2 MsgBox before main determines whether to call MsgBox with your error.

CreateFile is returning 0 so FileWrite in it is failing.

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

I believe I understand the flow of the code. If every function runs serial/synchronized there is no reason for it to be 0/False. 

CreateFile is called -  the File gets deleted and the following FileWrite(...) should create the file and write the string in it. What's a little surprising to me is that the documentation states

The file must be opened in write mode or the FileWrite() command will fail.

But the same page has the following function in it

Func FileCreate($sFilePath, $sString)
    Local $fReturn = True ; Create a variable to store a boolean value.
    If FileExists($sFilePath) = 0 Then $fReturn = FileWrite($sFilePath, $sString) = 1 ; If FileWrite returned 1 this will be True otherwise False.
    Return $fReturn ; Return the boolean value of either True of False, depending on the return value of FileWrite.
EndFunc   ;==>FileCreate

http://www.autoitscript.com/autoit3/docs/functions/FileWrite.htm

That's actually the source for my function, but nowhere is the file opened before the FileCreate function calls FileWrite. Or does FileExists open the File in Writemode?

Furthermore, there is no reason why the FileCreate function ist called more than one time, especially not at

RunWait(@Comspec & " /c " & "REGINI.EXE "&$permissionsFilePath,"",@SW_HIDE)
Edited by Kenavr
Link to comment
Share on other sites

Does the directory exist?  If not, FileWrite will fail.  This will work for most file names...you'll have to add in special chars as needed:

#include <File.au3>
Local Const $permissionsFilePath = "C:\temp\perms.txt"
Local Const $rootKey = "HKLM\...."
Local Const $permissions = "[ 1 ]"

$bReturn = FileCreate($permissionsFilePath, $rootKey & " " & $permissions & @CRLF)
MsgBox(1, "Return", "Return=" & $bReturn)

Func FileCreate($sFileAndPath, $sString)
    Local $bReturn = False
    $sPath = StringRegExpReplace($sFileAndPath,"(\\\w+\.\w+)","")
    If Not FileExists($sPath) Then
        DirCreate($sPath)
    EndIf
    If _FileCreate($sFileAndPath) Then
        $bReturn = FileWrite($sFileAndPath, $sString)
    EndIf
    Return $bReturn
EndFunc   ;==>FileCreate
Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
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...