0
I need to search the information of only a few subfolders inside a main directory, I made a code this way but I’m having a hard time finding a way to inform only the subfolders I need. For example I have a folder in this structure:
Briefcase
|
|_ _ _ subfolder 1/.xml files
|
|_ _ _ subfolder 2/.xml files
|
|_ _ _ subfolder 3/.xml files
You’d need to get the data from subpasta1 and subpasta3, the code I have I can only read the Briefcase entire:
using System.IO;
using System;
using System.Linq;
namespace LerDiretorios
{
class Program
{
static void Main(string[] args)
{
var directoryPath = @"C:\Pasta\";
var searchPattern = "*.xml";
var monthsAgo = -2;
string[] filesFound;
var startTime = DateTime.Now;
try
{
filesFound = Directory.GetFiles(directoryPath, searchPattern, SearchOption.AllDirectories);
filesFound
.Select(f => new FileInfo(f))
.Where(f => f.CreationTime >= f.CreationTime.AddMonths(monthsAgo))
.ToList();
}
catch (Exception ex)
{
Console.Write($"ERRO: {ex.Message}");
goto End;
}
if (filesFound.Length == 0)
{
Console.WriteLine("Nenhum arquivo encontrado.");
goto End;
}
foreach (string filePath in filesFound)
{
FileInfo fInfo = new FileInfo(filePath);
Console.WriteLine(
$"{fInfo.Directory}, Arquivo: {Path.GetFileName(fInfo.Name)}, " +
$"Criado em: {fInfo.CreationTime.ToString()}\n");
}
//Conta os registros lidos
Console.WriteLine($"Quantidade de registros lidos: {filesFound.Length}");
//Mostra hora de início do procedimento
Console.WriteLine($"Hora Inicio: {startTime.TimeOfDay}");
//Mostra hora de fim do procedimento
var finishTime = DateTime.Now;
Console.WriteLine(
$"Hora Fim: {finishTime.TimeOfDay} \n" +
$"Tempo de Execução: {finishTime.TimeOfDay - startTime.TimeOfDay}");
End:;
}
}
}
It has so many shapes but nothing direct has to pass the Path in this case apart since you know which one you want to read. Create an array with folder names and do a for. I ask is multiple folders?
– novic
Like you would if it wasn’t programming?
– Maniero
@Virgilionovic are almost 60 sub-folders, I simplified a little in the example, but the logic I think would be the same to solve. I’ll try it on and see how it looks.
– NR_Andre