Delete file if older than 5 days using FORFILES - batch-file

I cant get FORFILES to work for me. I run a .bat that backs up a few databases I have each day using this one example below.
COPY "X:\W Attendenance\C Crew Attendance\Attendance Control DB v4.xx.accdb" "X:\Supervisors Log\Backups\Attendance Bak\bak_%currdate%_Attendance Control DB v4.xx.accdb"
Then after running my backups I wanted to delete the old files that were older than five days by using the below example.
FORFILES /p "X:\Supervisors Log\Backups\Attendance Bak\" /d -5 /c "cmd /c del #path "
But what happens is it creates the backups but never deletes the old ones. What am I doing wrong?
Ken it still seems to not work. I changed the #file to #path and ran and I still have my backups. I also had another question, can I limit the delete to just files starting in bak_ ? and can I delete the file by its name because every backup starts with bak_ then the date mm-dd-yyyy_ then name of the file...

This fixed it... Thanks for your help and links!
forfiles -p "X:\Supervisors Log\Backups\Attendance Bak" -s -m *.* -d -1 -c "cmd /c del #path"

On a side note that only is able to delete the File but Sometimes I needed to delete a folder and everything in it so I use:
FORFILES /P "H:\BACKUPS\bk_Operator_Rate_Program" /d -10 /c "cmd /c IF #isdir == TRUE rd /S /Q #path"

Related

Command Line 3 days rolling backup using RoboCopy

what I would like to accomplish is have a three day rolling backup of the Source Folder, which will be modified every day. I want anything removed longer than 3 Days as worst case I will have to revert to the most recent backup but having that 3 Days allows me to revert back to a previous changes if need being.
What I have done in Command Line is as follows:
SET STAMP=%DATE:/=-% %TIME::=.%
SET SOURCE="C:\Users\PersonA\Documents\Test\Source"
SET DEST="C:\Users\PersonA\Documents\Test\Destination\%STAMP%"
robocopy %SOURCE% %DEST% /e
forfiles -p "C:\Users\PersonA\Documents\Test\Destination\" -s -m *.* -d -3 -c "cmd /c echo #file"
I have timestamped and concatenated the FolderName and Current Date and Time.
However the bit I am struggling with his removing anything longer than 3 Days, currently the process is just copying, but what I need is a process to detect modified date or perhaps created date and remove directory anything greater than 3 days with the code I produced. The deleted command I have added above doesn't seem to do anything at all.
If anyone can help I will be most grateful.
Thank you in Advance
I would recommend something like this:
SET STAMP=%DATE:/=-% %TIME::=.%
SET SOURCE="C:\Users\PersonA\Documents\Test\Source"
SET DEST="C:\Users\PersonA\Documents\Test\Destination\%STAMP%"
REM Set Robocopy to only retry once after waiting 1 second; copy all
REM file attributes, security, etc; and create a log file in the
REM temp folder with the same name as the date.
robocopy %SOURCE% %DEST% /e /r:1 /w:1 /COPYALL /LOG:%TEMP%\%STAMP%.LOG
C:
CD \Users\PersonA\Documents\Test\Destination\
REM Recursively and quietly delete all directories older than
REM 3 days
FORFILES /S /D -3 /C "cmd /c IF #isdir == TRUE rd /S /Q #path"
This is extrapolated from the answer given in this post.

Delete all *.Zip files older than today using Windows batch file

