2009-01-30 35 views
10

Làm thế nào để bạn duyệt qua một cây thư mục bằng ngôn ngữ yêu thích của bạn?Tất cả các cách để duyệt qua các cây thư mục là gì?

Bạn cần biết gì để đi qua cây thư mục trong các hệ điều hành khác nhau? Trên các hệ thống tập tin khác nhau?

Thư viện/mô-đun yêu thích của bạn để trợ giúp trong việc duyệt qua cây thư mục là gì?

+1

Tôi không downvote bạn, nhưng tôi nghĩ rằng tôi biết lý do tại sao ai đó đã làm - đây là một câu hỏi vô cùng mơ hồ, và doesn 't xuất hiện để giải quyết bất kỳ vấn đề cụ thể. –

+0

Đây là câu hỏi về chơi gôn. Bây giờ chúng ta có đang bỏ phiếu cho các môn chơi gôn không? –

+0

Erik, tôi chỉ đăng nó nghĩ rằng nó sẽ làm cho một "hit hàng đầu" tốt cho SO trong Google. – skiphoppy

Trả lời

3

Trong C#:

Stack<DirectoryInfo> dirs = new Stack<DirectoryInfo>(); 

dirs.Push(new DirectoryInfo("C:\\")); 

while (dirs.Count > 0) { 
    DirectoryInfo current = dirs.Pop(); 

    // Do something with 'current' (if you want) 

    Array.ForEach(current.GetFiles(), delegate(FileInfo f) 
    { 
     // Do something with 'f' 
    }); 

    Array.ForEach(current.GetDirectories(), delegate(DirectoryInfo d) 
    { 
     dirs.Push(d); 
    }); 
} 
6

Trong Java:

Đệ quy là hữu ích ở đây. Sau đây là một đoạn mã Java đã được tất cả trên Internet cho các lứa tuổi. Bạn không chắc chắn ai xứng đáng với tín dụng cho nó.

// 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])); 
      } 
     } 
    } 
7

Trong Python:

Nếu bạn đang tìm kiếm một giải pháp nhanh chóng, sạch sẽ, và cầm tay thử:

import os 
base_dir = '.' 

def foo(arg, curr_dir, files): 
    print curr_dir 
    print files 

os.path.walk(base_dir, foo, None) 

Lưu ý rằng bạn có thể sửa đổi foo để làm cái gì khác thay vì chỉ in tên. Hơn nữa, nếu bạn quan tâm đến việc chuyển sang Python 3.0, bạn sẽ phải sử dụng os.walk() để thay thế.

1

mmmm, C# với một liều lượng của đệ quy .....

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

Tôi nghĩ rằng chúng ta nên chạy thử các triển khai của chúng tôi: P –

+0

+1 cho "filez"() – RCIX

2

Trên Linux với công cụ GNU

find -print0 | xargs -0 md5sum 

hoặc

find -print0 | xargs -0 -iASD echo 'this file "ASD" should be dealt with lile this (ASD)' 
4

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 

(chuyển thể từ http://ubuntuforums.org/showthread.php?t=886272 Comment # 4)

3

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

Ngoài ra: http://pocoproject.org/docs/Poco.DirectoryIterator.html –

Các vấn đề liên quan