Computer Science Canada

Traversing Directories

Author:  Carey [ Thu Apr 17, 2008 10:09 am ]
Post subject:  Traversing Directories

Im making a tagging program (like windows search on 'roids) and i need help on how to add multiple files.

Turing example:

Turing:
var temp : string
procedure addDir (path : string)
    var streamNumber := Dir.Open (path)
    loop
        temp := Dir.Get (streamNumber)
        exit when temp = ""
        if File.Exists (path + "/" + temp) then
            addFile (path + "/" + temp) %add the file
        elsif temp not= "." and temp not= ".." then
            addDir (path + "/" + temp)
        end if
    end loop
    Dir.Close (streamNumber)
    Dir.Delete (path)
end addDir


any help would be muchly appreciated

Author:  Carey [ Fri Apr 18, 2008 11:44 am ]
Post subject:  RE:Traversing Directories

anyone?

Author:  Tony [ Fri Apr 18, 2008 12:03 pm ]
Post subject:  RE:Traversing Directories

eh.. call addFile multiple times? It's not clear what your question is.

Author:  Carey [ Fri Apr 18, 2008 12:15 pm ]
Post subject:  RE:Traversing Directories

I want to go through a directory recursively, adding every file in it and going into every directory. my problem is that i don't know how to get the names and paths of every file and sub-directory inside the initial directory passed to the method. tell me if i'm not making sense.

Author:  Tony [ Fri Apr 18, 2008 12:36 pm ]
Post subject:  RE:Traversing Directories

Dir.Get returns the "next" item in the directory. If it's a file (File.Exists), you add it to the list. Otherwise you assume it's a directory, and recursively examine that. The sample code already does just this
Turing:

procedure addDir (path : string)
   ...
   addDir (path + "/" + temp)
   ...
end addDir

Author:  Carey [ Fri Apr 18, 2008 12:40 pm ]
Post subject:  RE:Traversing Directories

I know that. I need help on porting this method into Java. I don't know how to get the paths and such of all the items in a directory in Java. i've searched the online help file and tutorials but i haven't found anything. I just gave the Turing example to clarify what i need to accomplish.

Author:  Tony [ Fri Apr 18, 2008 1:37 pm ]
Post subject:  RE:Traversing Directories

Ohh... I haven't noticed that this was in Java. Sorry. What do Java Docs say about the File class?

Author:  Carey [ Mon Apr 21, 2008 10:48 am ]
Post subject:  RE:Traversing Directories

hmmmmm. I think i have it
Java:
public void addDir(String path, boolean auto){
        String[] s = new File(path).list();
        for (int i = 0 ; i < s.length ; i++){
            File f = new File(s[i]);
            if (f.isDirectory())
                addDir(f.getAbsolutePath(), auto);
            else if (f.isFile())
                addFile(f.getAbsolutePath(), auto);
        }
    }



hmmmmm. doesn't seem to work. any ideas?

crap. [slaps forehead] im an idiot. this is a working version:

Java:

public void addDir(String path, boolean auto){
        File[] f = new File(path).listFiles();
        for (int i = 0 ; i < f.length ; i++){
            if (f[i].isDirectory())
                addDir(f[i].getAbsolutePath(), auto);
            else if (f[i].isFile())
                addFile(f[i], auto);
        }
    }


my question now is:

What is Java's definition of a [i]normal file
. Is a hidden file normal?

Quote:
A file is normal if it is not a directory and, in addition, satisfies other system-dependent criteria.


Do these system-dependent criteria specify that it has to be visible(not hidden)?


: