Sign in to follow this  
Followers 0
sness

Can't Open File

11 posts in this topic

Hello everyone.

I keep getting the "Can't open file" message. Funny thing, the filemove operation right before it works fine. I thought the FileOpen command is supposed to create the file. Any ideas why I would be having this problem?? It's probably easy, but I can't seem to figure out why....

Thanks for any pointers you may have to offer this green horn.

Sue

CODE:

$Appfile = $App & ".txt"

If FileExists($Appfile) Then

If $RNorOW = "RN" Then

$details = FileGetTime($Appfile)

$newfilename = string($details[0]) & string($details[1]) & string($details[2]) & string($details[3]) & string($details[4]) & string($details[5])

$newfilename = $app & $newfilename & ".txt"

filemove($appfile, $newfilename,1)

Else

$newfilename = $app & "old.txt"

Filemove($appfile, $newfilename,1)

EndIf

EndIf

;====================================================================Here

$file = FileOpen($AppFile, 1)

; Check if file opened for reading OK =======================ERROR HERE ??

If $file = -1 Then

MsgBox(0, "Error", "Unable to open file. " & $appfile)

Exit

EndIf

END OF CODE

Share this post


Link to post
Share on other sites



Is it just me or does the logic not make sense?

You have:

$Appfile = $App & ".txt"

If FileExists($Appfile) Then

...

filemove($appfile, $newfilename,1)

Else

...

filemove($appfile, $newfilename,1)

EndIf

How can it move a file that doesn't exist?

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Is it just me or does the logic not make sense?

You have:

$Appfile = $App & ".txt"

If FileExists($Appfile) Then

...

filemove($appfile, $newfilename,1)

Else

...

filemove($appfile, $newfilename,1)

EndIf

How can it move a file that doesn't exist?

What is happening here is that IF the file $Appfile already exisits, it is either renamed with the date and time added to the name OR with the word old added to the name (depends on the value of the variable $RNorOW). If the file does NOT already exist, then none of the renaming part takes place. Either way, when it gets to the FileOpen command, the $Appfile would not exist (as it didn't already exist or was renamed). The rename part is working OK both ways.

Just for fun, I commented out the rename routine and changed the 1 to a 2 (for clear and re-write). Still can't open the file (??).

Thanks

Sue

Edited by sness

Share this post


Link to post
Share on other sites

Is it perhaps a case of attempting to open $appfile after it has just renamed it to $newfilename?


[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Share this post


Link to post
Share on other sites

Hmm I thought AutoIt would create a new file for me if there wasn't one already. I rebooted, thinking maybe the file is already open in the background and can't be open again. Everything worked until I added a GUI to collect some of the variables up front and that's when started getting this message. I will take it all home and review the variables and various "open" "close" etc. Thanks everybody for the suggestions - If I figure it out, will update this post with my findings.

Share this post


Link to post
Share on other sites

FileOpen ( "filename", mode )

Remarks

A file can only be in either read or write mode; it cannot be in both.

When opening a file in write mode, the file will be created if it does not exist.

BTW - I still stand by my previous post. You are trying to move a file that doesn't exist.

Share this post


Link to post
Share on other sites

@WeaponX, yes, I stand by that post as well.... there is no logic in trying to move a file that you have just proven does not exist.

@sness, what are you trying to accomplish there?

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

You said you wanted to open the file for reading? You were opening the file for writing. You were also opening the old filename, not the new one. The following opens the file in its new location for reading:

$Appfile = $App & ".txt"
If FileExists($Appfile) Then
    If $RNorOW = "RN" Then
        $details = FileGetTime($Appfile)
        $newfilename = String($details[0]) & String($details[1]) & String($details[2]) & String($details[3]) & String($details[4]) & String($details[5])
        $newfilename = $App & $newfilename & ".txt"
        FileMove($Appfile, $newfilename, 1)
    Else
        $newfilename = $App & "old.txt"
        FileMove($Appfile, $newfilename, 1)
    EndIf
EndIf
;~ ;====================================================================Here
$file = FileOpen($newfilename, 0);Use parameter 0 for reading.  1 appends to already existing file and 2 erases existing one and opens it for writing.
; Check if file opened for reading OK =======================ERROR HERE ??
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file. " & $newfilename)
    Exit
EndIf

I hope that is what you were looking for.

- The Kandie Man ;-)

EDIT: Fixed msgbox file name

Edited by The Kandie Man

"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

Share this post


Link to post
Share on other sites

Thank you everyone for your help on this. This one was tricky. The reason the file could not be opened is that it was already opened. The script worked great until I added a GUI to the top of it. When I did this, I wanted the contents of the file to show in an edit box and since that is within a loop, then it was perpetually open. I had to create a secondary GUI to show the content of the file so I could first write to it. Thanks again for all of your help.

Share this post


Link to post
Share on other sites

Just make sure you put FileClose($filename) at the end of your script...

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
Sign in to follow this  
Followers 0