DeleteFile

Blitz3D Forums/Blitz3D Programming/DeleteFile

Hi all,

No matter what I try, DeleteFile steadfastly refuses to delete a file! I first use FileType and supply Path$+Filename$ (after setting Path$ via CurrentDir$() at the start of the program). Having received a "1" back from FileType, I then pass the same variables to DeleteFile but the file is never deleted:

If FileType(Path$+Filename$)=1 Then
DeleteFile Path$+Filename$
EndIf

The same things happens in Blitz Basic 2D. Can anyone please tell me what I am doing wrong? :o(

Most likely an error in the Path$ or Filename$.

Just created a folder with 'test.bb' and 'deleteme.txt' files in it.

test.bb:
DeleteFile "deleteme.txt"

Worked fine (didn't bother with error trapping because I knew for a fact that the file was there).

Try putting Path$+Filename$ into debuglog and see what it says...

I had this problem when trying to delete files from a temp folder I created in-code. I couldn't delete the temp folder because - for some reason - blitz refused to delete some files contained in it. Hmm, or was it the folder itself I couldn't delete?! Can't really remember.

Anyway, the only way I could get it to work was to do a EndGraphics before trying to delete the files! :/

Go figure.

[edit] Just checked. It was defo DeleteFile, not DeleteDir I was having the problem with. Using EndGraphics to 'fix' the prob was OK in my case because I was only deleting the temp folder when closing down the app, anyway. May not be a viable solution for you, though.

Hmmm. Just did same as Gfk and it worked for me too - file was deleted no probs. Tried again in graphics mode (same path and file) as per big10p and blitz didn't delete it!

Weird...

Thanks for the help - at least I now know of a workaround.

Graphics 1024,768,32,2
CreateDir "ThisIsAJoke"
filout=WriteFile("ThisIsAJoke\ItsNotThe1stofApril.txt")
WriteLine filout,"ComeOffItGuys"
CloseFile filout

While FileType("ThisIsAJoke\ItsNotThe1stofApril.txt")=0
Wend

If FileType("ThisIsAJoke\ItsNotThe1stofApril.txt")=1
	Print "The File is there"
Else
	Print "Sorry - No File"
EndIf

If FileType("ThisIsAJoke\ItsNotThe1stofApril.txt")=1
	DeleteFile "ThisIsAJoke\ItsNotThe1stofApril.txt"
	Print "Deleted file ThisIsAJoke\ItsNotThe1stofApril.txt"
EndIf

While FileType("ThisIsAJoke\ItsNotThe1stofApril.txt")=1
Wend

If FileType("ThisIsAJoke\ItsNotThe1stofApril.txt")=1
	Print "The File is there"
Else
	Print "Sorry - No File"
EndIf

If FileType("ThisIsAJoke")=2
	DeleteDir "ThisIsAJoke"
EndIf

If FileType("ThisIsAJoke")=2
	Print "The Dir is there"
Else
	Print "Sorry - No Dir"
EndIf

Print
Print "Hit Mouse"

WaitMouse()
End


You want to tell this doesn't work?
This Forum is on a good way to become a Punch and Judy show ...


Just look at those threads:
Error Opening/Closing File
Crash when leaving Screensaver
DeleteFile
Get File in String
array problam(i think so)
Save heightmap from Mesh-terrain.. Please help..

Its all in the wrong place ... Beginners is one Forum above ...

Chalky wrote:
The same things happens in Blitz Basic 2D. Can anyone please tell me what I am doing wrong? :o(

If you would have provided your complete relevant code, we could have told you what you're doing wrong...
One thing's for sure ... it's your program what's wrong ... nothing else!
.

Hi RaGR,

Not sure why you feel the need to be insulting. I am not a beginner - I have been programming professionally for 17 years, which is why this is not in the beginner's forum. What I do not understand is that this:

Path$=CurrentDir$()
FileName$="Test.txt"
If FileType(Path$+FileName$)=1 Then
  DeleteFile Path$+FileName$
EndIf


works, while this:

Graphics ScreenW%,ScreenH%,32,ScreenM%
Path$=CurrentDir$()
.
.
.
Function BackDropSelect()
  Local LoadFile$
  Local FileT%=0

  BackDropDraw()
  Flip
  LoadFile$=FileSelect$()
  If LoadFile$<>"<CANCEL>" Then
    If LoadFile$="<NONE>" Then
      If BackDrop%<>0 Then
        FreeImage BackDrop%
        BackDrop%=0
        FileT%=FileType(Path$+"Game.ini")
        If FileT%=1 Then
          DeleteFile Path$+"Game.ini"
        EndIf
      ElseIf LoadFile$<>""
        BackDropLoad(Path$+LoadFile$)
        INISave(LoadFile$)
      EndIf
    EndIf
  EndIf
  BackDropDraw()
  MenuDraw()
End Function


...doesn't.

I am well aware that it could be something I have done wrong, but the deletefile above does not delete the INI file even though FileType returns 1, whereas pasting contents of Path$+"Game.ini" into VB KILL command does.

[edit]
I have just hard coded the path and filename:

Function BackDropSelect()
  Local LoadFile$
  Local FileT%=0

  BackDropDraw()
  Flip
  LoadFile$=FileSelect$()
  If LoadFile$<>"<CANCEL>" Then
    If LoadFile$="<NONE>" Then
      If BackDrop%<>0 Then
        FreeImage BackDrop%
        BackDrop%=0
        FileT%=FileType("C:\BlitzBasic\SmallSoft\Game.ini")
        If FileT%=1 Then
          DeleteFile "C:\BlitzBasic\SmallSoft\Game.ini"
        EndIf
      ElseIf LoadFile$<>""
        BackDropLoad(Path$+LoadFile$)
        INISave(LoadFile$)
      EndIf
    EndIf
  EndIf
  BackDropDraw()
  MenuDraw()
End Function


and the file was not deleted. I have tested this repeatedly in debug mode and FileT% is definitely being set to 1 via FileType when BackDrop%<>0 and LoadFile$="<NONE>" [and "C:\BlitzBasic\SmallSoft\Game.ini" exists!].

Creating a new program with the code:

FileT%=FileType("C:\BlitzBasic\SmallSoft\Game.ini")
If FileT%=1 Then
  DeleteFile "C:\BlitzBasic\SmallSoft\Game.ini"
EndIf


DID delete the file.

If you can shed any light on what I am doing wrong I would be very grateful.

You want to tell this doesn't work?
This Forum is on a good way to become a Punch and Judy show ...

Just look at those threads:
Error Opening/Closing File
Crash when leaving Screensaver
DeleteFile
Get File in String
array problam(i think so)
Save heightmap from Mesh-terrain.. Please help..

Its all in the wrong place ... Beginners is one Forum above ...
FFS, who died and made you the Forum Police?!

You seriously think the best way to welcome new Blitz users is to kick off at them like that?? You really need to sharpen up your act and get rid of the idea that everbody is beneath you. This isn't the first time you've been outright insulting to people for no justifiable reason.

Anyhoo...

Chalky - I've tried all sorts to get it to not work here, but without success.

Are you maybe trying to delete a read-only file or something? Because that's the only way I can stop DeleteFile from doing what it should...

Looking at your code above, I'd put debug Stop's in every nested 'If'. That way you'll see which bits of your code are being executed and which aren't.

DeleteFile will also fail if the file is open.

It is fairly easy when debugging to end up with a copy of your program running in the background. In that case the file may still be in use even though you think you have closed it.

Its all in the wrong place
No it's not. It's correct to post in one of the programming forums when you think you've found a bug, so that others can confirm it before posting in the bug reports forum. Otherwise, the bug reports forum gets clogged with 'bugs' that aren't bugs at all.

I seem to remember BRL actually requested that everyone follow this protocol when reporting bugs. Unfortunately, most people don't follow it and just jump straight into the bug reports forum only to discover the 'bug' is infact a coding error on their part.

Your attitude isn't appreciated or helpful, RaGR.

Anyway, I'm going to look into this issue a bit more as I'm sure when I had the problem I wasn't forgetting to close the files before deleting them. :)

[edit]
I went back to my code I was having a similar problem with and it seems unrelated to your problem afterall, Chalky. I was messing with TTF font files which the system may have still had a handle on so wouldn't allow them to be deleted.

I tried re-creating the problem you're having (using the code you posted) and couldn't do it - the file got deleted. Maybe Floyd's points are worth checking out. I don't know what else to suggest.

Hi all,

The fact that so many of you can get it to work is proof that it must be my coding rather than Blitz which is at fault. Guess I have an evening's serious debugging to look forward to... :0)

[edit]
Floyd - thank's a million! So much for me being a pro programmer - my INILoad function was not closing the file! I can't believe I missed that! Duh!!! :o/

Thank you all so much for your help. Much appreciated.

This can happen to the best of us. Often a program gets so complicated that you might overlook even the simplest of things.

Eventhough the problem is solved I thought I might write something here the sprung into mind when reading this thread. I thought that a wait statement between creating and deleting the file might help. But not sure though...

Anyways, I am glad your problem is solved Chalky.