0
$\begingroup$

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

$\endgroup$
8
  • 1
    $\begingroup$ I strongly recommend saving your files in a different folder before experimenting with deleting files in Mathematica. From what I understood you need FileNames and DeleteFile. From the documentation of those functions it is kind of straight forward, $\endgroup$ Commented Nov 16, 2022 at 18:55
  • $\begingroup$ But as would be the structure of the program that does the deletion, I am a beginner in MMA. I have seen several posts but I have not seen one with these requirements $\endgroup$
    – Pamela
    Commented Nov 16, 2022 at 19:45
  • $\begingroup$ There is not much knowledge of MMA that is required beyond what the Details section shows. Here is a step by step guide. Step 1 : Go to the location of the folder using whatever file explorer you use on your computer. Step 2: Make a copy of that folder and put it somewhere else as a backup in case something goes wrong. Step 3: Copy the location of that folder (not the folder itself nor the name. The whole string/path for the folder). The location of that folder will be called home\docfolder in the following. $\endgroup$ Commented Nov 16, 2022 at 20:04
  • $\begingroup$ Step 4 go on Mathematica and set a variable named folder, for example, to folder="home\docfolder". Step 5 : Look for all files with naturales in it using notWanted=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 in notWanted by using DeleteFile[notWanted]. $\endgroup$ Commented Nov 16, 2022 at 20:08
  • $\begingroup$ You might also want to change the option for IgnoreCase depending on whether you want to treat uppercase and lowercase letters the same way or not. $\endgroup$ Commented Nov 16, 2022 at 20:20

1 Answer 1

0
$\begingroup$

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.

$\endgroup$
3
  • 1
    $\begingroup$ Hi, there might be a typo it should be DeleteFile rather than FileDelete. Also keywords2 is not defined in the post. $\endgroup$ Commented Nov 16, 2022 at 21:30
  • $\begingroup$ Thanks! Fixed in above. $\endgroup$ Commented Nov 17, 2022 at 20:30
  • 1
    $\begingroup$ Thank you, I will try to do something with the contribution of all of you, $\endgroup$
    – Pamela
    Commented Nov 17, 2022 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.