开发者

PERL - check if c:\temp has any files in it, if empty - delete c:\temp directory

开发者 https://www.devze.com 2023-03-26 04:20 出处:网络
I need an IF statement that will check if c:\\temp has any files in it.. If it is empty, I will want to delete that c:\\temp folder. I want to delete t开发者_Go百科he c:\\temp directory ONLY if there

I need an IF statement that will check if c:\temp has any files in it.. If it is empty, I will want to delete that c:\temp folder. I want to delete t开发者_Go百科he c:\temp directory ONLY if there is nothing in it.


To delete files use unlink. Unlink takes a list of filenames which you can get from glob.

The easy way is to not use an if-statement:

unlink glob 'c:/temp/*';

To delete the entire directory, if empty:

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

精彩评论

暂无评论...
验证码 换一张
取 消