Assuming you must use a Windows batch file, (not powershell), and one wants to delete all files ending in .zip that are in the current active directory. How to do this?
All attempts so far are failing:
forfiles -p "C:\temp\test" -s -m *.zip -d 1 -c "cmd /c del *.zip"
For this it says
ERROR: No files found with the specified search criteria.
As suggested in my comment, your problem could easily be solved by reading the usage information for your command, (available when entering FORFILES /? at the Command Prompt).
Based on your questions criteria, "delete all files ending in .zip that are in the current active directory":
You don't need to use the /P option because as stated in the usage information, "The default folder is the current working directory (.)".
There is nothing in your question regarding recursing subdirectories of the current directory, so the /S option which "Instructs forfiles to recurse into subdirectories" is not required.
For the /D option you are looking for files with a last modified date less than yesterday, i.e. "the current date minus "dd" days", /D -1.
Because you're wanting to delete files in the current directory, there's no need to use the "Full path of the file", #path, so what you need is the "The name of the file", #file.
FORFILES /M *.zip /D -1 /C "CMD /C DEL #file"
You did not mention anything about subdirectories or windows version so I'm assuming somewhat. You have an old version syntax. In Windows 7 and further the syntax changed a little bit.
For windows 7:
forfiles /P "C:\temp\test" /S /M *.zip /D -1 /C "cmd /c del #path"

Batch Script to delete files past 5 days old

I have a batch script that I have run through Task Scheduler every night at midnight. Here is the script:
forfiles /M *.bak /p "Z:\Logs" /S /D -5 /C "cmd /c del #file : date >= 5 days >NUL"
But when the task runs at midnight, it does not delete the files older than 5 days. If I double click on the batch file and run it manually, it does delete the files older than 5 days. What is wrong or do I need to do something different to make this work?
EDIT:
Here is my full batch file and more information about the task schedule:
sqlcmd -S server\SQLEXPRESS -U user -P password -i "D:\BackupPrograms\translogsbackup.sql"
forfiles /M *.trn /p "Z:\Logs" /S /D -5 /C "cmd /c del #file >NUL"
I am using an administrator account for the task schedule to run every night. I am trying to get it to delete the older backups that the sqlcmd is creating, that way I make sure I am not wasting a bunch of space on Full SQL backups that are not needed. I hope this helps more. I am just confused why the batch file would act differently running through the Task Scheduler and when I double click on it to run.
It doesn't work from the command prompt any more than it does from the scheduler, and here's why.
/C "cmd /c del #file : date >= 5 days>NUL"
The : is illegal at that position in a command line, and it's ignored.
The >= is interpreted as the output redirection symbol, and therefore all of the output is redirected to a file named 5 in the current directory.
You can test this at a command prompt yourself:
Create a new, empty folder on your system, such as C:\Test, from a command prompt, and make it the active directory.
C:\>md Test
C:\>cd Test
Create a couple of dummy files in the folder:
C:\Test>echo file1 > file1.txt
C:\Test>
C:\Test>echo file2 > file2.txt
Do a directory to see what's there:
C:\Test>dir /b
file1.txt
file2.txt
C:\Test>
Try this forfiles command to see the output:
C:\Test>forfiles /M *.txt /C "cmd /c echo #file"
"file1.txt"
"file2.txt"
C:\Test>
Change the forfiles to add the : date >= 5 days and run again:
C:\Test>forfiles /M *.txt /C "cmd /c echo #file : date >= 5 days"
C:\Test>
Do a directory to see what's there:
C:\Test>dir /b
5
file1.txt
file2.txt
C:\Test>
Note the new file with the name 5.
So the solution: Delete the : date >= 5 days. You can leave the NUL portion, as that legitimately redirects any output to NUL (nothing) so that it's not displayed. So your command would look like this:
forfiles /M *.bak /p "Z:\Logs" /S /D -5 /C "cmd /c del #file >NUL"

If Batch File Modified Date is older than 90 days, delete files

Basically what I'm trying to do is create a batch file and place it in my startup that will use the modified date of the same batch file and see if it is greater or less than 90 days old. If it isn't, nothing happens and life goes on, but if it is, I want it to delete the contents of my downloads folder. I have a little bit of coding, but I've been testing it, but for some reason it isn't wanting to work for me. Any help would be greatly appreciated.
forfiles -p "C:\LOCATION OF .BAT\" -s -m rmdownload.bat /D -90 /C "cmd /c del C:\user folder\Downloads"
I'm just starting out using batch commands and would really love the help
This works for you if you are using Win2003 or WinXP and have forfiles.exe installed on your machine...
forfiles -p "C:\path_of_your_bat" -s -m rmdownload.bat -d -90 -c "cmd /c del C:\user folder\Downloads"
Later versions of Windows and Windows Server have it installed by default.
For Win7 or higher: Syntax has changed a little therefore the updated command is:
forfiles -p "C:\path_of_your_bat" -s -m rmdownload.bat /D -90 /C "cmd /c del C:\user folder\Downloads"
Assuming your target path contains spaces etc, then use this in your forfiles command. The 0x22 represents a double quote character.
If it echos the command then try it without the echo to actually perform the deletion
"cmd /c echo del 0x22C:\user folder\Downloads\*.*?0x22"

