Xerix Posted March 8, 2015 Share Posted March 8, 2015 (edited) Hello, I try to open a hidden file in write mode but this doesn't seem to work. @error = -1 $open = FileOpen("d:\test.txt",2) MsgBox(4096,"",$open) It works in read mode (0) or Write mode (append to end of file) (1) but not in Write mode (erase previous contents) (2) Is it a normal work or a bug ? Thank you Eric Edited March 8, 2015 by Xerix Link to comment Share on other sites More sharing options...
Xerix Posted March 10, 2015 Author Share Posted March 10, 2015 No idea ? Thank you Link to comment Share on other sites More sharing options...
jdelaney Posted March 10, 2015 Share Posted March 10, 2015 (edited) Works when I make un-hidden first: $file = "c:\automation\temp\test\test.txt" FileSetAttrib($file,"-H") $h = FileOpen($file,2) If FileWrite($h,"something new") Then ConsoleWrite("worked fine" & @CRLF) Else ConsoleWrite("failed" & @CRLF) EndIf FileClose($h) FileSetAttrib($file,"+H") test is a hidden folder, and test.txt is a hidden file. Edited March 10, 2015 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...
Xerix Posted March 11, 2015 Author Share Posted March 11, 2015 Ok, thanks jdelaney. But if i do that on several files, all of them are not necessarily Hidden. And here, they will become hidden even if they were not hidden. Link to comment Share on other sites More sharing options...
mpower Posted March 11, 2015 Share Posted March 11, 2015 You don't have to hide files that were not hidden before.... just check the hidden attribute and if it was hidden set it to unhidden and write, then set back to hidden, otherwise don't make it hidden...simple Link to comment Share on other sites More sharing options...
Xerix Posted March 11, 2015 Author Share Posted March 11, 2015 Ok, thank you Link to comment Share on other sites More sharing options...
Xerix Posted March 11, 2015 Author Share Posted March 11, 2015 It's still a little strange that Fileopen fail on a hidden file in mode 2. i will use the solution with FileSetAttrib Link to comment Share on other sites More sharing options...
funkey Posted March 11, 2015 Share Posted March 11, 2015 Using the Windows api works as expected. #include <WinAPI.au3> Global $hFile = _WinAPI_CreateFile("test.txt", 3, 4) MsgBox(4096, "", $hFile) Global $sText = 'abcdefghijklmnopqrstuvwxyz2' Global $tBuffer = DllStructCreate("byte[" & StringLen($sText) & "]") DllStructSetData($tBuffer, 1, $sText) Global $nBytes _WinAPI_WriteFile($hFile, DllStructGetPtr($tBuffer), StringLen($sText), $nBytes) _WinAPI_CloseHandle($hFile)So maybe it's a bug. Programming today is a race between software engineers striving tobuild bigger and better idiot-proof programs, and the Universetrying to produce bigger and better idiots.So far, the Universe is winning. Link to comment Share on other sites More sharing options...
Xerix Posted May 13, 2015 Author Share Posted May 13, 2015 Sorry for the delai.For me it's a bug, but i'm not an Autoit expert.Could it be useful to add this to the bugtrack or not ?Thank you. Link to comment Share on other sites More sharing options...
Taz77 Posted June 13, 2016 Share Posted June 13, 2016 (edited) I have the same problem. ; Fails on hidden files Local $hFileOpen = FileOpen($sFile, $FO_READ + $FO_OVERWRITE) Have you created a ticket? Edited June 13, 2016 by Taz77 Dropbox - Sign up and get an extra 500MB Link to comment Share on other sites More sharing options...
Danyfirex Posted June 14, 2016 Share Posted June 14, 2016 29 minutes ago, Taz77 said: I have the same problem. ; Fails on hidden files Local $hFileOpen = FileOpen($sFile, $FO_READ + $FO_OVERWRITE) Have you created ticket? It's a old topic. I think is not a bug. But you can use Windows api. Autoit FileOpen does not mix GENERIC_READ with GENERIC_WRITE. Saludos Synapsee 1 Danysys.com AutoIt... UDFs: VirusTotal API 2.0 UDF - libZPlay UDF - Apps: Guitar Tab Tester - VirusTotal Hash Checker Examples: Text-to-Speech ISpVoice Interface - Get installed applications - Enable/Disable Network connection PrintHookProc - WINTRUST - Mute Microphone Level - Get Connected NetWorks - Create NetWork Connection ShortCut Link to comment Share on other sites More sharing options...
Taz77 Posted June 14, 2016 Share Posted June 14, 2016 (edited) Yes, I know that I can use Windows api, thanks But that has nothing to do with GENERIC_READ with GENERIC_WRITE. FileOpen with "$FO_OVERWRITE" Fails on hidden files, try this example: #include <File.au3> Example() Func Example() Local $sFile= _TempFile(@TempDir & '\', 'MyHiddenFile_', '.txt') _WriteFile($sFile, 'Write to file without hidden Attribute.') _WriteFile($sFile, 'Write another text to file without hidden Attribute.') FileSetAttrib($sFile, "+H") _WriteFile($sFile, 'Write to file WITH hidden Attribute.') FileDelete($sFile) EndFunc Func _WriteFile($sFilePath, $sText) Local $hFileOpen = FileOpen($sFilePath, $FO_OVERWRITE) If $hFileOpen = -1 Then ConsoleWrite('FileOpen fails for: "' & $sText & '"' & @CRLF) Return False EndIf If FileWrite($hFileOpen, $sText) And FileClose($hFileOpen) Then ConsoleWrite('"' & $sText & '" successfully written.' & @CRLF) Else ConsoleWrite('error while writing the content: "' & $sText & '"' & @CRLF) EndIf EndFunc Output: "Write to file without hidden Attribute." successfully written. "Write another text to file without hidden Attribute." successfully written. FileOpen fails for: "Write to file WITH hidden Attribute." Edited June 14, 2016 by Taz77 Dropbox - Sign up and get an extra 500MB Link to comment Share on other sites More sharing options...
Synapsee Posted June 14, 2016 Share Posted June 14, 2016 (edited) @Taz77, nice sample ^^ https://www.autoitscript.com/autoit3/docs/functions/FileOpen.htm > Remarks > A file may fail to open due to access rights or attributes. u can always "setattrib > FileOpen FileClose > setattrib" or use Windows api Edited June 14, 2016 by Synapsee Link to comment Share on other sites More sharing options...
Danyfirex Posted June 14, 2016 Share Posted June 14, 2016 That happens because AutoIt expects that the file Attribute is normal (FILE_ATTRIBUTE_NORMAL) Saludos Danysys.com AutoIt... UDFs: VirusTotal API 2.0 UDF - libZPlay UDF - Apps: Guitar Tab Tester - VirusTotal Hash Checker Examples: Text-to-Speech ISpVoice Interface - Get installed applications - Enable/Disable Network connection PrintHookProc - WINTRUST - Mute Microphone Level - Get Connected NetWorks - Create NetWork Connection ShortCut Link to comment Share on other sites More sharing options...
Taz77 Posted June 14, 2016 Share Posted June 14, 2016 (edited) thanks 1 hour ago, Synapsee said: https://www.autoitscript.com/autoit3/docs/functions/FileOpen.htm > Remarks > A file may fail to open due to access rights or attributes. True, I have not seen. But it also has nothing to do with the set file atribute. Run the example 2 times: #include <FileConstants.au3> Example() Func Example() Local $sFile = @TempDir & '\MyHiddenFile.txt' If Not FileExists($sFile) Then _WriteFile($sFile, 'Hidden File created. Run the script again.', $FO_OVERWRITE) FileSetAttrib($sFile, "+H") Else _WriteFile($sFile, 'Write to file WITH hidden Attribute and mode $FO_OVERWRITE.', $FO_OVERWRITE) _WriteFile($sFile, 'Write to file WITH hidden Attribute and mode $FO_APPEND.', $FO_APPEND) FileDelete($sFile) EndIf EndFunc Func _WriteFile($sFilePath, $sText, $iMode) Local $hFileOpen = FileOpen($sFilePath, $iMode) If $hFileOpen = -1 Then ConsoleWrite('FileOpen fails for: "' & $sText & '"' & @CRLF) Return False EndIf If FileWrite($hFileOpen, $sText) And FileClose($hFileOpen) Then ConsoleWrite('"' & $sText & '" successfully written.' & @CRLF) Else ConsoleWrite('error while writing the content: "' & $sText & '"' & @CRLF) EndIf EndFunc first run: "Hidden File created. Run the script again." successfully written. second run: FileOpen fails for: "Write to file WITH hidden Attribute and mode $FO_OVERWRITE." "Write to file WITH hidden Attribute and mode $FO_APPEND." successfully written. 44 minutes ago, Danyfirex said: That happens because AutoIt expects that the file Attribute is normal (FILE_ATTRIBUTE_NORMAL) Saludos OK, but why it works with $FO_APPEND but not with $FO_OVERWRITE? Both methods write to the same file.. Is it a bug or a feature? Edited June 14, 2016 by Taz77 Synapsee 1 Dropbox - Sign up and get an extra 500MB Link to comment Share on other sites More sharing options...
Danyfirex Posted June 14, 2016 Share Posted June 14, 2016 Hello. Probably because internally AutoIt does this: ;~ $FO_OVERWRITE Here file does not exist CreateFileW ( "MyHiddenFile.txt", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ) ;~ $FO_OVERWRITE here file does exist (here fails) CreateFileW ( "MyHiddenFile.txt", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ) ;~ $FO_APPEND;here work CreateFileW ( "MyHiddenFile.txt", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ) For me is not a bug. but maybe a dev can answer it. I think is intentionally developed just like that. Saludos Danysys.com AutoIt... UDFs: VirusTotal API 2.0 UDF - libZPlay UDF - Apps: Guitar Tab Tester - VirusTotal Hash Checker Examples: Text-to-Speech ISpVoice Interface - Get installed applications - Enable/Disable Network connection PrintHookProc - WINTRUST - Mute Microphone Level - Get Connected NetWorks - Create NetWork Connection ShortCut 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