I am wanting to loop through multiple folders with multiple foreach
, since each folder either has subfolders or has files.
The folders I want to loop through are Dates, so I want to loop through a year, the months folders, and the days folders, and within the days I have .jpg
. It is like this: wwwroot\images[year][month][day][image.jpg]
As an example of a directory, it looks like this: wwwroot\images\2022\08\09\vehicle.jpg
The problem I'm having is that it goes through the first time, that is, it goes through the first month, goes through the day and recovers the images, but then foreach
it repeats itself and goes through the same month and the same day, multiplying the images. How can I solve it?
So I'm doing theforeach
dir = DateTime.Now.ToString("yyyy/MM/dd");
fechaAMD = dir.Split("/");
listaPedidos = new List<PedidoViewModel>();
//\imagenes\2022\08\09\...
imagenes = Directory.GetFiles(@"wwwroot\imagenes\" + fechaAMD[0] + @"\" + fechaAMD[1] + @"\" + fechaAMD[2] + @"\", "*.jpg");
// \imagenes\2022\08\..
carpetaDia = Directory.GetDirectories(@"wwwroot\imagenes\" + fechaAMD[0] + @"\" + fechaAMD[1] + @"\");
// \imagenes\2022\..
carpetaMes = Directory.GetDirectories(@"wwwroot\imagenes\" + fechaAMD[0] + @"\" );
// \imagenes\..
carpetaAnio = Directory.GetDirectories(@"wwwroot\imagenes\");
foreach(var carAnio in carpetaAnio)
{
foreach (var carMes in carpetaMes)
{
foreach (var carDia in carpetaDia)
{
foreach (var img in imagenes)
{
rutaSplit = img.Split("\\");
rutaImagen = img.Split("wwwroot");
imagenRecuperada = rutaSplit[5].Split("_");
if (identi != "" && identi == imagenRecuperada[0])
{
pedido = new PedidoViewModel();
pedido.setNumeroPedido(identi);
pedido.setNumeroEntrega(imagenRecuperada[1]);
pedido.setNumeroRemito(imagenRecuperada[2]);
pedido.setimagenes(rutaImagen[1]);
pedido.setMonto("$11111");
listaPedidos.Add(pedido);
}
}
}
}
}
return listaPedidos;
Debugging I saw that Monthfolder is changing but Dayfolder is not.
In the end following what @fredyfx said, I was using the same path over and over again, so I gave it new directories in each
foreach
, or at least for now it works perfectly.