Delete all *.jpg in folder
Question
How do I delete files within a temp folder at only have the .jpg extension.
Here is what I tried;
unlink("../httpdocs/Temp/*.jpg);
Solution
You need to apply the glob function to the pattern, for example:
unlink glob "../httpdocs/Temp/*.jpg";
This is covered in the unlink and glob docs. Unlink itself expects a list of files to process. The glob function will, to quote the docs:
In list context, returns a (possibly empty) list of filename expansions on the value of EXPR such as the standard Unix shell /bin/csh would do.
OTHER TIPS
martin clayton has given a good answer. To give proper warnings for failed file deletions might be a good idea, however, in which case a loop is better than using the list form of unlink
:
unlink or warn "$_: $!" for glob "../httpdocs/Temp/*.jpg"
You can try this.. You need to use glob
for removing files..
chdir "../httpdocs/Temp"
unlink glob "*.jpg"
I would suggest to operate in a while loop (scalar context) as using glob in list context can raise memory consumption, depending on number of dentries.
while( my $dentry = <../httpdocs/Temp/*.jpg> ) {
unlink $dentry or die( "Couldn't delete the $dentry file: $!" );
}