perl - 删除给定目录中的子目录和文件

标签 perl file directory unlink

我想从目录中删除所有子目录和文件,而不是目录本身。例如,如果我在变量中传递“Sample”目录,并且“Sample”目录包含三个子目录和 3 个文件,我想删除所有这 3 个目录和 3 个文件。实际上,“Sample”目录可以包含许多子目录和文件。

最佳答案

预计到达时间:这实际上在 perlfaq5 中:How do I delete a directory tree?

使用 File::Path ,核心模块。

perl -MFile::Path=remove_tree -we 
    'remove_tree("Sample",{keep_root=>1}) or die $!'
keep_root选项将导致 remove_tree 保留顶级目录:

keep_root => $bool

When set to a true value, will cause all files and subdirectories to be removed, except the initially specified directories. This comes in handy when cleaning out an application's scratch directory.

关于perl - 删除给定目录中的子目录和文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9750835/

相关文章:

perl - 如何在调试器模式下运行 perl 测试?

c++ - Schliemann 的编程语言学习方法

c# - 在 c# 中从 MultipartFileData 获取原始文件名

directory - 使用 NSOutlineView 作为文件系统目录浏览器的 Swift 代码

perl - 如何编写一个 C 函数并能够从 perl 中调用它

perl - 为什么在分号前加一个空格不好?

java - 创建新的 File 实例是否会导致创建空文件?

file - 以固定大小的 block 读取二进制文件并将所有这些 block 存储到 Vec 中的正确方法是什么?

javascript - 如何使用 ExtendScript 创建文件夹?

python - 在 python 中,如何将文件复制到目录中并在该目录达到一定大小时停止