- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Only some of them would claim to know the language this is written in and therefore ought to know the standard library of that language.
Admin
You don't work for Dow Chemical by any chance?
Admin
Okay, all the recursion and Directory.Delete(Path, true); comments not withstanding, there is another approach that is strictly iterative and doesn't require a deep internal stack. I'm not suggesting this is the way to solve the problem, but if you want to think outside the box, it at least lets you see one more side of the box. Here goes:
Loop through the directory, deleting files as you find them. When you find a directory, move all the files and directories it contains to the top level, then delete the now empty sub directory. Repeat until there's nothing in the top directory.
But don't hire me for this job. Please.
Admin
I can't believe that nobody has pointed out that 640 levels of directory should be enough for anybody.
Admin
For an example of comment recursion see here.
Admin
Am I the only one absolutely terrified by the thought that this can really happen?
Admin
That's still recursive. Your base case is when there are no more files.
The only difference between infinite loop recursion and method based recursion is that method based is cleaner code.
Admin
The good thing about recursing directories is you can create deeply nested directories with the last one named after a return address and shell code binary dump.
Admin
claps +1
Admin
How can it be, for a programmer ?
Admin
This is a good demonstration of recursion for those unfamiliar.
Admin
Close but not even a cigarette. The three numbers identfied by PROPER mathematicians are "zero, one, infinity". (see "Bluffers Guide to Maths".)
Admin
Bah. Stupid BBCode stuff. Wasn't thinking.
Recursion at work.
Also, for those complaining about the performance of recursion: recursive programming can be much more easily optimized to run across multiple threads and processes. That's why languages like Haskell and Erlang are gaining prominence for certain applications.
Admin
Sounds to me that either your C# compiler sucks (because proper tail recursion is trivially converted into straight forward iteration/jumps, which should be very comparable to an inline version) or what you have isn't something that isn't properly tail recursive (in which case, hand rolled solutions that avoid all the stack manipulation will certainly kick a$$, because the compiler will be able to do very little).
Admin
Admin
Captcha: laoreet - is that similar to a meerkat?
Admin
Admin
Admin
That's not thinking outside the box. This is thinking outside the box:
Admin
It worries me that the programmer would not think to use recursion (or any other file tree walk).
It worries me more that the programmer thinks four levels of subdirs is enough, because that means he never uses more than four levels.
(CAPTCHA: What I do after I gotcha.)
Admin
But I'm not a mathematician, proper or otherwise. And I said "many", not "infinity".
Admin
Which is why recursion is the instinctive track, but I'm pretty sure that the filesystem objects allow one to say "Delete all files that meet this criteria: beneath this root directory and not, itself, a directory."
If I can do it from the command line, there must be an API call that can do it, and if there isn't, I can call out to the command line (which probably has less overhead than doing it recursively, but that's neither here nor there). The definite wrong answer is maintaining the stack in code and code alone.
Admin
Admin
That's true... It was invented by an indian guy who saw that we had numbers 1-9, and thought that none of those things was something that he wanted to pay for whatever he was buying, so he invented the 0.
Admin
To state the obvious middle-of-the-road position: everyone who sees this problem should know how to code it recursively (or with a manual stack), and everyone who seems this problem should code it by using whatever the library provides.
Put another way, libraries do not excuse ignorance of important programming techniques, and cleverness/education does not excuse use of a library when available.
I think this is a distinction lost on many.
Admin
Not very elegant
Admin
Agree about code reviews. That and a good version control system. Don't authorize them to check their changes into the release stream without first having it reviewed by at least one other developer.
However, I hate "code review meetings". We did this at my last company. Everyone literally showed up in a room with a stack of printouts and we went over it line by line as a group. It could take all day.
There are better ways. My company uses a tool called CodeCollaborator. You pull up the diffs, flag any issues, write your comments write next to the offending code. The author can provide explanatory comments or submit fixes for re-review. You do it all from your own desk at your own pace. The author gets notified when everyone is done reviewing. I really like it.
Admin
Admin
Which is good, because a REAL PROPER mathematician would recognize that "many" can be a number, but "infinity" is not (it's a useful concept, and you can play with it as a mathematical object, and even compare it to things like other infinities... but it isn't a number in the normal sense).
Admin
The Real WTF is that after years of this site, and dozens of posts in this thread people still think that recursion* jokes are funny.
*You can also substitute the definitions that people put in for their captcha.
Admin
Slacker early men counted "one, two, many, one-one, one-two, one-many, two-one, two-two... too many, I'm tired, let's go hit a bong"
Captcha: enim. Eminem, without the ego.
Admin
This is psuedocode so don't complain that it doesn't compile. It's non-recursive. note pwd == present working directory.
function DeleteFileTree(TopRemoveDir) { // A directory tree is effectively arbitrarily recursive user data // This cannot be implemented safely by storing each step either // on the stack or in the heap because a hard disk is generally much // larger than available ram. Instead the standard OS commands of // "cd .." is used to backtrace so the recursiveness is handled by the operating system.
fileSystem.changeDir(TopRemoveDir); location = fileSystem.pwd(); if (location != TopRemoveDir) { // Strict sanity check input folder as this can be dangerouse.
} // Top folder Delete files filesystem.deleteFiles("*");
depth = 0; while (true) {
} }
Admin
Recursion jokes are funny if recursion jokes are funny.
Admin
That should be nixed.
Admin
removeFiles(dirName) { dir.delete files for each subDir { removeFiles(subDir) } }
Admin
removeFiles(dirName) { dir.delete files for each subDir { removeFiles(subDir) } }
Because an anti-social person will nest directory structures until your program stack overflows and runs arbitrary code.
Actually my code contains a bug as well it should have been something like:
EmptyFolder = fileSystem.currentBottomDirOnly();
You don't want to store the entire directory path in RAM.
Admin
Depends on your language. If it's doing tail recursion, you'll never get a stack overflow. If it's a bytecode based or interpreted language, the runtime will handle it.
Basically: don't do that in C.
Admin
If you don't think that recursion jokes are funny, how in the world did you end up with a job in software development? What do you do for entertainment if not make up jokes about recursion and "byte" and "big Endian"? What, do you go to parties with members of the opposite sex or something crazy like that?
Admin
Not sure why the itch to post ad nauseum today.
Recursion jokes are funny. ENDLESS recursion jokes are not funny.
I remember back in the day that I hacked the FAT on one of my floppies so that a directory was its own subdirectory. Confused the heck out of my friends. "Graphical" tools blew chunks while the command line still worked fine. Good times.
Ancient disk hacking stories are amusing. ENDLESS disk hacking jokes are not amusing.
Admin
That's right; some silicon, plastic, various metals and a couple of lemons oughta be enough for anyone.
Admin
Admin
http://xkcd.com/703/
Admin
my brain hurts
Admin
actually it's ad nauseAm
Admin
Comment reviews.
-Harrow.
Admin
I know that when I studied ancient greek, I found out that they basically could count to 10000. Their word for 10000 and anything beyond that was μυρια (Myriad). If it was far beyond 10000 they might use μυρια μυρια. Only Archimedes is known to have had an interest in numbers larger than μυρια and tried to invent a way to count them.
I've always wondered how it would be to live in a world where large numbers just aren't needed.
Admin
That might be a bit much.
Admin
This reminds me of the zip quine.
Warning: some zip-scanning virus scanners seem to get an indigestion from this file; I wonder why?
Admin
While the virtual need for huge numbers is obvious, there really isn't an actual need for large numbers even in today's society. Most people don't comprehend a trillion, or even a million. If you ask them what a million is, most (Americans at least) will think of a million dollars, even though most don't know what a million dollars looks like or even what they would do with it.
But what if it were a million marbles? What is the mass and volume of a million tightly packed marbles? Would it fit in my upstairs bathroom? What's the monetary value, i.e. would it be worth acquiring and reselling a million marbles? I would estimate 99%+ couldn't grasp a single one of these concepts.
Admin
Washington, DC?