Skip to main content
2 of 5
deleted 10 characters in body
Rudy Potter
  • 2.6k
  • 8
  • 20

Here is how I might approach it. I would make a list of directories to look in.

dirsToClean = {"C:\\Users\\rpotter\\Downloads", 
  "C:\\Users\\rpotter\\Documents", "C:\\Users\\rpotter\\Desktop"};

Then I would make a list of keywords.

keywords = {"test", "temp",".tmp"};

And add wildcards to either side of the keywords.

keywordsWC = "*" <> # <> "*" & /@ keywords
(*{"*test*", "*temp*", "*.tmp*"}*)

Now I can search for those files.

filesToDelete = FileNames[keywords2, dirs, IgnoreCase -> True] (*Or False as desired*)

If everything looks good we can delete them.

FileDelete[filesToDelete]

But maybe before deleting them we move them to a folder so we can look through them first.

CreateDirectory["c:\\FilesToDelete"]
RenameFile[#, 
   FileNameJoin[{"c:\\FilesToDelete", 
     FileBaseName[#]}]] & /@ filestodelete

And then just delete all the files in that directory.

Rudy Potter
  • 2.6k
  • 8
  • 20