Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
Insomnijack
- Posts: 2
- Joined: 23 Jan 2023 12:42
#1
Post
by Insomnijack » 23 Jan 2023 13:32
Im trying to make a bat file that i can use with task scheduler to backup documents every day and then after 7 days id like it to delete the oldest day. Problem is, when this destination folder is created its created with the right timestamp first, then instantly changes to an older date then it gets instantly deleted. Note its the same issue if i use /MIR instead of /e, or remove /xo.
Code: Select all
set to=%~dp0
set from1=C:\Users\Insomnijack\Documents
set command=/e /xo /R:2 /W:1 /Xf backupalternative.bat
set deletein=7
robocopy %from1% %to%\%date:/=_% %command%
ForFiles /p %to% /d -%deletein% /c "cmd /c IF @isdir == TRUE rd /S /Q @path"
Any help is appreciated, thanks!
-
xiro
- Posts: 34
- Joined: 21 May 2014 00:17
#2
Post
by xiro » 29 Jan 2023 18:50
Just make to batch file one for creating a backup then the second is the one that deletes every 7th of the week then call them altogether with one batch problem solve
-
Insomnijack
- Posts: 2
- Joined: 23 Jan 2023 12:42
#3
Post
by Insomnijack » 06 Feb 2023 09:54
That doesnt seem to work, even without the forfiles line it still reverts back to an old date. So the issue seems to be with robocopy
-
pieh-ejdsch
- Posts: 240
- Joined: 04 Mar 2014 11:14
- Location: germany
#4
Post
by pieh-ejdsch » 05 Mar 2023 08:34
As it looks in your case, everything can be the same in 7 backups.
You can use the error level of Robocopy to know if something was copied or not. Depending on the result, you can then delete the oldest or the previously created folder.
You can also check the old backup(s) for timestamps from seven days ago - if there are none, it should be an older backup. But I would be very careful with this, because there might be no backup with freshly changed data.
Remains as an option: In the currently written backup, the folder name gets a timestamp to allow sorting. Or a fresh .log or similar is inserted into the folder - so the backup folder gets the current date. Then let sort this .log by Robocopy with timestamp and delete the folder accordingly.