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)     

```
You can use `lapply`. You would need a vector of file names, maybe with

```
fn_list <- list.files("PATH", pattern="asc$")

```

Your workhorse function could look like

```
one_file <- function(fn, template, rastergrande) {
   mau <- raster(fn)
   intermedio <- crop(mau, rastergrande)
   novoraster <- resample(intermedio, template, "bilinear")
   values(novoraster)[is.na(values(template))] <- NA
   fn_out <- gsub("asc$", "_final.asc", fn)
   writeRaster(novoraster, fn_out)
}

```

You can then apply `one_file` on `fn_list`:

```
library(raster)
library(usdm)
library(ggplot2)
library(rgdal)

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

lapply(fn_list, one_file, template=templ, rastergrande=rastergrande)

```

By the way, take a look at the `terra` package: <https://www.youtube.com/watch?v=O8V4QJ13Gjc>