Question: I have 90 rasters that I need to clip and ressample to match my template raster. Is there a way for me to "automize this"? (i.e., only have to run the process once, so that I can, for instance, leave it running overnight)

I know that the answer is probably a "loop", but I'm not sure how to do it because each original file has a different name and the final product needs to have that same name, but end in "\_final.asc"

This is the code I've been using:

```
library(raster)

#load original world raster (the one to be processed)
mau <- raster("PATH/name_of_the_raster.asc")

#load bounding box raster made by me (to clip the original and therefore make the next steps easier)
rastergrande <- raster("PATH")

#clip original raster by bounding box raster
intermedio <- crop(mau, rastergrande)

#load my template raster (the one that has the extent and cell size I want)
template <- raster("PATH")

#resample raster to template parameters
novoraster <- resample(intermedio, template, "bilinear")

#assign NA's to where template is NA
values(novoraster)[is.na(values(template))] <- NA

#Export to ascii
writeRaster(novoraster, "PATH/name_of_the_raster_final.asc", overwrite=TRUE)

```

Answer: 
Variations of this question have been asked a number of times. Here is one [example](https://stackoverflow.com/questions/53198261/multiple-input-filenames-as-output-filenames-in-r)

The general approach can be to get a vector with all your filenames, typically with `list.files`, and use that to make a vector of output filenames by changing the names or the path. Something like this:

```
input <- list.files(path=".", pattern="asc$", full.names=TRUE)
output <- gsub(".tif", "_out.tif", input)

```

Or change the folder

```
output <- gsub("this/path", "that/path", input)

```

And then use a loop

```
for (i in 1:length(input)) {
   # read input[i]
   # write output[i] 
}

```

A working minimal example (using a single file):

```
library(raster)
input <- system.file("external/test.grd", package="raster")
# write to current working directory, but with asc extensions
output <- gsub(".grd", ".asc", basename(input))

for (i in 1:length(input)) {
    r <- raster(input[i])
    writeRaster(r, output[i], overwrite=TRUE)
}

```

Also, not directly related to your question, but this:

```
values(novoraster)[is.na(values(template))] <- NA

```

Should be

```
novoraster <- mask(novoraster, template)     

```
Here you can work with a for loop or apply (lapply and mapply)

```
file_names <- list.files("PATH/", pattern = ".tif")
r_list <- lapply(file_names,raster)

rastergrande <- raster("PATH")
template <- raster("PATH")

intermedio <- list()
novoraster <- list()

```

Creating a vector with rasters names

```
rasternames <- c("name1","name2" ...)

```

For loop to process all rasters

```
for(i in 1:length(file_names){

intermedio[[i]] <- crop(r_list[[i]], rastergrande)
novoraster[[i]] <- resample(intermedio[[i]], template, "bilinear")  
values(novoraster[[i]])[is.na(values(template))] <- NA

writeRaster(novoraster[[i]], paste0("PATH/", rasternames[i], ".asc"), overwrite=TRUE)

}

```