Jump to content

Problem with the Open-Delete-ReCreate Pattern


Recommended Posts

Respected Autoit Forum Members,
If this is a problem my problem is basic.
 

$path=@AppDataDir & "\etc\Isproblem.txt"
FileOpen($path)
;FileClose($path) doesn't matter if it exists or not
;some code here
FileDelete($path)
;some code here
_FileCreate($path) ; It must be created in exactly the same path and with the same name.

#cs --------------------------

Result: file cannot be created.

#ce --------------------------

if you somehow force to create the file, this time, a file access problem occurs and cannot be overwritten.

Although I can't understand why it is caused, I can overcome it in 3 ways

1- Don't use FileOpen

2- Restart the script

3- Create the file with another name or path and move it to its old location.

Because of this problem, I had to remove "FileOpen" in all my code. But I still do not mind.
Now I use this to clean files instead of..

FileOpen($path,2)
FileClose($path)

this one:

_FileCreate($path)


I apologize for keeping you busy, this was my unnecessary problem. Thanks.

Link to comment
Share on other sites

Link to comment
Share on other sites

Not sure what problem you are referring to. Here's a modified version and my console output --

#include <File.au3>

$path=@AppDataDir & "\etc\Isproblem.txt"
ConsoleWrite($path & @CRLF)
$hFile = FileOpen($path)
ConsoleWrite("open=" & @error & @CRLF)
FileClose($hFile)
ConsoleWrite("close=" & @error & @CRLF)
;some code here
FileDelete($path)
ConsoleWrite("delete=" & @error & @CRLF)
;some code here
_FileCreate($path)
ConsoleWrite("FileCreate=" & @error & @CRLF)
>"C:\Program Files (x86)\AutoIt3\SciTE\..\AutoIt3.exe" "C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.au3" /run /prod /ErrorStdOut /in "C:\Users\danpollak\Dropbox\webdriver\test3.au3" /UserParams    
+>10:46:07 Starting AutoIt3Wrapper (19.1127.1402.26) from:SciTE.exe (4.4.6.0)  Keyboard:00000409  OS:WIN_10/2009  CPU:X64 OS:X64  Environment(Language:0409)  CodePage:0  utf8.auto.check:4
+>         SciTEDir => C:\Program Files (x86)\AutoIt3\SciTE   UserDir => C:\Users\danpollak\AppData\Local\AutoIt v3\SciTE\AutoIt3Wrapper   SCITE_USERHOME => C:\Users\danpollak\AppData\Local\AutoIt v3\SciTE 
>Running AU3Check (3.3.14.5)  from:C:\Program Files (x86)\AutoIt3  input:C:\Users\danpollak\Dropbox\webdriver\test3.au3
+>10:46:08 AU3Check ended.rc:0
>Running:(3.3.14.5):C:\Program Files (x86)\AutoIt3\autoit3.exe "C:\Users\danpollak\Dropbox\webdriver\test3.au3"    
+>Setting Hotkeys...--> Press Ctrl+Alt+Break to Restart or Ctrl+BREAK to Stop.
C:\Users\danpollak\AppData\Roaming\etc\Isproblem.txt
open=0
close=0
delete=0
FileCreate=0
+>10:46:08 AutoIt3.exe ended.rc:0
+>10:46:08 AutoIt3Wrapper Finished.
>Exit code: 0    Time: 1.256

 

Link to comment
Share on other sites

48 minutes ago, Danp2 said:

You can't delete a file that is currently open, and you aren't closing it correctly. Try this --

$path=@AppDataDir & "\etc\Isproblem.txt"
$hFile = FileOpen($path)
FileClose($hFile) 
;some code here
FileDelete($path)
;some code here
_FileCreate($path)

P.S. Welcome to the forum!

Oh sorry, I noticed that all this time I am not using "FileClose" as it should be. Yes this solves the problem. Thanks for all your advice and help.

Link to comment
Share on other sites

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...