seadoggie01 Posted January 22, 2020 Share Posted January 22, 2020 (edited) Is there a way to check which mode a file handle was opened with? I want to verify that the handle passed to my function was created with $FO_OVERWRITE so I can edit files correctly. I'm writing an IniDelete replacement function that I'm trying to check this in... expandcollapse popup; #FUNCTION# ==================================================================================================================== ; Name ..........: _Ini_Delete ; Description ...: Deletes a section or key-value pair. ; Syntax ........: _Ini_Delete($vFile, $sSection[, $sKey = ""]) ; Parameters ....: $vFile - the full file path and name or a file handle. ; $sSection - the section to use or delete. ; $sKey - [optional] the key to delete. Default is Default. ; Return values .: Success - True ; Failure - False and sets @error: ; |1- Can't read $vFile ; |2- $sSection doesn't exist ; |3- $sKey doesn't exist ; |4- Can't get handle to $vFile ; |5- Can't write to $vFile ; Author ........: Seadoggie01 ; Modified ......: January 22, 2020 ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _Ini_Delete($vFile, $sSection, $sKey = Default) If $sKey = Default Then $sKey = "" Local $hFile ; If we were given a file path + name If IsString($vFile) Then ; Get a file handle $hFile = FileOpen($vFile, $FO_OVERWRITE) If $hFile = -1 Then Return SetError(4, 0, False) Else #ToDo: Check to see if handle is opened in overwrite mode $hFile = $vFile EndIf ; Read the file Local $sContents = FileRead($hFile) If @error Then Return SetError(1, @error, False) ; Get the section's text Local $sSectionText = __Ini_SectionText($sContents, $sSection) If @error Then Return SetError(2, @extended, False) Local $sNewSectionText ; If the key is empty If $sKey = "" Then ; We'll replace the section with nothing $sNewSectionText = "" Else ; Replace the key-value pair(s?) $sNewSectionText = StringRegExpReplace($sSectionText, "(?m)^(\Q" & $sKey & "\E=.*)$\s+", "") If @error Then Return SetError(3, @error, False) EndIf ; Replace the old section with the new one $sContents = StringReplace($sContents, $sSectionText, $sNewSectionText) ; Write the contents of the file If Not FileWrite($hFile, $sContents) Then Return SetError(5, 0, False) ; If it's a string Then close the handle If IsString($vFile) Then FileClose($hFile) Return True EndFunc I just realized I've left the handle to the file open if it errors... I'll have to fix that, but this should give you an idea of what I'm trying to do. Edited January 22, 2020 by seadoggie01 All my code provided is Public Domain... but it may not work. Use it, change it, break it, whatever you want. Spoiler My Humble Contributions:Personal Function Documentation - A personal HelpFile for your functionsAcro.au3 UDF - Automating Acrobat ProToDo Finder - Find #ToDo: lines in your scriptsUI-SimpleWrappers UDF - Use UI Automation more Simply-erKeePass UDF - Automate KeePass, a password managerInputBoxes - Simple Input boxes for various variable types Link to comment Share on other sites More sharing options...
Nine Posted January 22, 2020 Share Posted January 22, 2020 How can you read a file you just overwrited before ? I don't get it. Anyway, you could close it and reopen it with the right mode ? Or if the file has write mode and is empty ? But really read the file before you overwrite it seadoggie01 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Subz Posted January 22, 2020 Share Posted January 22, 2020 @SmOke_N wrote a UDF a while back which I use from time to time when trying to read sections larger than 32kb, it also includes _IniDeleteEx function which does something similar, it may give you ideas on how to handle FileOpen. seadoggie01 1 Link to comment Share on other sites More sharing options...
seadoggie01 Posted January 22, 2020 Author Share Posted January 22, 2020 Sorry, I think I don't understand FileOpen well enough, sorry. I'm taking another look. All my code provided is Public Domain... but it may not work. Use it, change it, break it, whatever you want. Spoiler My Humble Contributions:Personal Function Documentation - A personal HelpFile for your functionsAcro.au3 UDF - Automating Acrobat ProToDo Finder - Find #ToDo: lines in your scriptsUI-SimpleWrappers UDF - Use UI Automation more Simply-erKeePass UDF - Automate KeePass, a password managerInputBoxes - Simple Input boxes for various variable types 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