How to apply apply code to many folders?

I have a main folder C:/Users/nikos/OneDrive/Desktop/path_to_inside_tst_v2. Inside that folder there are other folders representing cities. Inside these folders there other folders representing years. Inside each yearly folder there other folders representing months. An example:

C:/Users/nikos/OneDrive/Desktop/path_to_inside_tst_v2/cairo/2022/02

or

C:/Users/nikos/OneDrive/Desktop/path_to_inside_tst_v2/mexico/2019/11.

Also, I have the following code:

pacman::p_load(terra, parallel, doParallel, tools, fs, dplyr)

wd = "C:/Users/nikos/OneDrive/Desktop/path_to_inside_tst_v2/cairo/2022/02/" # where the tif images are located
mwd <- "C:/Users/nikos/OneDrive/Desktop/path_to_inside_tst_v2/cairo/"  # to read the geojson

ntl = rast(paste0(wd, "ntl.tif"))
pixel_value <- res(ntl)[1]

vectList <- list.files(path = paste0(mwd), pattern = "geojson$", all.files = TRUE, full.names = TRUE)

v <- terra::vect(vectList)

doStuff <- function(file){
  
  pic = rast(file)
  
  for (i in seq(from = 0.3, to = 0.5, by = 0.1)) {
    
    print(i)
    
    gf <- terra::focalMat(pic, i * pixel_value, "Gauss")
    r_gf <- terra::focal(pic, w = gf, fun = "mean", na.rm = TRUE)
    
    r_gf = terra::resample(r_gf, ntl, method = 'average', threads = TRUE)
    
    r_gf <- terra::crop(r_gf, ext(v))
    r_gf <- terra::mask(r_gf, v)
    
    (stringedi = gsub("\\.", "", toString(format(i, nsmall = 2))))
    
    writeRaster(r_gf, 
                paste0(wd, 
                       basename(fs::path_ext_remove(file)),
                       stringedi, ".tif"), 
                overwrite=TRUE)
  }
  
}

files <- list.files(wd, pattern = "tif$", full.names = TRUE)
files <- files[files != paste0(wd, "ntl.tif")]
purrr::walk(files, doStuff)

I want to run the above code for every monthly folder. This means that, when for example, I run the above code for the month 02 which is inside the folder 2022 which is inside the folder cairo, I want then to move to the folder 03 inside the folder 2022 inside cairo (i.e., to this path:

C:/Users/nikos/OneDrive/Desktop/path_to_inside_tst_v2/cairo/2022/03

When the code finishes all months for every year for cairo, then it should go to the next city and so on. Can someone provide a sample code?

Important thing is that both the wd and mwd should change, but wd should change more often. Another consideration is that inside each monthly folder I have another folder called hr, which I don't want to apply the code.

wouldn't the easiest be to list all the directories

dirs <- list.dirs(recursive = TRUE)

then write dostuff to take a directory as a an argument and do whatever it is you need to do

dostuff <- function(x){
 # extract the folder name from x
 dir <- dirname(x)
 base <- basename(x)
 path <- file.path(dir, base)
 system(paste0("touch ", path, "/nikos_was_here.txt"))
}

and then

purrr::map(dirs, dostuff)

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.