I have a question which drives me crazy. I have the following tables as my source table:
v1 v2
1 http://www.sec.gov/Archives/edgar/data/20/0000893220-01-000315.txt
2 http://www.sec.gov/Archives/edgar/data/20/0000893220-03-000441.txt
3 http://www.sec.gov/Archives/edgar/data/20/0000893220-04-000596.txt
4 http://www.sec.gov/Archives/edgar/data/20/0000893220-05-000728.txt
5 http://www.sec.gov/Archives/edgar/data/20/0000893220-06-000650.txt
.....
Basically, I have the ID in V1
and URL in V2
. I need to download thousands of similar file at once. So far I solved the problem of downloading by using following code:(lets say link
is the dataset containing the table I provide above)
urls<-c(link$v2)
for (url in urls){
download.file(url, destfile = basename(url), quiet=T)
}
This code works fine for downloading. However, now, instead of keeping the name of the downloaded file as original basename such as 0000893220-01-000315.txt
or 0000893220-03-000441.txt
etc, I wish to change the file name according to the ID in v1, to name the file as 1.txt
, 2.txt
etc.
Can anyone help me to solve this? Very appreciated to your help :)
Use file.rename
:
with(link, file.rename(basename(v2), paste0(v1, ".txt")))
Alternately, give them the name you want when you download them:
nr <- nrow(link)
for(i in 1:nr) with(link[i,], download.file(v2, destfile = paste0(v1, ".txt"), quiet=TRUE))