Delete all directory and files on disc
Delete all directory and files on disc
Hi!
I want to know without formatting, delete all files and directory on a disc (or directory)
Thank
I want to know without formatting, delete all files and directory on a disc (or directory)
Thank
Re: Delete all directory and files on disc
- del *
- SHELL("del *")
-
- Posts: 1022
- Joined: Nov 24, 2011 19:49
- Location: France
- Contact:
Re: Delete all directory and files on disc
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%
Re: Delete all directory and files on disc
Thank you
i know del *.* or * etc.. but i can't delete all directories, files and full subdirectories
Roland
I have this :

«CACA» contains files and directories and subdirectories
I use FreeDos *
i know del *.* or * etc.. but i can't delete all directories, files and full subdirectories
Roland
I have this :

«CACA» contains files and directories and subdirectories
I use FreeDos *
-
- Posts: 1022
- Joined: Nov 24, 2011 19:49
- Location: France
- Contact:
Re: Delete all directory and files on disc
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 ?
And why not the FreeDOS deltree command ?
Re: Delete all directory and files on disc
Ho!
I'm stupid
deltree I had not thought !
But if people found a freebasic code, this interest me ! :)
Thank you
I'm stupid
deltree I had not thought !
But if people found a freebasic code, this interest me ! :)
Thank you
Re: Delete all directory and files on disc
> 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.
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.
Re: Delete all directory and files on disc
Ho thank you very much ! :)
Re: Delete all directory and files on disc
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.
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.
Re: Delete all directory and files on disc
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()
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.
-
- Posts: 1022
- Joined: Nov 24, 2011 19:49
- Location: France
- Contact:
Re: Delete all directory and files on disc
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 :
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 ?
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
Re: Delete all directory and files on disc
Yes, it is. Good proposal!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 ?
And I forgot to remove the directories (fixed now in the code above).
Sorry, for me this sentence is ambiguous. Does the compiler crash or the compiled executable?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.
I didn't test the executable, but compiling with fbc-0.24 is fine here (even with option -exx).
-
- Posts: 1022
- Joined: Nov 24, 2011 19:49
- Location: France
- Contact:
Re: Delete all directory and files on disc
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) :TJF wrote:Sorry, for me this sentence is ambiguous. Does the compiler crash or the compiled executable?
Has someone else tested the program ?Aborting due to runtime error 12 ("segmentation violation" signal) in delete_all_test.bas::DELETE_ALL()
Re: Delete all directory and files on disc
Thanks for testing and reporting!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()
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.