I'm a teacher and I have a lot of files (hundreds) with different names in different directories *for example "religion guide.docx","natural sciences guide.pdf" ,"Algebra 2 Guide.pdf". I intend to leave only the math and physics and delete the rest but in batches, how do I make MMA allow me to choose the directory with which I want to operate and make a database with keywords of things that I want to delete something like,this is a possible example: $Delete [ *religion*.* , *naturales*.* etc]$ Hope it's understandable
1 Answer
$\begingroup$
$\endgroup$
3
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[keywordsWC, dirsToClean, IgnoreCase -> True] (*Or False as desired*)
If everything looks good we can delete them.
DeleteFile[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",
FileNameSplit[#][[-1]]}]] & /@ filesToDelete
And then just delete all the files in that directory.
-
1$\begingroup$ Hi, there might be a typo it should be
DeleteFile
rather thanFileDelete
. Alsokeywords2
is not defined in the post. $\endgroup$ Commented Nov 16, 2022 at 21:30 -
-
1$\begingroup$ Thank you, I will try to do something with the contribution of all of you, $\endgroup$– PamelaCommented Nov 17, 2022 at 23:15
FileNames
andDeleteFile
. From the documentation of those functions it is kind of straight forward, $\endgroup$folder="home\docfolder"
. Step 5 : Look for all files with naturales in it usingnotWanted=FileNames["*naturales*",folder]
. Check that the list is not empty and contains the files you expect. Step 6: Again please be careful and either save a backup of the folder or make a practice folder with 3 files. You can delete the entire list of files innotWanted
by usingDeleteFile[notWanted]
. $\endgroup$IgnoreCase
depending on whether you want to treat uppercase and lowercase letters the same way or not. $\endgroup$