Author Topic: Deleting Files  (Read 3410 times)

0 Members and 1 Guest are viewing this topic.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Deleting Files
« on: July 10, 2019, 04:50:17 pm »
I have a subroutine in a program which calculates a number of values. These are then stored to a file for future use. The program will return to this subroutine in the course of its run. The subsequent runs of this subroutine however is duplicating the values. I have tried to reset the local and dim shared variables used in the calculation to zero before the subsequent runs but still getting duplicate values. The values are being written to a file using "for output" which I thought would simply rewrite the corrected values (ie "for output" would in essence delete the old file) but that is not working.

I would now like to find another way to delete the complete file after the each run thru the subroutine but can't seem to find a command for this. I have tried Shell and Kill but it would appear I may not have Dos in my windows 10 os. In any event, is there a _DeleteFile command somewhere in the universe that may work in this situation? No big deal if there isn't as I don't know if that would solve my problem anyway. Thinking I need to scrap the whole routine and rework it - just the disappointment to see all those hours building the subroutine as a waste of time.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Deleting Files
« Reply #1 on: July 10, 2019, 05:11:28 pm »
Check your paths if you really open the same file for writing (for example, if you don't have a file with the same name in the QB64 installation folder and the same in the program directory), I don't know of a single case where KILL or OPEN FOR OUTPUT wouldn't work. Standard for delete file is KILL "filepath+filename.extension"   To get current work directory use _CWD$.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Deleting Files
« Reply #2 on: July 10, 2019, 05:13:26 pm »
If it isn't a path issue, my thought is, you cannot delete the file, because it is still open in the routine. Files need to be closed first. del can be used with SHELL, which works in Win10 via the command prompt.

http://www.easydos.com/del.html

But KILL will work in QB64, provided the file has been closed, first.

Maybe this isn't your problem, but I'm having difficulty understanding your explanation. If I experienced the routine, that probably wouldn't be an issue, but without a common form of reference, well, it leaves me just guessing.

Pete

Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Deleting Files
« Reply #3 on: July 10, 2019, 06:22:40 pm »
Hi
reading the wiki http://www.qb64.org/wiki/OPEN#File_Access_Modes

Quote
OUTPUT: Sequential mode creates a new file or erases an existing file for new program output. Use WRITE # to write numerical or text data or PRINT # for text. OUTPUT clears files of all data and clears the receive buffer on other devices such as COM.

and this example of wiki works fine
Code: QB64: [Select]
  1.  
  2. OPEN "test.tst" FOR OUTPUT AS #1
  3. PRINT #1, "If test.tst didn't exist:"
  4. PRINT #1, "A new file was created named test.tst and then deleted."
  5. PRINT #1, "If test.tst did exist:"
  6. PRINT #1, "It was overwritten with this and deleted."
  7.  
  8. OPEN "test.tst" FOR INPUT AS #1
  9.     INPUT #1, a$
  10.     PRINT a$
  11.  
  12. KILL "test.tst"
  13.  
  14.  

so playing with the code we can experiment that if you don't close OUTPUT file, you write data appending to the previous data as the following example shows
Code: QB64: [Select]
  1.  
  2. OPEN "test.tst" FOR OUTPUT AS #1
  3. PRINT #1, "If test.tst didn't exist:"
  4. PRINT #1, "A new file was created named test.tst and then deleted."
  5. PRINT #1, "If test.tst did exist:"
  6. PRINT #1, "It was overwritten with this and deleted."
  7. COLOR 12, 4
  8. GOSUB readfile
  9.  
  10. PRINT #1, "-If test.tst didn't exist:"
  11. PRINT #1, "-A new file was created named test.tst and then deleted."
  12. PRINT #1, "-If test.tst did exist:"
  13. PRINT #1, "-It was overwritten with this and deleted."
  14. COLOR 11, 1
  15. GOSUB readfile
  16.  
  17. KILL "test.tst"
  18. COLOR 7, 0
  19.  
  20. readfile:
  21. OPEN "test.tst" FOR INPUT AS #2
  22.     INPUT #2, a$
  23.     PRINT a$

IMHO Petr and Pete have showed you where is the bug. :-)
Good Coding
Programming isn't difficult, only it's  consuming time and coffee

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Deleting Files
« Reply #4 on: July 11, 2019, 10:51:10 am »
Thanks guys. The offending file was closed - still not sure why the Shell Kill is not working and will explore that but I think I may have discovered a possible cause. The program is complex with the Subroutines having multiple smaller subroutine within them. I have been drawing a flow chart of the movement of the data - Picasso would be proud - and I seem to have a smaller subroutine which is jumping out of the problematic one and (so far as I trace the flow) does not go back before the problematic subroutine is called again. I'm thinking I need the flow to return to hit that End of Sub marker before it can be called again. Rookie mistake if that is what is happening.
   In terms of using Dos commands, when that wasn't working I checked the wiki on Dos and as Windows 10 wasn't listed I just assumed that was my problem using Kill. I have since found the Command Prompt when I fire up windows, so Dos must be there. Just the statement SHELL, should be all I need to jump into the Dos world?? So step two would be to ID the file and step 3 to just Kill that file? I have zero experience with Dos and will go on the web to search out commands. The file I'm thinking of Killing is in my "j" drive. So I'm thinking I need to do something like SHELL j:GDProblimaticFile\Kill?
  All these years and I have never had to access Dos - I'm hoping the problem is in the data flow.
Thanks again guys

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Deleting Files
« Reply #5 on: July 11, 2019, 01:16:43 pm »
Use Explorer to find the file, and go to the address bar, and copy its full path. That's what you need to kill the file, if it is on a different drive, the complete drive plus path and file name.

The only other thing that prevents a file from being killed on command is if it is a READ ONLY file. That would result in a permission denied error code message.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Deleting Files
« Reply #6 on: July 12, 2019, 07:19:26 am »
TempodiBasic - your examples work fine. I hadn't realized the Kill statement would work without first invoking SHELL. I have been on line checking out Dos and the potential which SHELL could add to some of the tasks coded into the existing program. Surprising how a new perspective on old coding sweeps over you when you ask someone for help. Thanks again guys.

Offline euklides

  • Forum Regular
  • Posts: 128
    • View Profile
Re: Deleting Files
« Reply #7 on: July 12, 2019, 08:04:44 am »
Perhaps this ???

if the file is already destroyed or is missing, then the kill function will no work.

so I do always so:

Fictokil$="c:\exemple.txt"
close 4:open fictokil$ for output as 4:print #4,"**":close 4
kill fictokill$
Why not yes ?

FellippeHeitor

  • Guest
Re: Deleting Files
« Reply #8 on: July 12, 2019, 08:37:01 am »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Deleting Files
« Reply #9 on: July 12, 2019, 09:41:37 am »
Thanks guys. The offending file was closed - still not sure why the Shell Kill is not working and will explore that but I think I may have discovered a possible cause. The program is complex with the Subroutines having multiple smaller subroutine within them. I have been drawing a flow chart of the movement of the data - Picasso would be proud - and I seem to have a smaller subroutine which is jumping out of the problematic one and (so far as I trace the flow) does not go back before the problematic subroutine is called again. I'm thinking I need the flow to return to hit that End of Sub marker before it can be called again. Rookie mistake if that is what is happening.
   In terms of using Dos command when that wasn't working I checked the wiki on Dos and as Windows 10 wasn't listed I just assumed that was my problem using Kill.  I have since found the Command Prompt when I fire up windows, so Dos must be there. Just the statement SHELL, should be all I need to jump into the Dos world?? So step two would be to ID the file and step 3 to just Kill that file? I have zero experience with Dos and will go on the web to search out commands. The file I'm thinking of Killing is in my "j" drive. So I'm thinking I need to do something like SHELL j:GDProblimaticFile\Kill?
  All these years and I have never had to access Dos - I'm hoping the problem is in the data flow.
Thanks again guys

It looks to me like you, Dimster, think DOS or SHELL has a KILL command, but KILL is only a BASIC command that works only inside BASIC. DELETE is the command for removing files in a SHELL.

And as Pete said, it is possible you hadn't closed the file, so it wont KILL from BASIC nor DELETE from SHELL until file is closed.
« Last Edit: July 12, 2019, 09:46:58 am by bplus »

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Deleting Files
« Reply #10 on: July 12, 2019, 10:19:02 am »
No the file was absolutely closed bplus before I ventured into the Shell/Kill unknown universe - but you are absolutely correct, I completely misunderstood the Kill command being a basic statement v's Shell/Dos. In the past I have always use the "for Output" in situations where new data has changed the relevant values stored in a file which is relied on for calculations within the program.