在R中,我试图使用download.file()命令在一个简单的代码中下载文件(完全是新手)。文件正在正确下载。但是,如果下载目标中已存在文件,我希望以增量重命名下载的文件,而不是覆盖,这似乎是默认进程。
nse.url = "https://www1.nseindia.com/content/historical/DERIVATIVES/2016/FEB/fo04FEB2016bhav.csv.zip"
nse.folder = "D:/R/Download files from Internet/"
nse.destfile = paste0(nse.folder,"fo04FEB2016bhav.csv.zip")
download.file(nse.url,nse.destfile,mode = "wb",method = "libcurl")
问题w.r.t这个特定的代码:如果“fo04FEB2016bhav.csv.zip”已经存在,那么请说“fo04FEB2016bhav.csv(2).zip”?
问题的一般答案(而不仅仅是上面提到的代码)将会受到赞赏,因为这样的瓶颈也会出现在任何其他情况中。
答案 0 :(得分:2)
以下功能将根据下载的文件自动分配文件名。它将检查您要下载的文件夹是否存在类似命名的文件。如果找到匹配项,它将添加一个增量并下载到新文件名。
ekstroem建议摆弄卷曲设置可能是一个更好的方法,但我并不聪明,无法弄清楚如何使这项工作。download_without_overwrite <- function(url, folder)
{
filename <- basename(url)
base <- tools::file_path_sans_ext(filename)
ext <- tools::file_ext(filename)
file_exists <- grepl(base, list.files(folder), fixed = TRUE)
if (any(file_exists))
{
filename <- paste0(base, " (", sum(file_exists), ")", ".", ext)
}
download.file(url, file.path(folder, filename), mode = "wb", method = "libcurl")
}
download_without_overwrite(
url = "https://raw.githubusercontent.com/nutterb/redcapAPI/master/README.md",
folder = "[path_to_folder]")
答案 1 :(得分:1)
试试这个:
Validating
如果文件名的一部分已经存在,例如您有nse.url = "https://www1.nseindia.com/content/historical/DERIVATIVES/2016/FEB/fo04FEB2016bhav.csv.zip"
nse.folder = "D:/R/Download files from Internet/"
#Get file name from url, with file extention
fname.x <- gsub(".*/(.*)", "\\1", nse.url)
#Get file name from url, without file extention
fname <- gsub("(.*)\\.csv.*", "\\1", fname.x)
#Get xtention of file from url
xt <- gsub(".*(\\.csv.*)", "\\1", fname.x)
#How many times does the the file exist in folder
exist.times <- sum(grepl(fname, list.files(path = nse.folder)))
if(exist.times){
# if it does increment by 1
fname.x <- paste0(fname, "(", exist.times + 1, ")", xt)
}
nse.destfile = paste0(nse.folder, fname.x)
download.file(nse.url, nse.destfile, mode = "wb",method = "libcurl")
,并且文件夹中有文件url/test.csv.zip
,则此方法不起作用。它会认为该文件已存在,因此会将其保存为testABC1234blahblah.csv.zip
。
您需要相应地更改代码的test(2).csv.zip
部分。
答案 2 :(得分:0)
这不是一个正确的答案,不应该被认为是这样,但上面的评论部分太小,无法全部写下来。
我认为卷曲的<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<div class="container">
<div class="row">
<div class="col-sm-12 col-md-8 col-md-offset-2">
<button type="button" type="button" class="btn btn-default btn-lg btn-block" data-toggle="modal" data-target="#first-modal">Open Modal</button>
</div>
</div>
</div>
<div id="first-modal" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">First Modal</h4>
</div>
<div class="modal-body">
<button type="button" type="button" class="btn btn-default btn-lg btn-block">Open Second Modal</button>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<div id="second-modal" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Second Modal</h4>
</div>
<div class="modal-body">
<p>One fine body…</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
选项可以用来,但是现在我更仔细地看了它,结果发现它还没有实现。现在-O -n
在下载已存在的文件时自动递增文件名。但是,设置wget
不能与method="wget"
一起使用,因为您被迫设置目标文件名,一旦这样做,您将覆盖自动文件增量。
我喜欢@Benjamin提供的解决方案。或者,您可以使用
download.file
通过系统获取文件(假设您已安装system(paste0("wget ", nse.url))
)并让wget
处理增量。