Best Way to Iterate Folders and Subfolders

Best way to iterate folders and subfolders

Use Directory.GetFiles(). The bottom of that page includes an example that's fully recursive.

Note: Use Chris Dunaway's answer below for a more modern approach when using .NET 4 and above.

// For Directory.GetFiles and Directory.GetDirectories
// For File.Exists, Directory.Exists
using System;
using System.IO;
using System.Collections;

public class RecursiveFileProcessor
{
public static void Main(string[] args)
{
foreach(string path in args)
{
if(File.Exists(path))
{
// This path is a file
ProcessFile(path);
}
else if(Directory.Exists(path))
{
// This path is a directory
ProcessDirectory(path);
}
else
{
Console.WriteLine("{0} is not a valid file or directory.", path);
}
}
}

// Process all files in the directory passed in, recurse on any directories
// that are found, and process the files they contain.
public static void ProcessDirectory(string targetDirectory)
{
// Process the list of files found in the directory.
string [] fileEntries = Directory.GetFiles(targetDirectory);
foreach(string fileName in fileEntries)
ProcessFile(fileName);

// Recurse into subdirectories of this directory.
string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach(string subdirectory in subdirectoryEntries)
ProcessDirectory(subdirectory);
}

// Insert logic for processing found files here.
public static void ProcessFile(string path)
{
Console.WriteLine("Processed file '{0}'.", path);
}
}

Iterate through folders, then subfolders and print filenames with path to text file

Use os.walk(). The following will output a list of all files within the subdirectories of "dir". The results can be manipulated to suit you needs:

import os                                                                                                             

def list_files(dir):
r = []
subdirs = [x[0] for x in os.walk(dir)]
for subdir in subdirs:
files = os.walk(subdir).next()[2]
if (len(files) > 0):
for file in files:
r.append(os.path.join(subdir, file))
return r

For python 3, change next() to __next__().

how iterate through a folder by subfolder in python

You could try the following:

In case you want to give the function the base folder, in which all the customer folders are located, and then want for each of the customer folders a list of all .ai-files (from every sublevel):

from pathlib import Path

def folder_loop(folder):
for path in Path(folder).iterdir():
if path.is_dir():
yield list(path.rglob("*.ai"))

Path.rglob("*.ai") is recursively globbing the the given Path with all its subfolders for .ai-files.

To use it:

the_folder = "..."
for file_list in folder_loop(the_folder):
print(file_list)
# do whatever you want to do with the files

If you want to give it a folder and want one list with all the .ai files in it:

def folder_loop(folder):
return list(Path(folder).rglob("*.ai"))

The yielded/returned lists here contain Path-objects (which are pretty handy). If you want strings instead, then you could do

       ....
yield list(map(str, path.rglob("*.ai")))

etc.

How to iterate through folder and get certain files from the subfolders grouped?

The problem is, that you do not "store" to which folder each file belongs. A solution would be the following:

result = []
for i, (path, subdirs, files) in enumerate(os.walk(myDir)): #use enumerate to track folder id
subdir = {"id": i}
j = 0 #file counter in subfolder
for file in files:
if (file.endswith('.xlsx') or file.endswith('.xls') or file.endswith('.XLS')) and "Release" in file and "Integrated" not in file:
subdir[f"name{j}"] = file
j += 1
result.append(subdir)

EDIT

To ignore folders with no useful files:

result = []
i = 0 #track folder id manually
for path, subdirs, files in os.walk(myDir):
subdir = {}
j = 0 #file counter in subfolder
for file in files:
if (file.endswith('.xlsx') or file.endswith('.xls') or file.endswith('.XLS')) and "Release" in file and "Integrated" not in file:
subdir[f"name{j}"] = file
j += 1
if len(subdir) > 0:
subdir = {"id": i}
result.append(subdir)
i += 1 #increase counter

Iterate through directories, subdirectories, to get files with specialized ext. , copy in new directory

Use os.walk() and find all files. Then use endswith() to find the files ending with .trl.

Sample code:

import os, shutil;

directory = "/home/.../test_daten";
dest_dir = "/home/.../test_korpus";

filelist = [];

for root, dirs, files in os.walk(directory):
for file in files:
filelist.append(os.path.join(root,file));

for trlFile in filelist:
if trlFile.endswith(".trl"):
shutil.copy(trlFile, dest_dir);

Loop through sub directories in directory

Janne Matikainen answer is correct but you need to know how to modify in your code...

First change your code this line

 string[] filesindirectory = Directory.GetFiles(Server.MapPath("~/Folder"));

to

 string[] filesindirectory = Directory.GetDirectories(Server.MapPath("~/Folder"));

Secondly you need to search file in your sub folder path which is your

foreach (string subdir in filesindirectory)

subdir is your path for your directory.

Just do back the same thing what you did to get the files

foreach (string img in  Directory.GetFiles(subdir))

After you finish the foreach subdirectory

foreach (string img in  Directory.GetFiles(subdir)) 
{
// Your Code
}
// Reset the Count1
count1 = 0;

Reset it because you are increasing for dynamic row generating for each sheet.

Then you at new sheet and you didn't reset it.
It will continue the count as per previous sheet.

To get the folder name you can easily get it by split.

Before you create the worksheet you do as per below.

string[] splitter = subdir.Split('\\');
string folderName = splitter[splitter.Length - 1];

Take NOTES if the folderName contain some symbol it might not able to set it to excel worksheet and also the name cannot be too long.

Please ensure that you replace with supportable symbol for excel worksheet

How do I iterate through the files in a directory and it's sub-directories in Java?

You can use File#isDirectory() to test if the given file (path) is a directory. If this is true, then you just call the same method again with its File#listFiles() outcome. This is called recursion.

Here's a basic kickoff example:

package com.stackoverflow.q3154488;

import java.io.File;

public class Demo {

public static void main(String... args) {
File dir = new File("/path/to/dir");
showFiles(dir.listFiles());
}

public static void showFiles(File[] files) {
for (File file : files) {
if (file.isDirectory()) {
System.out.println("Directory: " + file.getAbsolutePath());
showFiles(file.listFiles()); // Calls same method again.
} else {
System.out.println("File: " + file.getAbsolutePath());
}
}
}
}

Note that this is sensitive to StackOverflowError when the tree is deeper than the JVM's stack can hold. If you're already on Java 8 or newer, then you'd better use Files#walk() instead which utilizes tail recursion:

package com.stackoverflow.q3154488;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class DemoWithJava8 {

public static void main(String... args) throws Exception {
Path dir = Paths.get("/path/to/dir");
Files.walk(dir).forEach(path -> showFile(path.toFile()));
}

public static void showFile(File file) {
if (file.isDirectory()) {
System.out.println("Directory: " + file.getAbsolutePath());
} else {
System.out.println("File: " + file.getAbsolutePath());
}
}
}

Iterating through directories with Python

The actual walk through the directories works as you have coded it. If you replace the contents of the inner loop with a simple print statement you can see that each file is found:

import os
rootdir = 'C:/Users/sid/Desktop/test'

for subdir, dirs, files in os.walk(rootdir):
for file in files:
print(os.path.join(subdir, file))

If you still get errors when running the above, please provide the error message.

Iterate inside folders and copy subfolders only

In recursive function you have to create another loop which iterates inside of subfilders and then you just copy the files inside the new directory. Basically like this:

 if (recursive)
{
foreach (DirectoryInfo subDir in dirs)
{
DirectoryInfo[] Subdirs = subDir.GetDirectories();
foreach (DirectoryInfo subDir2 in Subdirs)
{
string newDestinationDir = Path.Combine(destinationDir, subDir2.Name);
CopyDirectory(subDir2.FullName, newDestinationDir, true);
Console.WriteLine("Finnished copying file: " + subDir2.Name);
}
}
}


Related Topics



Leave a reply



Submit