简体   繁体   English

R,重命名成千上万的下载文件

[英]R, rename thousands of downloaded files

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 . 基本上,我在V1具有ID,在V2具有URL。 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) 到目前为止,我已经通过使用以下代码解决了下载问题:(可以说link是包含我上面提供的表的数据集)

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. 但是,现在,我不想将下载文件的名称保留为原始基本名称(例如0000893220-01-000315.txt0000893220-03-000441.txt等),而是希望根据v1中的ID将文件名更改为将该文件命名为1.txt2.txt等。

Can anyone help me to solve this? 谁能帮我解决这个问题? Very appreciated to your help :) 非常感谢您的帮助:)

Use file.rename : 使用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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM