Delete Files

The code below is Windows Powershell code. Click here for the c# version. It will delete any files matching the search criteria in the given location. There are many ways of doing this, so please bear in mind that this is not the only solution available.

$fileLoc = “Target directory here”
foreach($file in $fileLoc)
Remove-item -Include *.tmp -Recurse -Path $file

Ok, lets look at the -Include parameter. This will only return the files with a file type of .TMP, This can be changed to match your requirements. the -recurse parameter will tell the script to look under any subfolders that exist under the $fileLoc directory. “-Path $file” is basically telling the Remove-Item line what file(s) to remove.

as this is in a foreach loop, it will remove any files that match the given criteria. You can always change the -Include parameter to “*.*” which will remove all files in the given directory.

Use all code at your own risk. while this has been tested in my environment, it should be tested on a temporary directory prior to running properly.

Leave a Reply