I have a main directory, and each folder within this has the same format of data. I'd like to go into each folder individually and preform the same function. Each folder has a slightly different name, but the files all follow the same naming protocol (ie. YYYYMMDD_XX_raster1.tif; YYYYMMDD_XX_raster2.geo.tif).
I've tried modifying a code that I already had, but in that instance the .tifs were all in the same folder.
library(raster)
library(sp)
library(rgdal)
#create sample rasters
r <- raster(nrow=10, ncol=10)
x <- setValues(r, sample(-180:180,ncell(r), replace(T))
y <- setValues(r, sample(-90:90,ncell(r), replace(T))
dq2<- function(in_dir_path, add_file_name, silent=F){
    #inputs
    #in_dir_path: path to directory
    #add_file_name: ie. finished
   list_files <- sort(list.files(path= in_dir_path, pattern =""))
   print(list_files)
   if(silent == F){
     message(paste"Nb files in in_dir", length(list_files))
  }
  for(i in 1: length(list_files)){
    if(silent == F){
     message(paste("Processing", i, "out of", length(list_files)))
  }
   #get rasters (this would normally grab from within the subfolder, but here refers to the rasters created above)
   x <- raster(paste(in_dir_path, "/$raster1.geo.tif", list_files[i], sep = "")) 
   y <- raster(paste(in_dir_path, "/$raster2.geo.tif", list_files[i], sep = ""))
   #Create filename to save
  split<- strsplit(list_files[i], split=".tif")[[1]][1]
    sprint (split)
  nameFile <- paster(in_dir_path, "/", split, "_", add_file_name, ".tif", sep = "")
  # calculation here
    tf <- function(x,y) {
      return(100 * x / y)
    }
    answer <- overlay(x,y, fun=tf)
    WriteRaster(answer, filename=nameFile, format="GTiff", overwrite=TRUE, options=c('TFW=YES'))
    if(silent == F){
    message(paste("process complete", nameFile))
    }
}
message("finished")
dq2("C:/Users", "fin", silent=F) 
When I try to run this, it looks like the code is waiting for more input.