Kenavr Posted April 10, 2014 Share Posted April 10, 2014 (edited) 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 April 10, 2014 by Kenavr Link to comment Share on other sites More sharing options...
JohnOne Posted April 10, 2014 Share Posted April 10, 2014 1. There is no code following the MsgBox 2. You upgraded OS since 3.4.1? 3. Try with #reqireAdmin? 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 More sharing options...
Kenavr Posted April 10, 2014 Author Share Posted April 10, 2014 @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 More sharing options...
JohnOne Posted April 10, 2014 Share Posted April 10, 2014 try $fReturn = FileWrite($sFilePath, $sString) 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 More sharing options...
Kenavr Posted April 10, 2014 Author Share Posted April 10, 2014 No succes, still the same error ;( Link to comment Share on other sites More sharing options...
JohnOne Posted April 10, 2014 Share Posted April 10, 2014 You need to get the value of @error after the filewrite function, which I can only assume actually writes data to the file. 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 More sharing options...
MyEarth Posted April 10, 2014 Share Posted April 10, 2014 (edited) #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 April 10, 2014 by MyEarth Link to comment Share on other sites More sharing options...
JohnOne Posted April 10, 2014 Share Posted April 10, 2014 Don't need to make = True because a variable is always = Trueis ovb it delete the file if existNonsense and nonsense. 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 More sharing options...
MyEarth Posted April 10, 2014 Share Posted April 10, 2014 You method to answer is nonsense, in every thread i see your name. Arguments instead of increasing the number of post and maybe write some code to help someone "novice" with autoit Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted April 10, 2014 Moderators Share Posted April 10, 2014 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 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Kenavr Posted April 10, 2014 Author Share Posted April 10, 2014 (edited) 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. expandcollapse popup#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 April 10, 2014 by Kenavr Link to comment Share on other sites More sharing options...
JohnOne Posted April 10, 2014 Share Posted April 10, 2014 You need to understand the flow of your code.CreateFile is called firstIn CreateFile, DeleteFile is calledThene 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 More sharing options...
Kenavr Posted April 10, 2014 Author Share Posted April 10, 2014 (edited) 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 April 10, 2014 by Kenavr Link to comment Share on other sites More sharing options...
jdelaney Posted April 10, 2014 Share Posted April 10, 2014 (edited) 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 April 10, 2014 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 More sharing options...
Kenavr Posted April 10, 2014 Author Share Posted April 10, 2014 The given path is the one I am using, but I guess I need to use _FileCreate to be on the save side. I will try it "tomorrow" and come back to you. thanks Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now