Jump to content

FileClose doesn't release file so I can access it in other programs


 Share

Recommended Posts

So for some reason while my program is running the text files that I manipulate don't seem to close and I get errors from notepad when I write to them (The process cannot access the file because it is being used by another process). I searched around on the forums and found that apparently maybe the way I'm calling FileOpen is the issue? I don't understand it, I use Fileclose everytime I fileopen, and I even run a special function at the end of the program to close the files (see third part of code). My program has a GUI and isn't designed to be closed, so I am really hoping I can fix this FileOpen issue.. So my questions are:

1) Am I calling the FileOpen/FileClose function incorrectly and How do I fix my current situation? 

2) Do I even NEED to use the fileopen? Apparently it's not necessary to use FileOpen to FileWrite? If so.. How do I replace the FileOpen (Bla, 2) mode? Or is my issue that I ran FileWrite somewhere before opening?

 

$Whattowrite = StringRegExpReplace(FileRead($filetocleanup), "(?m)/*$", "")
    FileOpen($filetocleanup, 2)
    FileWrite($filetocleanup, $Whattowrite)

    Local $aFile, $aArray
    _FileReadToArray($filetocleanup, $aFile, $FRTA_NOCOUNT)
    $aArray = _ArrayUnique($aFile)
    _ArrayDelete($aArray, 0)
;~  _ArrayDisplay($aArray)
    _FileWriteFromArray($filetocleanup, $aArray)
    FileClose($filetocleanup)
    FileClose($aFile)
If $WhichMachine = "1" Then
        $sFileName = $vRemoteDir & "ic1.txt"
    EndIf

    If $WhichMachine = "2" Then
        $sFileName = $vRemoteDir & "ic2.txt"
    EndIf

    If $WhichMachine = "3" Then
        $sFileName = $vRemoteDir & "ic3.txt"
    EndIf


    Local $sText = FileRead($sFileName)
    $sText = StringRegExpReplace($sText, '(?m)^([\w-]+\h[\w.]+)\N+\R?\K(\1\R?)*', "")
    ConsoleWrite($sText & @LF)
    FileOpen($sFileName, 2)
    FileWrite($sFileName, $sText)
    FindAndReplace("<", "", $sFileName) ; Change First Two Patterns to go go!
    FindAndReplace(">", "", $sFileName) ; Change First Two Patterns to go go!
    FindAndReplace("; ", "/", $sFileName) ; Change First Two Patterns to go go!
    FindAndReplace("NO3/NO3_1/", "NO3/", $sFileName) ; Change First Two Patterns to go go!
    FindAndReplace("NO2/NO2_1/", "NO2/", $sFileName) ; Change First Two Patterns to go go!
    $Whattowrite = StringRegExpReplace(FileRead($sFileName), "(?m)/*$", "") ; Remove Empty Lines
    FileOpen($sFileName, 2)
    FileWrite($sFileName, $Whattowrite)
    FileClose($sFileName)
Func ReleaseFiles()
    FileClose($vRemoteDir & "ic1.txt")
    FileClose($vRemoteDir & "ic1.txt")
    FileClose($vRemoteDir & "ic3.txt")
    FileClose($vRemoteDir & "ic5.txt")
EndFunc   ;==>ReleaseFiles

 

Edited by BatMan22
Link to comment
Share on other sites

  • Developers

Please read the Helpfile! The FileClose takes the FileHandle returned by the FileOPen(), not a filename!

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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...