renaixsence Posted June 7, 2010 Share Posted June 7, 2010 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 More sharing options...
Moderators SmOke_N Posted June 7, 2010 Moderators Share Posted June 7, 2010 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 More sharing options...
BurakSZ Posted June 7, 2010 Share Posted June 7, 2010 Maybe the current folder is another. When you were on your windows folder before and justen typed FileDelete("test.txt") then it searches the text.txt in the windows folder. So always: FileDelete(@ScriptDir & "\test.txt") Of course if the file is in your Script folder. Link to comment Share on other sites More sharing options...
GEOSoft Posted June 7, 2010 Share Posted June 7, 2010 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 More sharing options...
renaixsence Posted June 7, 2010 Author Share Posted June 7, 2010 (edited) 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 ) expandcollapse popup#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 June 7, 2010 by renaixsence Link to comment Share on other sites More sharing options...
AdmiralAlkex Posted June 7, 2010 Share Posted June 7, 2010 You can't delete a file that is opened!! Add FileClose($File) after the FileWriteLine. .Some of my scripts: ShiftER, Codec-Control, Resolution switcher for HTC ShiftSome of my UDFs: SDL UDF, SetDefaultDllDirectories, Converting GDI+ Bitmap/Image to SDL Surface Link to comment Share on other sites More sharing options...
renaixsence Posted June 7, 2010 Author Share Posted June 7, 2010 You can't delete a file that is opened!! Add FileClose($File) after the FileWriteLine. 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) .... Link to comment Share on other sites More sharing options...
kaotkbliss Posted June 7, 2010 Share Posted June 7, 2010 (edited) 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 June 7, 2010 by kaotkbliss 010101000110100001101001011100110010000001101001011100110010000 001101101011110010010000001110011011010010110011100100001 My Android cat and mouse gamehttps://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek We're gonna need another Timmy! Link to comment Share on other sites More sharing options...
GEOSoft Posted June 7, 2010 Share Posted June 7, 2010 (edited) 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 ) expandcollapse popup#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 June 7, 2010 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 More sharing options...
renaixsence Posted June 7, 2010 Author Share Posted June 7, 2010 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 More sharing options...
AdmiralAlkex Posted June 7, 2010 Share Posted June 7, 2010 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. .Some of my scripts: ShiftER, Codec-Control, Resolution switcher for HTC ShiftSome of my UDFs: SDL UDF, SetDefaultDllDirectories, Converting GDI+ Bitmap/Image to SDL Surface Link to comment Share on other sites More sharing options...
GEOSoft Posted June 7, 2010 Share Posted June 7, 2010 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().@GEOSoftThat 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 More sharing options...
AdmiralAlkex Posted June 7, 2010 Share Posted June 7, 2010 I'm perfectly aware what you did. renaixsence is most likely not, so posting confusing code is only going to confuse him. .Some of my scripts: ShiftER, Codec-Control, Resolution switcher for HTC ShiftSome of my UDFs: SDL UDF, SetDefaultDllDirectories, Converting GDI+ Bitmap/Image to SDL Surface Link to comment Share on other sites More sharing options...
renaixsence Posted June 8, 2010 Author Share Posted June 8, 2010 Also you're using FileRead() completely wrong, either use the file-handle from FileOpen() or remove the FileOpen().@GEOSoftThat 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(). Link to comment Share on other sites More sharing options...
DarkHo Posted June 8, 2010 Share Posted June 8, 2010 Try FileDelete(@ScriptDir & "\test.txt") is the exe file is in the same folder Link to comment Share on other sites More sharing options...
GEOSoft Posted June 8, 2010 Share Posted June 8, 2010 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(). 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now