Rename a file with a vector value

Asked

Viewed 172 times

5

I am using a loop to extract hundreds of data using unzip. However, I want to rename the files with the same name as the extracted file. I’m making the following code:

zip_postos <- setwd("ZIP_Postos")
zip_postos <- list.files(zip_postos)
postos <- setwd("Postos")
for (i in 1:184){
  unzip(zip_postos[[i]], "CHUVAS.MDB", exdir = postos)
  file.rename("CHUVAS.MDB", paste(zip_postos[[i]]))
} 

I know the code is wrong, but I cannot rename the RAINFALL file to the extracted file name without the format ". zip"

Any suggestions?

1 answer

4


If I understand correctly, basically what you want to do is remove the ". zip" extension from the file name. You can do this by using the gsub().

For example, suppose your zip file has following name:

nome_do_zip <- "arquivo.zip"

Then to remove the zip just do the following:

nome_sem_zip <- gsub("\\.zip", "", nome_do_zip)
nome_sem_zip
[1] "arquivo"

And then you can use this name to do what you want.

  • 1

    Thank you very much! It helped a lot.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.