Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Recursion for deleting
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Carey




PostPosted: Thu Apr 19, 2007 9:36 am   Post subject: Recursion for deleting

Having a bit of difficulty making a procedure that deletes all the dirs and files within a dir. Heres my code so far. Sorry about the lack of commenting and the messyness.

Turing:

var temp : string
var streamNumber : int

function slash : string
    if length (Dir.Current) = 3 then  %if you are at the root of a drive
        result ""
    else
        result "/"
    end if
end slash

procedure delete2 (path : string)
    Dir.Change (path)
    temp := ""
    streamNumber := Dir.Open (path)
    assert streamNumber > 9
    loop
        temp := Dir.Get (streamNumber)
        exit when temp = ""
        if temp not= "." and temp not= ".." then
            if Dir.Exists (temp) then
                delete2 (Dir.Current + slash + temp)
            else
                File.Delete (temp)
            end if
        end if
    end loop
    Dir.Close (streamNumber)
    Dir.Delete (path)
    streamNumber := Dir.Open ("..")
end delete2

Just call delete2 with the folder you want to delete as the parameter to test.
Sponsor
Sponsor
Sponsor
sponsor
ericfourfour




PostPosted: Thu Apr 19, 2007 4:25 pm   Post subject: RE:Recursion for deleting

All of that code can be replaced with Dir.Delete (path).
Carey




PostPosted: Mon Apr 23, 2007 6:57 am   Post subject: Re: Recursion for deleting

In what version of Turing? Ive got 4.0.5. is it different in 4.1?

Turing 4.0.5 Help wrote:

Dir.Delete is used to delete the directory specified by the parameter directoryPathName. This is the equivalent of doing a rmdir in DOS or UNIX. On the Macintosh, it removes a folder.

Dir.Delete will fail if it attempts delete a directory that has files in it.
Carey




PostPosted: Wed May 02, 2007 9:06 am   Post subject: Re: Recursion for deleting

Sorry about the double post, but does anybody have any suggestions? Or have i finally stumped the compsci.ca members? Very Happy
Cervantes




PostPosted: Sat May 05, 2007 2:01 pm   Post subject: Re: Recursion for deleting

Oh no, we're not stumped. Never!

Here you go. Feel free to ask questions.
Turing:

% don't give a final slash ("/") to the parameter for this procedure
procedure del_dir (path : string)
    var stream := Dir.Open (path)
    loop
        var next := Dir.Get (stream)
        exit when next = ""
        if File.Exists (path + "/" + next) then     % it's a file!
            File.Delete (path + "/" + next)

        % it's a directory! (but don't act on the current or parent directory)
        elsif next ~= "." and next ~= ".." then
            del_dir (path + "/" + next)
            Dir.Delete (path + "/" + next)
        end if
    end loop
    Dir.Close (stream)
    Dir.Delete (path)
end del_dir

del_dir ("C:/test")
BenLi




PostPosted: Sat May 05, 2007 9:46 pm   Post subject: RE:Recursion for deleting

potentially able to be put in bad use?
Cervantes




PostPosted: Sat May 05, 2007 10:51 pm   Post subject: RE:Recursion for deleting

Potentially, yes. But it's a very good example of recursion; a good learning experience.
Carey




PostPosted: Mon May 07, 2007 9:20 am   Post subject: Re: Recursion for deleting

Thanks guys. I see what i did wrong now. I knew that stumped comment would get me some replys!! Very Happy
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: