Xerix

fileopen with hidden file

16 posts in this topic

#1 ·  Posted (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 by Xerix

Share this post


Link to post
Share on other sites



No idea ?

Thank you

Share this post


Link to post
Share on other sites

#3 ·  Posted (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 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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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 :)

Share this post


Link to post
Share on other sites

Ok, thank you

Share this post


Link to post
Share on other sites

It's still a little strange that Fileopen fail on a hidden file in mode 2.

i will use the solution with FileSetAttrib

Share this post


Link to post
Share on other sites

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 to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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

1 person likes this

Share this post


Link to post
Share on other sites

#12 ·  Posted (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 by Taz77

Share this post


Link to post
Share on other sites

That happens because AutoIt expects that the file Attribute is  normal (FILE_ATTRIBUTE_NORMAL)

 

Saludos

 

Share this post


Link to post
Share on other sites

#15 ·  Posted (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?:lol:

Edited by Taz77
1 person likes this

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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