Discussion forum for all Windows batch related topics.
- Posts: 1
- Joined: 01 Aug 2018 09:00
So, I'm trying to make a bat file that copies data but after so many days I want it to delete folders as well. My plan is to have the script write a log giving each backup event a number that counts up 1 every time it runs then after 30 days it will delete the folder. So if the script made backup number 35 it would delete backup number 5. At the moment I have no Idea how to make a log that would count like this or would I know how to make my bat count back so many days to find the file to delete. If it helps here is the way I create my backup folder.
Code: Select all
set t = %date:~4,2%-%date:~7,2%-%date:~10,4%@%time:~0,2%.%time:~3,2%
set destination = C:\OfficeBackups\%t%
echo Creating directory
I do not want to have to download anything all I want is to know if it is possible to make a database type txt file that keeps counting up 1 every time a file is added and deletes 1 file after 30 other files have been added. If you know a way to avoid having to pull from my log idea please tell me.
- Posts: 3643
- Joined: 22 Jan 2010 18:01
- Location: Germany
May I suggest a different way? As far as I understood you create subfolders in C:\OfficeBackups where you want to keep the 30 newest folders. If there are more than 30 folders then the old folders that exceed this limit shall be deleted. Is that correct?
Code: Select all
for /f "skip=30 delims=" %%i in ('dir /ad /b /tc /o-d') do ECHO rd /s /q "%%i"
This code doesn't delete anything yet. It only shows the commands. If the right folders were displayed then remove ECHO and PAUSE to perform the deletion.