Jump to content

automatically delete files with Robocopy

Hello,

 

I've got a folder on my desktop where all my scanned documents go. To keep it clean and tidy I'd like to have robocopy or another program to delete all the .pdf files inside this folder. How can I configure robocopy to do that? 

I only want it to delete actual files. Inside the scan folder is another folder with some more important stuff, but that should not be deleted. I only want Robocopy to delete FILES inside a folder that are older than 1 day. 

CPU: AMD 3800X GPU: GTX 1080 Ti RAM: (16GB) 2x Corsair 8gb DDR4 3200Mhz Drives: SanDisk 240GB SSD, Samsung 500GB SSD, WD 1TB HDD

Motherboard: MSI X470 Gaming pro plus PSU: Gigabyte 650 watt Monitor(s): 27 inch AOC 1440p

Link to comment
https://linustechtips.com/topic/911082-automatically-delete-files-with-robocopy/
Share on other sites

Link to post
Share on other sites

You could just create a batch file  (a file with the .BAT extension - use notepad, save as , save as type : all files , type file name and put .BAT at the end )

 

Type this inside this batch file :

 

DEL *.pdf

 

That command deletes any file name with the extension pdf in the folder where the batch file is.  It doesn't go through sub-folders, so your folder will remain untouched.

 

If you want it to go through sub-folders, then you can say

 

DEL /S *.pdf 

 

and it will go through sub folders searching for files with the PDF extension and delete those as well.

 

* is  a wildcard it means any combination of characters ... you can say DEL A*.pdf to delete only documents that start with letter A 

 

ah you want files older than 1 day ... i'll think about it.

 

Link to post
Share on other sites

Robocopy (Robust File Copy) is a copy/backup/mirroring utility, it isn't generally used for removal of files.

 

For your requirements it sounds like you require something like forfiles, which is the utility used to match and perform actions on files with specific attributes (delete files older than one day and contain .pdf at the end of the file name, for example).

 

Here's the command to do what you're asking for (remember to replace C:\directory\of\Scanned Documents with the actual directory you want to search in.)

forfiles /P "C:\directory\of\Scanned Documents" /M "*.pdf" /D -1 /C "cmd /C del @path"

You can use your favourite text editor (Notepad, for example) to save it to a .bat file (e.g. Clean Scanned Documents.bat) and run it whenever you want to clean out the folder.

If you want it to be run automatically however, you can use the built-in Windows Task Scheduler to run the batch file automatically at a specific time of day, for example.

/P "C:\directory\of\Scanned Documents" - /P stands for Path, this specifies where you want the command to search.

/M "*.pdf" - /M stands for Match, this searches for filenames that match what you've specified. In this case, * is a wildcard, so *.pdf will match anything that ends in .pdf

/D -1 - /D stands for Day or Date, this selects files with a last modified date later than, equal to or before the specified date. In this case we've specified -1, so the command will search for files that were last modified on the current day, minus one day, i.e. one day old

/C "cmd /C del @path" - /C stands for Command, this runs a command when it finds a matching file. In this case we specified "cmd /C del @path", which will run CMD with the command "/C del @path", the /C in this case just means that CMD should run the following command before closing. @path will be replaced with the full path to the matched file (e.g. C:\directory\of\Scanned Documents\A Scanned Document.pdf)

 

Edited by TakataruMC
Added reminder to swap out placeholder values in code, added explanation of command line switches

Desktop: HP Z220 Workstation, 12 GB RAM, 2x500 GB HDD RAID0, + GTX 1060 3GB

Laptop: ThinkPad T430, 8 GB RAM, 1x120 GB SSD

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×