What are all the ways to traverse directory trees?

后端 未结 7 1040
挽巷
挽巷 2021-01-02 04:13

How do you traverse a directory tree in your favorite language?

What do you need to know to traverse a directory tree in different operating systems? On different f

相关标签:
7条回答
  • 2021-01-02 04:14

    mmmm, C# with a dose of recursion.....

    public static List<string> CrawlPath(string path, bool IncludeSubFolders)
    {
        List<string> fileList = new List<string>();
        try
        {
            Stack<string> filez = new Stack<string>(Directory.GetFiles(path));
            while (filez.Count > 0)
            {
                fileList.Add(filez.Pop());
            }
    
            if (IncludeSubFolders)
            {
                filez = new Stack<string>(Directory.GetDirectories(path));
                while (filez.Count > 0)
                {
                    string curDir = filez.Pop();
                    fileList.AddRange(CrawlPath(curDir, IncludeSubFolders));
                }
            }
         }
         catch (System.Exception err)
         {
             Console.WriteLine("Error: " + err.Message);
         }
         return fileList;
      }
    
    0 讨论(0)
  • 2021-01-02 04:16

    In Python:

    If you're looking for a quick, clean, and portable solution try:

    import os
    base_dir = '.'
    
    def foo(arg, curr_dir, files):
      print curr_dir
      print files
    
    os.path.walk(base_dir, foo, None)
    

    Note that you can modify foo to do something else instead of just printing the names. Furthermore, if you're interested in migrating to Python 3.0, you will have to use os.walk() instead.

    0 讨论(0)
  • 2021-01-02 04:24

    On Linux with GNU tools

    find -print0 | xargs -0 md5sum
    

    or

    find -print0 | xargs -0 -iASD echo 'this file "ASD" should be dealt with lile this (ASD)'
    
    0 讨论(0)
  • 2021-01-02 04:35

    C++

    #include <utility>
    #include <boost/filesystem.hpp>
    #include <boost/foreach.hpp>
    
    #define foreach BOOST_FOREACH
    namespace fs = boost::filesystem;
    
    fs::recursive_directory_iterator it(top), eod;
    foreach (fs::path const & p, std::make_pair(it, eod)) {
        if (is_directory(p)) {
            ...
        } else if (is_regular_file(p)) {
            ...
        } else if (is_symlink(p)) {
            ...
        }
    }
    
    0 讨论(0)
  • 2021-01-02 04:37

    In Java:

    Recursion is useful here. Following is a Java code snippet that's been all over the Internet for ages. Not sure who deserves the credit for it.

    // Process all files and directories under dir
    
        public static void visitAllDirsAndFiles(File dir) {
    
            process(dir);  //do something useful with the file or dir
    
            if (dir.isDirectory()) {
                String[] children = dir.list();
                for (int i=0; i<children.length; i++) {
                    visitAllDirsAndFiles(new File(dir, children[i]));
                }
            }
        }
    
    0 讨论(0)
  • bash:

    #!/bin/bash
    
    function walk_tree {
          echo "Directory: $1"
          local directory="$1"
          local i
          for i in "$directory"/*; 
          do
          echo "File: $i"
            if [ "$i" = . -o "$i" = .. ]; then 
                continue
            elif [ -d "$i" ]; then  # Process directory and / or walk-down into directory
                # add command here to process all files in directory (i.e. ls -l "$i/"*)
                walk_tree "$i"      # DO NOT COMMENT OUT THIS LINE!!
            else
                continue    # replace continue to process individual file (i.e. echo "$i")
            fi
          done
    }
    
    walk_tree $HOME
    

    (adapted from http://ubuntuforums.org/showthread.php?t=886272 Comment #4)

    0 讨论(0)
提交回复
热议问题