Delete all directory and files on disc

DOS specific questions.
Cpcdos
Posts: 207
Joined: Mar 06, 2013 13:52
Location: France - LYON 69003
Contact:

Delete all directory and files on disc

Post by Cpcdos »

Hi!

I want to know without formatting, delete all files and directory on a disc (or directory)

Thank
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Delete all directory and files on disc

Post by TJF »

  • del *
or in FB
  • SHELL("del *")
fxm
Moderator
Posts: 12550
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Delete all directory and files on disc

Post by fxm »

del *.*
Roland Chastain
Posts: 1022
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Delete all directory and files on disc

Post by Roland Chastain »

Another proposition.

Code: Select all

@echo off

rem DelTree.bat

rem Deletes in a specified directory all subdirectories including any files
rem and then deletes the directory itself.

set root="c:\users\roland\thedirectory"

for /d /r %root% %%f in (*) do rd "%%f" /s /q

rd %root%
Cpcdos
Posts: 207
Joined: Mar 06, 2013 13:52
Location: France - LYON 69003
Contact:

Re: Delete all directory and files on disc

Post by Cpcdos »

Thank you
i know del *.* or * etc.. but i can't delete all directories, files and full subdirectories


Roland
I have this :
Image

«CACA» contains files and directories and subdirectories

I use FreeDos *
Roland Chastain
Posts: 1022
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Delete all directory and files on disc

Post by Roland Chastain »

Since you don't use a batch file, but type in the command directly, maybe it's %f and "%f" (instead of %%f and "%%f"), but I am not sure.

And why not the FreeDOS deltree command ?
Cpcdos
Posts: 207
Joined: Mar 06, 2013 13:52
Location: France - LYON 69003
Contact:

Re: Delete all directory and files on disc

Post by Cpcdos »

Ho!
I'm stupid
deltree I had not thought !

But if people found a freebasic code, this interest me ! :)


Thank you
DOS386
Posts: 798
Joined: Jul 02, 2005 20:55

Re: Delete all directory and files on disc

Post by DOS386 »

> But if people found a freebasic code, this interest me !

You just have to copy the behavior of FreeDOS DELTREE tool, look at http://www.freebasic.net/wiki/wikka.php?wakka=KeyPgDir DIR function. Run through the directory and inspect the "D" attribute of every found item, if file, delete it (try KILL or ASSASSINATE command), if directory, process it recursively, after you emptied a direcotry, you can delete it using RMDIR command. Note: with DOS target of FB, the KILL command apparently is able to delete even files having the "R" attribute set and empty subdirectories too (http://freebasic.net/forum/viewtopic.php?t=13128), but it definitely won't delete subdirectories with all content - you must do the recursive processing yourself.
Cpcdos
Posts: 207
Joined: Mar 06, 2013 13:52
Location: France - LYON 69003
Contact:

Re: Delete all directory and files on disc

Post by Cpcdos »

Ho thank you very much ! :)
marcov
Posts: 3503
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: Delete all directory and files on disc

Post by marcov »

Doesn't deleting files from a directory you are findfirsting make the ongoing findfirst unreliable? I'm not 100% sure how dos reacts to that.

If that is the case, do a findfirst only recording filenames till memory is full, then delete them all, and then do findfirst again.

That takes keeping track of some things though.
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Delete all directory and files on disc

Post by TJF »

Cpcdos wrote:But if people found a freebasic code, this interest me ! :)

Code: Select all

#INCLUDE ONCE "dir.bi"

#IFDEF __FB_UNIX__
#DEFINE SLASH "/"
#ELSE
#DEFINE SLASH "\"
#ENDIF
#DEFINE NL !"\n"

SUB delete_all()
  VAR n = DIR("*", fbDirectory), t = ""
  WHILE LEN(n)
    IF n <> "." ANDALSO n <> ".." THEN t &= n & NL
    n = DIR()
  WEND

  VAR a = 1, e = a, l = LEN(t)
  WHILE a < l
    e = INSTR(a, t, NL)
    n = MID(t, a, e - a)
    IF 0 = CHDIR(n) THEN
      delete_all()
      CHDIR ("..")
      RMDIR(n)
    END IF
    a = e + 1
  WEND

  n = DIR("*")
  WHILE LEN(n)
    KILL (n)
    n = DIR()
  WEND
END SUB

IF 0 = CHDIR("X:\") THEN _' adapt your drive (or directory) here
  delete_all()
[Edit]
RMDIR added.
Rolands improvements added.
IF 0 = CHDIR(n) added.
[/Edit]
Last edited by TJF on Dec 31, 2013 11:44, edited 4 times in total.
Roland Chastain
Posts: 1022
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Delete all directory and files on disc

Post by Roland Chastain »

Hello TJF !

Your code looks fine but, unfortunately, with me (fbc 0.90.0, Windows 8) it crashes.

When I compile with -exx option, I get a "segment violation signal". I will see if I manage to find what happens.

I suggest a little modification, for security reasons :

Code: Select all

IF CHDIR("C:\Atelier\Basic\DelTree\tree") = 0 then
  delete_all()
END IF
Indeed, if I make a typo in the folder name, it could happen that the program does its job with the current directory, instead of the expected one, isn't it ?
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Delete all directory and files on disc

Post by TJF »

Roland Chastain wrote:Indeed, if I make a typo in the folder name, it could happen that the program does its job with the current directory, instead of the expected one, isn't it ?
Yes, it is. Good proposal!

And I forgot to remove the directories (fixed now in the code above).
Roland Chastain wrote:When I compile with -exx option, I get a "segment violation signal". I will see if I manage to find what happens.
Sorry, for me this sentence is ambiguous. Does the compiler crash or the compiled executable?

I didn't test the executable, but compiling with fbc-0.24 is fine here (even with option -exx).
Roland Chastain
Posts: 1022
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Delete all directory and files on disc

Post by Roland Chastain »

TJF wrote:Sorry, for me this sentence is ambiguous. Does the compiler crash or the compiled executable?
Yes, it is ambiguous. I wanted to say that the executable crashes. As I saw that, I compiled with -exx in order to get more information. Here is the message I get (even with your last correction) :
Aborting due to runtime error 12 ("segmentation violation" signal) in delete_all_test.bas::DELETE_ALL()
Has someone else tested the program ?
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Delete all directory and files on disc

Post by TJF »

Roland Chastain wrote:Here is the message I get (even with your last correction) :
Aborting due to runtime error 12 ("segmentation violation" signal) in delete_all_test.bas::DELETE_ALL()
Thanks for testing and reporting!

I found the problem: scanning directories doesn't work properly on windows. When a filename has no extension, windows reports the file as a directory and the recursion runs endless.

You can fix it by checking the result of the CHDIR statement, see (fixed) code above.
Post Reply