Jump to content

File Deletion Problems


JohnBoy
 Share

Recommended Posts

I'm sure it's something dumb I'm doing, or not doing, but I've pretty much run out of ideas. Problem synopsis:

$OutputFile is a string containing the complete path and filename of the desired file. I open a file with FileOpen($OutputFile, 2). I add things to the file, change things, etc. I then close the file with FileClose($OutputFile). After closing the file, I decide I want to delete the file with FileRecycle($OutputFile) or FileDelete($OutputFile). The file will NOT delete. If I alt-tab and leave the script running and manually try to delete the file myself, I get a sharing violation error. I thought when I closed the file with FileClose, it would release any locks on the file. The only way I've been able to delete the file is to terminate the script, which then obviously releases any locks on the file. The idea I'm trying to implement is to have the script automatically delete any 0-byte size output files, since for my purposes, a 0-byte file size would by definition be an invalid data file. I am running v3.1.1.84 (beta) with Window$ 2000, SP4. Any help would be gratefully appreciated.

Link to comment
Share on other sites

Alrighty, let's see if anybody can tell me what I'm doing wrong. Here's a segment of code that's giving me problems. The entire project is 1000+ lines.

If GUICtrlRead($ChkAlphaSource) = $GUI_CHECKED Then

If $NumOfBkgPoints < 3 Or $NumOfAlphaSrcPoints < 3 Then

$Msg = "This set of data cannot be processed due to an insufficient number of data points being present. "

$Msg = $Msg & "Please examine the data files for errors or select another set of data files and try again."

MsgBox(0, "Error Encountered!", $Msg)

Return

EndIf

$Ctr = 0

$AlphaBkgCtr = 0

$AlphaSrcCtr = 0

ProgressOn("Processing Alpha Source Data", "Percent Processed. ", "0 percent")

$OutputFile = $OutputFileLoc & "\" & @MON & "-" & @MDAY & "-" & @YEAR & "_" & @HOUR & "_" & @MIN & "_" & @SEC & "_AlphaSrc" & "." & $OutputFileExtension

FileOpen($OutputFile, 2)

While $AlphaBkgCtr < $NumOfBkgPoints

If $AlphaSrcCtr >= $NumOfAlphaSrcPoints Then

ProgressSet(100, "Done", "Complete")

ProgressOff()

ExitLoop

EndIf

$DeltaTime = _DateDiff("n", $AlphaBkgTime[$AlphaBkgCtr], $AlphaSrcTime[$AlphaSrcCtr])

If $AlphaBkgCtr = $NumOfBkgPoints - 1 Then

ProgressSet(100, "Done", "Complete")

ProgressOff()

EndIf

Select

Case $DeltaTime > 0 And $DeltaTime <= $MaxDeltaTime

If FilterSampleData($AlphaSrcTime[$AlphaSrcCtr]) Then

$AlphaNetCPM[$Ctr] = $GrossAlphaCPM[$AlphaSrcCtr] - $AlphaBkgCPM[$AlphaBkgCtr]

FileWrite($OutputFile, _FormatDateLabStat(_FormatDateSpecify ("YYYY/MM/DD HH:mm", "MM-DD-YY HH:mm", $AlphaSrcTime[$AlphaSrcCtr])))

FileWrite($OutputFile, ", " & Int($AlphaNetCPM[$Ctr]) & ", , " & Int($AlphaNetCPM[$Ctr]) & @CRLF)

EndIf

$DataProcessed = Int(($AlphaBkgCtr / $NumOfBkgPoints) * 100)

ProgressSet($DataProcessed, $DataProcessed & " percent")

$Ctr = $Ctr + 1

ReDim $AlphaNetCPM[$Ctr + 1]

$AlphaBkgCtr = $AlphaBkgCtr + 1

$AlphaSrcCtr = $AlphaSrcCtr + 1

Case $DeltaTime > 0 And $DeltaTime > $MaxDeltaTime

$AlphaBkgCtr = $AlphaBkgCtr + 1

Case $DeltaTime <= 0

$AlphaSrcCtr = $AlphaSrcCtr + 1

EndSelect

WEnd

If $AlphaSrcCtr = 0 Then

$Msg = "No net alpha source cpm results could be calculated based on the background and source data available. "

$Msg = $Msg & "This is typically due to selecting a set of background and source data files with no "

$Msg = $Msg & "overlapping date ranges. Please ensure that you have selected the appropriate data files "

$Msg = $Msg & "and try again."

MsgBox(0, "Error Encountered!", $Msg)

Return

EndIf

FileClose($OutputFile)

$FileSize = FileGetSize ($OutputFile)

If $FileSize = 0 Then

$Msg = "The Alpha source file produced contains no data! Please ensure that the filter date "

$Msg = $Msg & "range selected is correct for the raw data file set."

MsgBox(0, "Error Encountered!", $Msg)

FileRecycle( $OutputFile )

EndIf

EndIf

Link to comment
Share on other sites

···
$OutputFile = '(a file path)'
FileOpen($OutputFile, 2)
···
FileWrite($OutputFile, ...)
···
FileClose($OutputFile)
···

These lines are causing the problem. FileOpen() returns a handle which you would then use in FileWrite() and FileClose() calls. The correct way to do what you want is closer to this:

···
$OutputFile = '(a file path)'
$FileHandle = FileOpen($OutputFile, 2)
···
FileWrite($FileHandle, ...)
···
FileClose($FileHandle)
···
Link to comment
Share on other sites

While we're on the topic of FileClose() usage, the help file states the following:

Remarks

If filehandle is invalid, an AutoIt runtime error will be thrown, and the script will terminate!

And therefore this line should throw a fatal error:

FileClose('C:\Any File Path.txt')

however it does not. My personal opinion is that this should in fact happen -- it will make usage errors much more obvious. Failing that, the documentation should probably be updated to have this information removed.

Link to comment
Share on other sites

B) Thanks a bunch LxP. That did it. If I understand what happened correctly, I opened the file, but without using the filehandle to identify it for the FileClose function, the file never would close. Ergo the sharing violation. Would this be something close to correct?
Link to comment
Share on other sites

Correct.

What was happening precisely (if anyone's interested) was that you were requesting a handle to the file (but not accepting it), writing to the file via its path instead of via its handle (which would open and close temporary handles to perform that write, which is why they would have worked), and then passing a file path to FileClose() instead of the handle previously returned by FileOpen().

I suppose that since you're only making two writes (which could be combined into one call anyway), you could probably do away with the FileOpen() and FileClose() if you wanted to.

Edited by LxP
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...