Jump to content

FileDelete doesn't


Recommended Posts

I'm using FileDelete("filename") at the end of a script but it doesn't work.

(The file is made with the script as well)

When I make a script with only FileDelete in the script, it deletes the file as it should.

Why doesn't AutoIt delete the file when I use it at the end of the script?

Link to comment
Share on other sites

  • Moderators

Is the file in use within your script ( do you have it open in another app or are you using FileOpen() without FileClose() before your call FileDelete() ) or by another application?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Post that part of the code where you are creating the file and the part where you are deleting it.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Post that part of the code where you are creating the file and the part where you are deleting it.

Strange, when I posted my question I added the script as an attachment but it seems that it disappeared.

I'll try again ( see attachment, script + the files that is being read by the script )

Change the extention from the file "backup.txt" to .bat ( I'm not allowed to upload .dat files )

#Include <Excel.au3>

;Local $sFileName = "backup.dat"
$sFileName = FileOpenDialog("Kies het .dat bestand.", @ScriptDir & "\", "Solarlog (*.dat)", 1 + 4 )
$startdatum = InputBox("Testing", "Vul datum in dd.mm.jj", "06.11.09", "", 190, 115, 200, 200,"","M")
$einddatum = InputBox("Testing", "Vul einddatum in dd.mm.jj", "06.06.10", "", 190, 115, 200, 200,"","M")
Local $iSearchStart = "2;1;" & $startdatum ;Including this date & time
Local $iSearchEnd = "2;1;" & $einddatum ; Excluding this date & time.

Local $sRes
Local $hFile = FileOpen($sFileName, 0)

; Check if file opened
If $hFile = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

Local $iCharNumStart = StringInStr(FileRead($sFileName), $iSearchStart, 0) - 1
If $iCharNumStart <= 0 Then
    MsgBox(16, "Error", 'Start date, "' & $iSearchStart & '" string not found in file.')
    Exit
EndIf

Local $iCharNumEnd = StringInStr(FileRead($sFileName), $iSearchEnd, 0)
If $iCharNumEnd <= 0 Then
    MsgBox(16, "Error", 'End date, "' & $iSearchEnd & '" string not found in file.')
    Exit
EndIf
;ConsoleWrite($iCharNumStart & " to " & $iCharNumEnd & @CRLF)

FileSetPos($hFile, $iCharNumStart, 0)

While 1
    $line = FileReadLine($hFile)
    If @error = -1 Or FileGetPos($hFile) >= $iCharNumEnd Then ExitLoop
    $sRes &= $line & @CRLF
    ;If FileGetPos($hFile) >= $iCharNumEnd Then ExitLoop ; If this line is used and line #33 edited, then end search date included.
WEnd

$file = FileOpen("DagMax.csv", 1)
FileWriteLine($file, $sRes & @CRLF)

; Close the handle.
FileClose($hFile)

;ConsoleWrite($sRes & @CRLF)

$tempcsv = _ExcelBookOpen(@ScriptDir &"\DagMax.csv", 0)
_ExcelBookSaveAs($tempcsv, @ScriptDir & "\DagMax", "xls")
_ExcelBookClose("DagMax.csv", 0)
ProcessClose("EXCEL.EXE") ; If not, DagMax.csv opens
$oExcel = _ExcelBookOpen(@ScriptDir &"\DagMax.xls", 0)
_ExcelColumnDelete($oExcel, 1, 2)
_ExcelColumnDelete($oExcel, 2)
_ExcelBookClose($oExcel)

FileDelete(@ScriptDir &"\DagMax.csv")

csv2excelMax.au3

backup.txt

Edited by renaixsence
Link to comment
Share on other sites

3rd edit...

you close $hFile, but the file you want to delete at the end of the script is opened with the variable $file. You need to close that one to delete it.

$file = FileOpen("DagMax.csv", 1)

FileDelete(@ScriptDir &"\DagMax.csv")

Edited by kaotkbliss

010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Link to comment
Share on other sites

Strange, when I posted my question I added the script as an attachment but it seems that it disappeared.

I'll try again ( see attachment, script + the files that is being read by the script )

Change the extention from the file "backup.txt" to .bat ( I'm not allowed to upload .dat files )

#Include <Excel.au3>

;Local $sFileName = "backup.dat"
$sFileName = FileOpenDialog("Kies het .dat bestand.", @ScriptDir & "\", "Solarlog (*.dat)", 1 + 4 )
$startdatum = InputBox("Testing", "Vul datum in dd.mm.jj", "06.11.09", "", 190, 115, 200, 200,"","M")
$einddatum = InputBox("Testing", "Vul einddatum in dd.mm.jj", "06.06.10", "", 190, 115, 200, 200,"","M")
Local $iSearchStart = "2;1;" & $startdatum ;Including this date & time
Local $iSearchEnd = "2;1;" & $einddatum ; Excluding this date & time.

Local $sRes
Local $hFile = FileOpen($sFileName, 0)

; Check if file opened
If $hFile = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

Local $iCharNumStart = StringInStr(FileRead($sFileName), $iSearchStart, 0) - 1
If $iCharNumStart <= 0 Then
    MsgBox(16, "Error", 'Start date, "' & $iSearchStart & '" string not found in file.')
    Exit
EndIf

Local $iCharNumEnd = StringInStr(FileRead($sFileName), $iSearchEnd, 0)
If $iCharNumEnd <= 0 Then
    MsgBox(16, "Error", 'End date, "' & $iSearchEnd & '" string not found in file.')
    Exit
EndIf
;ConsoleWrite($iCharNumStart & " to " & $iCharNumEnd & @CRLF)

FileSetPos($hFile, $iCharNumStart, 0)

While 1
    $line = FileReadLine($hFile)
    If @error = -1 Or FileGetPos($hFile) >= $iCharNumEnd Then ExitLoop
    $sRes &= $line & @CRLF
    ;If FileGetPos($hFile) >= $iCharNumEnd Then ExitLoop ; If this line is used and line #33 edited, then end search date included.
WEnd

$file = FileOpen("DagMax.csv", 1)
FileWriteLine($file, $sRes & @CRLF)

; Close the handle.
FileClose($hFile)

;ConsoleWrite($sRes & @CRLF)

$tempcsv = _ExcelBookOpen(@ScriptDir &"\DagMax.csv", 0)
_ExcelBookSaveAs($tempcsv, @ScriptDir & "\DagMax", "xls")
_ExcelBookClose("DagMax.csv", 0)
ProcessClose("EXCEL.EXE") ; If not, DagMax.csv opens
$oExcel = _ExcelBookOpen(@ScriptDir &"\DagMax.xls", 0)
_ExcelColumnDelete($oExcel, 1, 2)
_ExcelColumnDelete($oExcel, 2)
_ExcelBookClose($oExcel)

FileDelete(@ScriptDir &"\DagMax.csv")

csv2excelMax.au3

backup.txt

Your FileClose() is incorrect.

That portion of the code is better written as

$file = "DagMax.csv"
$hFile = FileOpen($file, 1);; open the file as a handle
FileWriteLine($$hFile, $sRes & @CRLF);; Use the handle

; Close the handle.
FileClose($hFile)

Edit: forgot the code tags

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

3rd edit...

you close $hFile, but the file you want to delete at the end of the script is opened with the variable $file. You need to close that one to delete it.

$file = FileOpen("DagMax.csv", 1)

FileDelete(@ScriptDir &"\DagMax.csv")

Thanks, seems to work ( when I uncommented "ProcessClose("EXCEL.EXE") ; If not, DagMax.csv opens" )
Link to comment
Share on other sites

After FileWriteLine I did put FileClose with the comment ";Close the handle"

.....
$file = FileOpen("DagMax.csv", 1)
FileWriteLine($file, $sRes & @CRLF)

; Close the handle.
FileClose($hFile)
....

Yes I see that you're closing the first FileOpen, but you're not closing the second even though it's that one you want! Now what you need to do is exactly as I and kaotkbliss said, add a FileClose. Also you're using FileRead() completely wrong, either use the file-handle from FileOpen() or remove the FileOpen().

@GEOSoft

That doesn't seem much better. I'm pretty sure that redeclaring a variable with a file-handle won't close the old handle.

Link to comment
Share on other sites

Yes I see that you're closing the first FileOpen, but you're not closing the second even though it's that one you want! Now what you need to do is exactly as I and kaotkbliss said, add a FileClose. Also you're using FileRead() completely wrong, either use the file-handle from FileOpen() or remove the FileOpen().

@GEOSoft

That doesn't seem much better. I'm pretty sure that redeclaring a variable with a file-handle won't close the old handle.

I only put it in as it applies to that portion of the code I showed. Not the whole script. I declared the file as a variable for easier reference and then declared the handle with the FileOpen() and then used the handle until the FileClose again.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Also you're using FileRead() completely wrong, either use the file-handle from FileOpen() or remove the FileOpen().

@GEOSoft

That doesn't seem much better. I'm pretty sure that redeclaring a variable with a file-handle won't close the old handle.

The part with FileRead() is a script that I found on this forum, you're the first to tell me it's not correct.

It works for me and I'm not a crack in AutoIt so I don't mind if the script contains errors.

Maybe you can help me out the modify the part with FileRead(). :mellow:

Link to comment
Share on other sites

The part with FileRead() is a script that I found on this forum, you're the first to tell me it's not correct.

It works for me and I'm not a crack in AutoIt so I don't mind if the script contains errors.

Maybe you can help me out the modify the part with FileRead(). :mellow:

Actually for FileRead() you don't have to FileOpen() it at all.

$sStr = FileRead("somefile.txt") works just as well. Then perform your operations on $sStr before you write any data back.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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