Jump to content
BatMan22

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

Recommended Posts

BatMan22

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

Share this post


Link to post
Share on other sites
Jos

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

Jos

  • Like 1

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
BatMan22

Yeah.. I read it. I'm just an idiot. I figured it out about 30 seconds after I posted by looking at the example... I just can't find out how to delete this posting. Took me 10 minutes to write up this post. 

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

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.