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.
Related
Everyday at 1 am some zip folders will come in on one server(let us say A). I need to copy the files to another server(let us say B). After copying the files to server B, then after some time the copied files will moves to another destination. For the next day the previously copied files should not come with the files from today.
Now, the problem is, it is working fine in my Local PC .. It is not working in the server.
I don't have knowledge of batch file programming. Please let me know if there is a way to accomplish this.
My code:
#echo off
forfiles /s /p "\\10.35.0.44\Da\Ra\Folder" /m "*.zip" /d +"02/04/2019" /c "cmd /c move #path \\10.35.0.44\Da\sai"
pause
It is the throwing following error:
Error:UNC paths <\\machine\share> are not supported
Because of this I've used pushD()popD. The error was not there but the files are not being copied.
I'm working with the forfiles command
I am attempting to get a list of files newer than X days ago.
When I use the /D +X command I get a not files found error:
forfiles /P %filename% /D +3
However if I use a date it works fine:
forfiles /P %filename% /D +06/29/2016
Am I missing something on the +X formatting? It seems like it is adding 3 days to the current date and then looking for files newer than that.
Or is there a simpler way to increment the date than I'm aware of. Only work with Batch once in a while
I don't want to use vbs, and I would rather not have a set of code longer than the rest of my script just to get the date, there should be a simpler way.
I want to be able to create a batch files that copies files by date only up to 3 or 4 days but use the %edate% %user%\ syntax instead of specifying a date. Here is what I have so far:
XCOPY %user%\folder_1 %edate% %user%\folder_2.
I am unable to download any 3 party software. I can only use the .bat format.
The comment suggestion to use forfiles is a good one, but I note the question has remained open. Perhaps more details are required.
If the questioner has a system more recent than XP (and I assume so, as XP and previous are now deprecated) the ROBOCOPY also has appropriate options. I'll give examples of both for files greater than 4 days old.
Using forfiles:
FORFILES -P %user%\folder_1 /D - 4 /C "CMD /C XCOPY #file %user\folder_2"
Using ROBOCOPY:
ROBOCOPY %user%\folder_1 %user%\folder_2 /MINAGE:4
I hope that is sufficient detail....
I am looking for a way to zip folders based on a timespan of their lastmodified date and remove them afterwards
the structure is as follows
Folder_1 01.01.2012 12:00
- sub1
- sub2
- file1
- file2
Folder_2 01.01.2012 12:02
...
Folder_20202 05.05.2012 03:00
I want now:
to select all folders that are 3 months older than this month
(e.g. lastmodified is february when current is may). An importand point is that I want to use 1st to last of month and not -90 days
then zip the selected folders with all subfolders and files contained into one february2012.zip
delete the originals of the (now) zipped files
my best idea so far is to use forfiles.exe (which takes only one date), use a generated date (1st day of 3 month ago and use the older than modifier) and create a resultextfile. Then use the resultfile as import for 7zip.exe and somehow loop over the file and delete the folders afterwards (for part 2&3, I have not yet a working code)
-- UPDATE --
At the moment I am experimenting with:
FORFILES /P H:\Temp\2zip /M O* /D -31.4.2012 /C "cmd /c C:\Program Files (x86)\7-Zip\7z.exe a -t7z H:\temp\x2.zip #path
which seems to work - but just very slowly (I guess reason is mapped drive -> 3h for 150MB)
Be careful when working with last modified date of folders. On a FAT file system, the time stamp does not change when the contents of the folder change.
On a NTFS file system, the last modified date is updated whenever a file or folder is created or deleted within the folder. But it is not updated if a file is modified, or if a file/folder is created or deleted in a sub folder.
If you still want to move forward with your plan, then your basic strategy is pretty solid. Windows batch has very weak support for dealing with dates, so there is not much room for improvement.
The best way to parse the current date (and time if you want it) regardless of locale is to use WMIC:
for /f "skip=1" %%A in ('wmic os get localdatetime') do (
set currentDateTime=%%A
goto :exitLoop
)
:exitLoop
set year=%currentDateTime:~0,4%
set month=%currentDateTime:~4,2%
You should be able to use a move option of 7zip such that you don't need to delete the folder afterwards.
You should be able to eliminate the need for a temp file by processing the results of the FORFILES with a FOR /F. Or better yet, you may be able to specify the appropriate 7zip command with the FORFILES /C option.
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.