快速测试目录是否为空 [英] Fast test if directory is empty

查看:165
本文介绍了快速测试目录是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当然,我可以查看


$ b $的长度b

  list.files(path,all.files = TRUE,include.dirs = TRUE,no .. = TRUE)

但这需要枚举我宁愿避免的目录的整个内容。



编辑:我正在寻找便携式解决方案。



编辑^ 2 :一个巨大的目录的一些时间在最初为空的目录中,它将创建100000个空文件):

 > system.time(file.create(as.character(0:99999)))
用户系统已用
0.720 12.223 14.948
> system.time(length(dir()))
用户系统已用
2.419 0.600 3.167
> system.time(system(ls | head -n 1))
0
用户系统已用
0.788 0.495 1.312
> system.time(system(ls -f | head -n 3))

..
99064
用户系统已用
0.002 0.015 0.019

-f 开关对 ls 至关重要,否则将避免排序。

解决方案

if(length(dir(all.files = TRUE))== 0)



我不知道你的资格是快,但如果 dir 需要很长时间,有人滥用你的文件系统:-(。


What is the fastest way to test if a directory is empty?

Of course I can check the length of

list.files(path, all.files = TRUE, include.dirs = TRUE, no.. = TRUE)

but this requires enumerating the entire contents of the directory which I'd rather avoid.

EDIT: I'm looking for portable solutions.

EDIT^2: Some timings for a huge directory (run this in a directory that's initially empty, it will create 100000 empty files):

> system.time(file.create(as.character(0:99999)))
   user  system elapsed 
  0.720  12.223  14.948 
> system.time(length(dir()))
   user  system elapsed 
  2.419   0.600   3.167 
> system.time(system("ls | head -n 1"))
0
   user  system elapsed 
  0.788   0.495   1.312 
> system.time(system("ls -f | head -n 3"))
.
..
99064
   user  system elapsed 
  0.002   0.015   0.019 

The -f switch is crucial for ls, it will avoid the sorting that will take place otherwise.

解决方案

How about if(length(dir(all.files=TRUE)) ==0) ?

I'm not sure what you qualify as "fast," but if dir takes a long time, someone is abusing your filesystem :-(.

这篇关于快速测试目录是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