Delete sub directories older than 30 days

I'm trying to create a batch script that will delete all sub directories which are older than 30 days.
I'm really new to batch scripting so I'm just hoping that someone can help me out on this.
I was able to find a script that delete all sub directories in specified location.
#for /f "tokens=*" %%a in ('dir /s /b "C:/temp/test" 2^>NUL') do rd /s /q "%%a"
Can someone tweak this script so it deletes only directories which are 30 days old?
I think FORFILES is going to do it...
FORFILES -D 30 will iterate all files last modified over 30 days ago
So, something like...
FORFILES /S /D -30 /C "cmd /c IF #isdir == TRUE rd #path"
Should (untested) recursively remove all folders older than 30 days :)
Btw - Check out http://ss64.com/nt/ for more command line goodness
This is working fine for me:
Posted by Matt Roberts --> FORFILES /S /D -30 /C "cmd /c IF #isdir == TRUE rd #path"
Be careful when you start the ".bat" file with "Run as administrator", it's not using the current location of the file it takes this location --> "C:\Windows\system32"
For savety reasons add the /P "C:\temp"
FORFILES /P C:\temp /S /D -30 /C "cmd /c IF #isdir == TRUE rd #path"
Assuming you are restricted to batch files... check this - but if you can use VBScript, I think you can do a bit better.
Now that you have clarified your real problem, please understand that if you meant to use System.exec() you will have to contend with plenty of drawbacks.
Alternatively if you really want to use JNDI (you mention "native" but don't make clear what you mean) maybe this will be of help: http://mindprod.com/products1.html#FILETIMES
Do you have powershell on your system?
dir | ? { $_.LastWriteTime -lt [DateTime]::Now.AddDays(-30) } | % { rm $_.FullName }
I know this is an older topic; however, I was recently looking to do this and was able to by building off of what Matt Roberts suggested. I used the following FORFILES syntax:
FORFILES /P \path\to\dir /S /C "cmd /c IF #isdir == TRUE rmdir /S #path /Q" -D -30
You will need to replace \path\to\dir with the path (including drive letter) to the directory (/P) you want to run this on. This will then recursively (/S) search through the directory and run (/C) rmdir (again recursively). Without /Q you are asked if you want to delete each dir/file so if you are planning on running this silently in a script you will need to have it. You can change the amount of days (-D) by changing -30 to something else.
I then used Task Scheduler to automate this once every week (confirmed working). To make sure it works, set the task with 'run with highest privileges'
Note: This setup will irrevocably delete the files. They will not appear in your recycle bin. So once they are gone they're gone (unless you have backups).
You might want to consider using Windows Script Host, in which you could run a VBScript to do just that. Think of it like a batch file with more flexibility and power.
See http://www.computing.net/answers/programming/wsh-delete-files-in-folder-by-date/16255.html
This is not going to be a simple tweak. As it stands, there is no easy way (that I know of) to do the date-diff operation in the cmd interpreter.
First you'll have to parse the current date:
C:\>date /T
Thu 08/05/2010
Once you have the day, month, year, you have to get the date for each item in the directory listing, which you can do by removing the /b param in the dir command and modifying the tokens line to give you tokens 1 and 4, i.e. tokens=1,4, and then doing the date math yourself, which will be a pain because you have to handle different lenght months, and December -> January transitions (new years), etc, and you're almost certain to get it wrong.
Also add the /ad param to get just directories.
I would recommend using PowerShell or some other scripting technology that gives you the tools you need to manipulate dates.

Resources