thandel
asked on
How do delete a folder after X days old
Using Windows XP, I have the following batch file:
@echo off
cls
SET month=%DATE:~4,2%
SET day=%DATE:~7,2%
SET year=%DATE:~-4%
SET hour=%time:~0,2%
SET sec=%time:~3,2%
SET millis=%time:~6,2%
set folder=%year%_%month%%day% _%hour%%se c%
rem remove spaces from single digit values
set folder=%folder: =%
mkdir %folder%
:Done
This makes a folder based and named based on date and time. This folder will later be populated for backup purposes. Of course this can get rather large so I would like to also add to the batch to check for any folders X days old and then delete those folders.
Is this possible?
Thank you
@echo off
cls
SET month=%DATE:~4,2%
SET day=%DATE:~7,2%
SET year=%DATE:~-4%
SET hour=%time:~0,2%
SET sec=%time:~3,2%
SET millis=%time:~6,2%
set folder=%year%_%month%%day%
rem remove spaces from single digit values
set folder=%folder: =%
mkdir %folder%
:Done
This makes a folder based and named based on date and time. This folder will later be populated for backup purposes. Of course this can get rather large so I would like to also add to the batch to check for any folders X days old and then delete those folders.
Is this possible?
Thank you
Unless you have a particular reason to go the DOS route to delete older backups, it might make more sense to go for a backup program that keeps a certain number (that you specify .. e.g. 10) of backups and when it created a new backup, the oldest one is deleted.
This is just one example of such a program ...
http://www.kls-soft.com/klsbackup/
This is just one example of such a program ...
http://www.kls-soft.com/klsbackup/
ASKER
Thanks NewVillageIT... will this work for a folder/directory?
ASKER
I'm getting an error that Forfiles is not a recognizable command.
ASKER
I have Windows XP so I don't think XP supports this command.
The @isdir==TRUE option checks for directories.
Before doing your real folders, test against an area that has some folders and files.
I haven't confirmed this but, it's in the Windows 2003 Resource Kit at http://www.microsoft.com/en-us/download/details.aspx?id=17657
XP versions here: http://www.petri.com/download_windows_xp_reskit_tools.htm
Before doing your real folders, test against an area that has some folders and files.
I haven't confirmed this but, it's in the Windows 2003 Resource Kit at http://www.microsoft.com/en-us/download/details.aspx?id=17657
XP versions here: http://www.petri.com/download_windows_xp_reskit_tools.htm
ASKER
What is that 12MB installation going to do, install several .exe tool files? Can you attach the forfile to this? I only need that and not the other 12+MB supporting files.
ASKER
OK never mind I installed the kit but forfiles was not part of the installation.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks now getting an error invalid argument/option with forfiles
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
thandel,
Did you get it working? If not, can you post a sample code?
Did you get it working? If not, can you post a sample code?
ASKER
Getting an error that "P" can not be empty. Here is my code:
@echo off
cls
SET month=%DATE:~4,2%
SET day=%DATE:~7,2%
SET year=%DATE:~-4%
SET hour=%time:~0,2%
SET sec=%time:~3,2%
SET millis=%time:~6,2%
SET DAYS_TO_KEEP=-1
set folder=%year%_%month%%day% _%hour%%se c%
rem remove spaces from single digit values
set folder=%folder: =%
set set BAKPath=C:\Documents and Settings\Todd\My Documents\DbBAK\%folder%
mkdir %folder%
cd %folder%
forfiles /d %DAYS_TO_KEEP% /p "%BAKpath%" /c "cmd /c if @isdir==TRUE rd /s /q @path"
@echo off
cls
SET month=%DATE:~4,2%
SET day=%DATE:~7,2%
SET year=%DATE:~-4%
SET hour=%time:~0,2%
SET sec=%time:~3,2%
SET millis=%time:~6,2%
SET DAYS_TO_KEEP=-1
set folder=%year%_%month%%day%
rem remove spaces from single digit values
set folder=%folder: =%
set set BAKPath=C:\Documents and Settings\Todd\My Documents\DbBAK\%folder%
mkdir %folder%
cd %folder%
forfiles /d %DAYS_TO_KEEP% /p "%BAKpath%" /c "cmd /c if @isdir==TRUE rd /s /q @path"
ASKER
Sorry cut n' paste error but didn't resolve the P variable error:
Should be: set BAKPath=C:\Documents and Settings\Todd\My Documents\DbBAK
Should be: set BAKPath=C:\Documents and Settings\Todd\My Documents\DbBAK
ASKER
I have it working... my set command had typos "set set".... working now!
Thanks
Thanks
ASKER
Does everyone agree that a 50/50 split is fair? Ultimately used ZabagaR's forfile line
OK, thandle.
Open in new window