Solved

How do delete a folder after X days old

Posted on 2014-11-08
18
171 Views
Last Modified: 2014-11-09
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%%sec%

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
0
Comment
Question by:thandel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 5
  • 2
  • +1
18 Comments
 
LVL 24

Expert Comment

by:NVIT
ID: 40430193
Would this help? Change 15 to the DaysOld you want.

FORFILES /d -15 "cmd /c if @isdir==TRUE del /q /s /f @file"

Open in new window

0
 
LVL 23

Expert Comment

by:Eirman
ID: 40430209
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/
0
 

Author Comment

by:thandel
ID: 40430347
Thanks NewVillageIT... will this work for a folder/directory?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:thandel
ID: 40430349
I'm getting an error that Forfiles is not a recognizable command.
0
 

Author Comment

by:thandel
ID: 40430361
I have Windows XP so  I don't think XP supports this command.
0
 
LVL 24

Expert Comment

by:NVIT
ID: 40430381
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
0
 

Author Comment

by:thandel
ID: 40431270
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.
0
 

Author Comment

by:thandel
ID: 40431277
OK never mind I installed the kit but forfiles was not part of the installation.
0
 
LVL 24

Assisted Solution

by:NVIT
NVIT earned 250 total points
ID: 40431293
Attached. Rename .txt to .exe
forfiles.txt
0
 
LVL 15

Expert Comment

by:ZabagaR
ID: 40431295
0
 

Author Comment

by:thandel
ID: 40431378
Thanks now getting an error invalid argument/option  with forfiles
0
 
LVL 15

Accepted Solution

by:
ZabagaR earned 250 total points
ID: 40431518
I already had a batch script to do this from a while back, I'll just repost below:
I keep 14 days and my path to search is C:\MyFolderPath

@echo off
setlocal

set DAYS_TO_KEEP=-14
set BACKUP_PATH=C:\MyFolderPath

forfiles /d %DAYS_TO_KEEP% /p "%BACKUP_PATH%" /c "cmd /c if @isdir==TRUE rd /s /q @path"
0
 
LVL 24

Expert Comment

by:NVIT
ID: 40431532
thandel,

Did you get it working? If not, can you post a sample code?
0
 

Author Comment

by:thandel
ID: 40431856
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%%sec%
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"
0
 

Author Comment

by:thandel
ID: 40431857
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
0
 

Author Comment

by:thandel
ID: 40431858
I have it working... my set command had typos "set set".... working now!

Thanks
0
 

Author Comment

by:thandel
ID: 40431860
Does everyone agree that a 50/50 split is fair?    Ultimately used ZabagaR's forfile line
0
 
LVL 24

Expert Comment

by:NVIT
ID: 40431918
OK, thandle.
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Move XP PC to Vmware 22 164
XP as a dual boot with Windows 10 10 112
Event ID: 1008 / Source: Microsoft-Windows-Perflib 2 240
Event ID: 5719 / Source: NETLOGON 9 177
If you build your web application in Visual Studio you'll get at least a few binaries, or .DLL, files in your bin folder. However, there is more compiling to be done. Normally this would happen when an ASP.NET resource within the web site is request…
Can I legally transfer my OEM version of Windows to another PC?  (AKA - Can I put a new systemboard in my OEM PC?) Few of us are both IT and legal experts but we all have our own views of Microsoft's licensing rules and how they apply.  There are…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Suggested Courses

742 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question