Rename a file with a value of a vector

5

I'm 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 am 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 can not rename the RAW file to the name of the extracted file, without the ".zip" format

Any suggestions?

    
asked by anonymous 19.04.2015 / 11:01

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 using gsub() .

For example, suppose your zip file has the 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.

    
19.04.2015 / 12:46