#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Posts
    34
    Rep Power
    13

    How do i loop through all files in a directory?


    Can someone help me, How can I loop through all of the files in a directory. Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Posts
    34
    Rep Power
    13
    Or get their name atleast
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Posts
    34
    Rep Power
    13
    How do i loop through files in a directory? Posted on: 07/13/2003 13:48:02


    Here are two *examples* (recursive and non-recursive).
    Alternatively, you may use DirectoryInfo/FileInfo.

    public static void Perform(String directoryName) {
    String[] fileNames=Directory.GetFiles(directoryName);
    for (Int32 i=0;i<fileNames.Length;++i) {
    // Do something.
    Console.WriteLine(fileNames[i]);
    }
    String[] directories=Directory.GetDirectories(directoryName);
    for (Int32 i=0;i<directories.Length;++i) {
    Perform(directories[i]);
    }
    }

    public static void Perform(String directoryName) {
    Stack stack=new Stack();
    stack.Push(directoryName);
    while (stack.Count>0) {
    String directory=stack.Pop() as String;
    String[] fileNames=Directory.GetFiles(directory);
    for (Int32 i=0;i<fileNames.Length;++i) {
    // Do something.
    Console.WriteLine(fileNames[i]);
    }
    String[] directories=Directory.GetDirectories(directory);
    for (Int32 i=directories.Length-1;i>=0;--i) {
    stack.Push(directories[i]);
    }
    }
    }


    from boss another board, thanks.

IMN logo majestic logo threadwatch logo seochat tools logo