• (cs) in reply to HurrDurr
    HurrDurr:
    Holy crap. All the people who say "recursion" in this thread need to get fiiiiiired.

    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.

  • Anon (unregistered)

    You don't work for Dow Chemical by any chance?

  • Todd Lewis (unregistered)

    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.

  • (cs)

    I can't believe that nobody has pointed out that 640 levels of directory should be enough for anybody.

  • (cs) in reply to recursive ClaudeSuck
    recursive ClaudeSuck:
    ClaudeSuck.de:
    Not everybody knows recursion

    Not everybody knows recursion

    Quoting someone and repeating what they said is not recursion, it's copying and pasting. Do you understand the difference between recursion and copy-pasting?

    For an example of comment recursion see here.

  • (cs)
    Every business in town – from the dry cleaners to the restaurants – is owned or subsidized by the company. Just about every resident works, worked, or will work for the company.

    Am I the only one absolutely terrified by the thought that this can really happen?

  • Ken (unregistered) in reply to Todd Lewis
    Todd Lewis:
    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.

    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.

  • greg (unregistered)

    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.

  • Ken (unregistered) in reply to Aaron
    Aaron:
    recursive ClaudeSuck:
    ClaudeSuck.de:
    Not everybody knows recursion

    Not everybody knows recursion

    Quoting someone and repeating what they said is not recursion, it's copying and pasting. Do you understand the difference between recursion and copy-pasting?

    For an example of comment recursion see here.

    claps +1

  • Fred (unregistered) in reply to ClaudeSuck.de

    How can it be, for a programmer ?

  • t3knomanser (unregistered) in reply to Aaron
  • Matt Westwood (unregistered) in reply to mike5
    mike5:
    Steve the Cynic:
    So Easy A Caveman Could Do It:
    The usual joke is that early man counted "one, two, many". After much education, many of us could count indefinitely and not tire. It's interesting to imagine whether that actually works against us: there's something for developers who know that, after two or more levels, you may as well generalize to "many".
    I guess that makes our interesting number sequence: zero, one, many.
    No, no, no! Zero came much, much later...

    Close but not even a cigarette. The three numbers identfied by PROPER mathematicians are "zero, one, infinity". (see "Bluffers Guide to Maths".)

  • t3knomanser (unregistered) in reply to t3knomanser

    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.

  • Brent (unregistered) in reply to TopCod3r
    TopCod3r:
    But I'm not a big fan of tail recursion because it has some serious performance problems in C#. I once ran a benchmark of using tail recursion versus inline and inline kicked a$$ versus recursion.

    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).

  • FIA (unregistered) in reply to mike5
    mike5:
    Steve the Cynic:
    So Easy A Caveman Could Do It:
    The usual joke is that early man counted "one, two, many". After much education, many of us could count indefinitely and not tire. It's interesting to imagine whether that actually works against us: there's something for developers who know that, after two or more levels, you may as well generalize to "many".
    I guess that makes our interesting number sequence: zero, one, many.
    No, no, no! Zero came much, much later...
    Wasn't zero outsourced to India? I hear they did a pretty good job.
  • OldCoder (unregistered) in reply to ObiWayneKenobi
    ObiWayneKenobi:
    Every business in town – from the dry cleaners to the restaurants – is owned or subsidized by the company. Just about every resident works, worked, or will work for the company.

    Am I the only one absolutely terrified by the thought that this can really happen?

    Eeeeasy. Think of any remote military base.

    Captcha: laoreet - is that similar to a meerkat?

  • (cs) in reply to HurrDurr
    HurrDurr:
    Holy crap. All the people who say "recursion" in this thread need to get fiiiiiired. Or their own WTF entry.

    FRAMEWORK.

    Frameworks are for pussies.

  • (cs) in reply to Ikkonoishi
    Ikkonoishi:
    Recursion isn't even needed here.

    FileInfo[] files = dir.GetFiles("", SearchOption.AllDirectories); foreach(FileInfo file in files) { file.Delete(); } DirectoryInfo[] subDirs = dir.GetDirectories("", SearchOption.AllDirectories); foreach(DirectoryInfo subDir in subDirs) { subDir.Delete(); }

    The original example did not delete the directories, it left the structure intact. Presumably either that was intentional, or an error. Assuming it was intentional, only the first half of you code would be needed...

  • (cs) in reply to Todd Lewis
    Todd Lewis:
    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.

    That's not thinking outside the box. This is thinking outside the box:

    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.FileName = "rd";
    startInfo.Arguments = @"C:\MyPath /s /q";
    startInfo.UseShellExecute = true;
    startInfo.WindowStyle = ProcessWindowStyle.Hidden;            
    Process p = Process.Start(startInfo);
    
    //10 seconds should be long enough ;)
    if(!p.WaitForExit(10000))
    {
        throw new TimeoutException("Oops.");
    }
    
    if(p.ExitCode != 0)
    {
        throw new BlaBlaBlaException("This crap didn't work!  Exit code:" + p.ExitCode);
    }
    
    
  • BentFranklin (unregistered)

    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.)

  • Steve the Cynic (unregistered) in reply to Matt Westwood
    Matt Westwood:
    mike5:
    Steve the Cynic:
    So Easy A Caveman Could Do It:
    The usual joke is that early man counted "one, two, many". After much education, many of us could count indefinitely and not tire. It's interesting to imagine whether that actually works against us: there's something for developers who know that, after two or more levels, you may as well generalize to "many".
    I guess that makes our interesting number sequence: zero, one, many.
    No, no, no! Zero came much, much later...

    Close but not even a cigarette. The three numbers identfied by PROPER mathematicians are "zero, one, infinity". (see "Bluffers Guide to Maths".)

    But I'm not a mathematician, proper or otherwise. And I said "many", not "infinity".

  • t3knomanser (unregistered) in reply to ircmaxell

    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.

  • (cs) in reply to Anon
    Anon:
    Recursion

    re.curs.ion

    Definition:

    See recursion

    Brainsplosion

  • BF (unregistered) in reply to mike5
    mike5:
    Steve the Cynic:
    So Easy A Caveman Could Do It:
    The usual joke is that early man counted "one, two, many". After much education, many of us could count indefinitely and not tire. It's interesting to imagine whether that actually works against us: there's something for developers who know that, after two or more levels, you may as well generalize to "many".
    I guess that makes our interesting number sequence: zero, one, many.
    No, no, no! Zero came much, much later...

    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.

  • So Easy A Caveman Could Do It (unregistered) in reply to Zylon
    Zylon:
    HurrDurr:
    Holy crap. All the people who say "recursion" in this thread need to get fiiiiiired. Or their own WTF entry.

    FRAMEWORK.

    Frameworks are for pussies.

    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.

  • ClaudeSuck.de (unregistered) in reply to Resuna
    Resuna:
    ClaudeSuck.de:
    Not everybody knows recursion
    1. If you don't know recursion you shouldn't be working as a software developer.

    2. This doesn't actually need recursion, you can maintain a directory stack manually.

    Not very elegant

  • (cs) in reply to True that
    True that:
    Two words...

    Code reviews.

    Trot that pony out in front of the whole development team (in fact, the whole team should be trotting their ponies out at a regularly scheduled meeting) as a change to redirect, refactor and (sadly enough) educate even your best developer.

    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.

  • Quirkafleeg (unregistered)
    rm -rf /
  • Brent (unregistered) in reply to Steve the Cynic
    Steve the Cynic:
    Matt Westwood:
    Close but not even a cigarette. The three numbers identfied by PROPER mathematicians are "zero, one, infinity". (see "Bluffers Guide to Maths".)

    But I'm not a mathematician, proper or otherwise. And I said "many", not "infinity".

    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).

  • Hatterson (unregistered)

    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.

  • Roo (unregistered) in reply to tovarich
    tovarich:
    Smart early men counted "one, two, many... many-one, many-two, many-many..." etc.

    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.

  • greg barton (unregistered) in reply to Zylon

    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.

    return;
    

    } // Top folder Delete files filesystem.deleteFiles("*");

    depth = 0; while (true) {

    // descend into first directory only, as it is going to  be removed the first directory 
    // can also be considered the next.
    
    SubDir = fileSystem.GetDirectories();			// Get iterator for directories in pwd.
    if (SubDir.hasNext())
    {
    	fileSystem.changeDir("./" + SubDir.next());
    	depth++;
    	filesystem.deleteFiles("*");
    }
    else
    {
    	if (depth > 0)
    	{
    		// Reached the maximum depth for this directory path so back up and remove this folder
    
    		EmptyFolder = string.lastToken(fileSystem.pwd(), "/");    // don't worry token parser properly escapes "/" in folder name.
    		fileSystem.changeDir("..");
    		fileSystem.removeDir(EmptyFolder);
    		depth--;
    	}
    	else
    	{
    		// Finished descending now back to where started so all done
    
    		break;
    	}
    }
    

    } }

  • t3knomanser (unregistered) in reply to Hatterson

    Recursion jokes are funny if recursion jokes are funny.

  • (cs) in reply to Quirkafleeg

    That should be nixed.

  • Hatterson (unregistered) in reply to greg barton
    greg barton:
    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.

    return;
    

    } // Top folder Delete files filesystem.deleteFiles("*");

    depth = 0; while (true) {

    // descend into first directory only, as it is going to  be removed the first directory 
    // can also be considered the next.
    
    SubDir = fileSystem.GetDirectories();			// Get iterator for directories in pwd.
    if (SubDir.hasNext())
    {
    	fileSystem.changeDir("./" + SubDir.next());
    	depth++;
    	filesystem.deleteFiles("*");
    }
    else
    {
    	if (depth > 0)
    	{
    		// Reached the maximum depth for this directory path so back up and remove this folder
    
    		EmptyFolder = string.lastToken(fileSystem.pwd(), "/");    // don't worry token parser properly escapes "/" in folder name.
    		fileSystem.changeDir("..");
    		fileSystem.removeDir(EmptyFolder);
    		depth--;
    	}
    	else
    	{
    		// Finished descending now back to where started so all done
    
    		break;
    	}
    }
    

    } }

    I fail to see why you would do that over this

    removeFiles(dirName) { dir.delete files for each subDir { removeFiles(subDir) } }

  • greg barton (unregistered) in reply to Hatterson

    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.

  • t3knomanser (unregistered) in reply to greg barton

    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.

  • Jay (unregistered) in reply to Hatterson
    Hatterson:
    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.

    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?

  • So Easy A Caveman Could Do It (unregistered)

    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.

  • (username *)me (unregistered) in reply to Zylon
    Zylon:
    HurrDurr:
    Holy crap. All the people who say "recursion" in this thread need to get fiiiiiired. Or their own WTF entry.

    FRAMEWORK.

    Frameworks are for pussies.

    That's right; some silicon, plastic, various metals and a couple of lemons oughta be enough for anyone.

  • J. Random PMP (unregistered) in reply to anon
    anon:
    Every business is owned or subsidized by the company? Where does he live, Naples?
    I'm guessing Peoria, although I imagine there are other such places.
  • (cs) in reply to t3knomanser
    t3knomanser:
    Recursion jokes are funny if recursion jokes are funny.

    http://xkcd.com/703/

  • Mike (unregistered) in reply to Anon
    Anon:
    Recursion

    re.curs.ion

    Definition:

    See recursion

    my brain hurts

  • doh (unregistered) in reply to So Easy A Caveman Could Do It
    So Easy A Caveman Could Do It:
    Not sure why the itch to post ad nauseum today.

    actually it's ad nauseAm

  • Harrow (unregistered) in reply to True that
    True that:
    True that:
    Two words...

    Code reviews.

    Trot that pony out in front of the whole development team (in fact, the whole team should be trotting their ponies out at a regularly scheduled meeting) as a change to redirect, refactor and (sadly enough) educate even your best developer.

    Captcha: delenit (timely that)

    Oh bugger... make that chance, not change. More coffee!!

    Two words...

    Comment reviews.

    -Harrow.

  • (cs) in reply to Jay

    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.

  • Mike (unregistered) in reply to Quirkafleeg
    Quirkafleeg:
    rm -rf /

    That might be a bit much.

  • just me (unregistered) in reply to So Easy A Caveman Could Do It
    So Easy A Caveman Could Do It:
    ...

    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.

    ...

    This reminds me of the zip quine.

    Warning: some zip-scanning virus scanners seem to get an indigestion from this file; I wonder why?

  • Mike (unregistered) in reply to RogerWilco
    RogerWilco:
    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.

    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.

  • Crash Magnet (unregistered) in reply to J. Random PMP
    J. Random PMP:
    anon:
    Every business is owned or subsidized by the company? Where does he live, Naples?
    I'm guessing Peoria, although I imagine there are other such places.

    Washington, DC?

Leave a comment on “Should Be Enough”

Log In or post as a guest

Replying to comment #:

« Return to Article