【问题标题】:PERL - check if c:\temp has any files in it, if empty - delete c:\temp directoryPERL - 检查 c:\temp 中是否有任何文件,如果为空 - 删除 c:\temp 目录
【发布时间】:2011-08-05 04:48:50
【问题描述】:

我需要一个 IF 语句来检查 c:\temp 中是否有任何文件。如果它为空,我将要删除该 c:\temp 文件夹。仅当其中没有任何内容时,我才想删除 c:\temp 目录。

【问题讨论】:

标签: perl


【解决方案1】:

要删除文件,请使用unlink。 Unlink 获取一个文件名列表,您可以从 glob 获取这些文件名。

简单的方法是不使用 if 语句:

unlink glob 'c:/temp/*';

删除整个目录,如果为空:

$dir = 'c:/temp';
@files = glob "$dir/*";            # Get files in c:/temp/
rmdir $dir unless(scalar @files);  # Remove directory if empty

【讨论】:

  • 如果$dir 包含名称以“.”开头的文件,glob "$dir/*" 将返回一个空列表。 (这意味着 rmdir 会失败,这是您想要的,但首先使用 glob 是没有意义的。)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-26
  • 2012-07-04
  • 1970-01-01
  • 2021-12-04
  • 2012-02-12
  • 2020-12-22
  • 2023-03-25
相关资源
最近更新 更多