PERL - check if c:\temp has any files in it, if empty - delete c:\temp directory
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
精彩评论