I want to delete any files that are older than a certain, arbitrary date (which date I wish to designate manually in my code).
How can I get the timestamps of the files concerned and compare them with my arbitrary date? I know I can get the timestamps with something like:
for %%f in (./*) do echo %%~tf
...and I can compare strings or numbers with gtr and lss, but I can't very well compare the timestamps as strings (because they'd be compared alphabetically instead of chronologically).
Use FORFILES with the /D option. (type FORFILES /? from a command prompt for help).
For example, the following will delete all files from the current directory that have not been modified since March 1, 2011:
forfiles /d -3/1/2011 /c "cmd /c if #isdir==FALSE del #file"
It is possible to work with dates returned by %%~tf, but it is a royal pain; especially if you want your solution to work on multiple machines, possibly with different date format settings.
Related
I'm trying to automatically copy 5 .xlsx files from the server to my desktop. I will do this every Monday. The files are from previous week (Monday, Tuesday, Wednesday, Thursday, Friday)
My date scripting is not working. Can someone help me?
C:\Users\jgi>forfiles /P C:\Users\jgi\Documents /S /D +(today'date - 7 days)
ERROR: Invalid argument/option - '-'.
Type "FORFILES /?" for usage.
According to your requirements, you will need something like this:
#echo off
forfiles /P C:\Users\jgi\Documents /S /M *.xlsx /D -8 /C "cmd /c copy #file D:\Desktop\"
/P option specifies the directory to search for the files.
/S is used to say forfiles to search files in all subdirectories of location specified in /P.
/M specifies which files should be processed.
/D Selects files with a last modified date greater than or equal to (+), or less than or equal to (-).
/C specifies command to be run for each file found.
cmd /c tells system to open a new cmd which will will carry out the command specified by string and then it will terminate.
Now, copy #file (forfiles variable; returns the name of the file) to a custom destination.
I'm trying to create a batch file in Win7 that will copy any files that have been created or modified today and copy them to a destination with a similar directory structure. This is what I have so far:
set today="20180721"
robocopy "C:\temp\" "D:\backup\temp\" *.* /s /DCOPY:T /MINAGE:%today%
I know that /e copies empty directories and /xf excludes all files, but I'm not sure if that helps me. The code above seems to copy all files regardless of date, so I'm a little lost here.
Assigning quotes to your variables is not a best practice and will cause problems with some commands if you try to quote the variable later on. Regardless that was not your problem. Your problem is you need to use the /MAXAGE option. Reading the help file you should see this:
/MAXAGE:n : MAXimum file AGE - exclude files older than n days/date.`
So your code should be:
set "today=20180721"
robocopy "C:\temp\" "D:\backup\temp\" *.* /s /DCOPY:T /MAXAGE:%today%
Going to assume you thought the options were for INCLUDE.
robocopy's /MINAGE//MAXAGE options regard the full date and time, so specifying something like /MAXAGE:1 filters for files that have been modified within the last 24 hours.
If you want to process files which have been modified today only, hence regarding the date but not the time, you could use forfiles and its '/D' option, like this:
set "DEST=D:\backup\temp"
forfiles /P "C:\temp" /D +0 /C "cmd /C if #isdir==FALSE for %%Z in (#relpath) do #(2> nul md 0x22%DEST%\%%~Z\..0x22 & copy #relpath 0x22%DEST%\%%~Z0x22)"
So I've got a batch file that backs up a folder to my Google Drive directory, like so:
C:\Program Files\WinRAR\rar.exe a -r "D:\Google Drive\Saves Backup\%DATE%.rar" "D:\Documents\My Games\"
This makes a file called 30-Sep-12.rar (being run today) in the appropriate folder.
However, my question is this: Is there some way to go through said folder (D:\Google Drive\Saves Backup) and delete backups that are more than a week old, as determined by the filename?
Why must you use the date embedded within the file name? The last modified date should be the same as the date embedded in the file name as long as the backup has not been modified since it was created.
FORFILES is one of the few Windows utilities that conveniently works with date arithmetic. Type FORFILES /? from the command line to get help on its usage.
forfiles /p "D:\Google Drive\Saves Backup" /m "*.rar" /d -7 /c "cmd /c del #path"
If you have a risk that someone could modify a backup, thus changing the last modified date, then the above will not work. Parsing and comparing dates is a pain in batch. You would be better off using VBScript.
I have written code in window batch file (eq. getFiles.bat ) that get all files within select date range.
eq.
xcopy /S /D:01-10-2011 *.* C:\todaysFiles
But I want get all files in between two dates including From date and To date.
file extension is .cmd or .bat
If you're on Vista/Win7/WinServer2008 you can use robocopy like so:
robocopy c:\source c:\destination *.* /MAXAGE:20101231 /MINAGE:20111001
On XP, I'm not sure if there are built-in solutions short of using Powershell and the like.
The title of this question is slightly misleading. Based on the questioner's xcopy example, I believe the questioner wants to know "How to copy all files created...", not how to get all files (which I equate with list).
The questioner also states "file extension is .cmd or .bat". This could mean that the questioner wants to copy only files with these extensions but I think it means the questioner would like a batch script solution.
To use the following solution, open a command prompt and chdir to the folder where the files to be copied are located. Then enter the commands listed below, changing the SET values as appropriate.
SET DESTINATION=C:\destination
SET DATE_FROM=01/01/2005
SET DATE_TO=01/01/2007
> nul forfiles /S /D +%DATE_FROM% /C "cmd /C if #isdir==FALSE 2> nul forfiles /M #file /D -%DATE_TO% && > con ( echo #path && copy /V #path %DESTINATION% )"
Note: this will copy files in subfolders as well as files in the top-level folder.
The SET values could be hard-coded directly into the > nul forfiles... line, meaning only one line is required, but for clarity I've used variable substitution.
A caveat is that it is based on date modified (original question asked for date created)
Credit to aschipfl (https://stackoverflow.com/a/36585535/1754517) for providing the inspiration for my answer.
You can also use the forfiles command. It can search recursively in all subfolders /s for files created in the selected root folder /p <Path>, execute commands on the selected files /c "<Command>", apply masks to search /m <SearchMask>, between a date range /d [{+|-}][{<Date>|<Days>}]
more info here, and here
Here's a batch for viewing files by creation date by year. Easily changed to cmd prompt by removing the extra percentage symbols.
for %%a in (2011 2012 2013 2014 2015 2016 2017) do (
for /f %%i in ('xxcopy i:\podcasts\*.* /LL /ZS /Q /FC /DA:%%a-01-01 /DB:%%a-12-31 ^| find /c /v ""') do echo %%a: %%i
)
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.