• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is it possible to delete all files and folders in a drive non-recursively?  RSS feed

 
umasankar puranam
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that all files and directories and all subdirectories under a drive can be deleted programatically using recurssion. Is it possible to do the same non-recursively? If yes, please give the sample code or if not possible state the reason?

I faced this question in an interview and I said its not possible to delete non-recursively? Am I currect?
 
Jan Cumps
Bartender
Posts: 2632
17
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The File class only allows you to delete empty dirs, so yes: this is typically done by recursively visiting lower directories and emptying them.
But you could also start a native operation system command from java that removes a dir that is not empty and its subdirs (e.g. rm -fr in unix). That would be a non-recursive solution.
 
Dave Lorde
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In principle, any recursive procedure can be achieved non-recursively, by using an explicit stack instead of the implicit stack of recursion.

See Convert Recursive to Non-recursive.
 
umasankar puranam
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dave Lorde,

Could you please give me sample non-recursive code in Java to delete all files, directories, and sub-directories in a drive?
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Cumps wrote:(e.g. rm -fr in unix). That would be a non-recursive solution.

I wouldn't be too sure. The recursion may not be in your Java program, but it will probably be moved to the native program.
 
umasankar puranam
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob Prime,

I need a solution not by running an unix command, I need it using java code? Could you, or anybody in the forum please provide sample java code for doing this?
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you want to use recursion? For any tree-like structure, including files and folders, recursion is the obvious and easiest mechanism.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Why don't you want to use recursion? For any tree-like structure, including files and folders, recursion is the obvious and easiest mechanism.


Agreed. Most of the time recursion is avoided (assuming that its the best option) because the algorithm doesn't have an upper capacity in memory usage. In this case, I highly doubt you will encounter a case where the directory structure is so deep that you'll run out of heap.... And it's also the best option too.

Henry
 
Jan Cumps
Bartender
Posts: 2632
17
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:I wouldn't be too sure. The recursion may not be in your Java program, but it will probably be moved to the native program.
True. I should have read the man page for the -r option:
-r, -R, --recursive remove the contents of directories recursively

It's a giveaway, isn't it?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sorry... didn't read the question correctly. It's an interview question. Anyway....


Having an example on how to delete a directory, in a non-recursive manner, misses the point. Remember back to your old algorithms class? when you first learned about recursion? Remember all that work that you did prior to using recursion, with stack based data structures, to do stuff, that became really easy once you added recursion to your arsenal?

That's the point of the interview question. The interview was likely testing you to see if you understood the basics of recursion, which includes an understanding on how to do recursion, without the language making it so easy for you to do it -- to simulate it using data structures.

Henry
 
Dave Lorde
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
umasankar puranam wrote:Hi Dave Lorde,

Could you please give me sample non-recursive code in Java to delete all files, directories, and sub-directories in a drive?


Hi Umasankar Puranam,

No, I couldn't. It is possible but pointless. If you would like to do it for yourself, by all means go for it, I'm sure you'd learn a lot. Try it with a very simple example to start.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!