?
Solved

Delete Folder and its contents in a batch file

Posted on 2016-08-30
12
Medium Priority
?
127 Views
Last Modified: 2016-09-04
I have files that are backed up in folders with date and time stamp. I need to delete the folders that have date and timestamp that are older then 30 days. I also want to delete the contents in those folders.

Example Folder Name Test_20160630, Test_20160730

Can someone please help.

Thanks
0
Comment
Question by:Vicki05
[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
  • 6
  • 5
12 Comments
 
LVL 16

Expert Comment

by:FOX
ID: 41776321
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 41776323
Here is a BAT script that will do this task.  Change the SET near the top to indicate the base folder where the folders you want to remove exist.

Currently the script is set to only display the folders that would be deleted, and their age.  If you like the results and it looks like that would be right, remove the word ECHO on the RD line, and run again.  It will delete all the folders then though, so test carefully first.

It assumes all folders in the base folder will have YYYYMMDD as the rightmost 8 characters.  It may not work properly is that assumption is not true.

@echo off
setlocal EnableDelayedExpansion

REM Define base for folder, and days to keep old folders
set BaseDir=c:\temp
set DaysToKeep=30

REM Get current date in YYYYMMDD format, convert to julian
set Today=
for /f "tokens=* skip=1" %%A in ('wmic os get LocalDateTime') do (
  if not defined LocalDateTime (
    set LocalDateTime=%%A
    set Today=!LocalDateTime:~0,8!
    call :jDate jToday !Today!
  )
)

REM Process all files, delete if too old
for /f "tokens=*" %%A in ('dir /ad /b "%BaseDir%\*"') do (
  set DirName=%%~nA
  call :jDate jDirDate !DirName:~-8!
  set /A DirAge = !jToday! - !jDirDate!
  if !DirAge! GTR %DaysToKeep% (
    echo Removing Dir:[%BaseDir%\%%~A], Age:[!DirAge! days]
    ECHO rd /s /q "%BaseDir%\%%~A"
  )
)

REM Done
exit /b

REM Subroutine to calculate julian date from (YYYYMMDD) date
:jDate [return-variable] [date-string]
  set DateStr=%~2
  set yy=%DateStr:~0,4%
  set /A mm=1%DateStr:~4,2%-100
  set /A dd=1%DateStr:~6,2%-100
  set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
  set /a %~1=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
  exit /b

Open in new window

~bp
0
 

Author Comment

by:Vicki05
ID: 41776432
Hi Bill,

The batch file deleted folder that did not have a date time stamp on it which I did not want. The folder was created today. The folders that I want deleted are labeled as Test_20160829-121525 for example. The date and time changes. Every time the system is rebooted. It moves the test folder with date and time stamp. It creates a new folder Test and creates a new backup.

I would like the batch file to only look for files that have a date and time stamp and delete folds including its content Test_20160829-121525, Test_20160629-111543
untitled.bmp
untitled2.bmp
untitled3.bmp
untitled4.bmp
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

Author Comment

by:Vicki05
ID: 41776433
Forefiles does not work on xp as it is not resident in this version of Windows XP
0
 
LVL 56

Accepted Solution

by:
Bill Prew earned 2000 total points
ID: 41776465
Okay, I have adjusted for the new folder naming format you have mentioned.  It will only look for folders matching the pattern *_????????-?????? which should narrow it down to just the ones you want.  It doesn't test that the date and time fields are indeed date and time fields, but I don't think that will be needed with this additional check (it would be a bit hard to implement in a BAT script, not impossible, but a bit more work).  Test this and see how it goes.

@echo off
setlocal EnableDelayedExpansion

REM Define base for folder, and days to keep old folders
set BaseDir=c:\temp
set DaysToKeep=30

REM Get current date in YYYYMMDD format, convert to julian
set Today=
for /f "tokens=* skip=1" %%A in ('wmic os get LocalDateTime') do (
  if not defined LocalDateTime (
    set LocalDateTime=%%A
    set Today=!LocalDateTime:~0,8!
    call :jDate jToday !Today!
  )
)

REM Process all files, delete if too old
for /f "tokens=*" %%A in ('dir /ad /b "%BaseDir%\*_????????-??????"') do (
  set DirName=%%~nA
  call :jDate jDirDate !DirName:~-15,8!
  set /A DirAge = !jToday! - !jDirDate!
  if !DirAge! GTR %DaysToKeep% (
    echo Removing Dir:[%BaseDir%\%%~A], Age:[!DirAge! days]
    ECHO rd /s /q "%BaseDir%\%%~A"
  )
)

REM Done
exit /b

REM Subroutine to calculate julian date from (YYYYMMDD) date
:jDate [return-variable] [date-string]
  set DateStr=%~2
  set yy=%DateStr:~0,4%
  set /A mm=1%DateStr:~4,2%-100
  set /A dd=1%DateStr:~6,2%-100
  set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
  set /a %~1=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
  exit /b

Open in new window

~bp
0
 

Author Comment

by:Vicki05
ID: 41776552
Hi Bill,

The above batch files seems to work. Quick question. If I want to insert this part in another batch file. I do I prevent it from exiting?
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 41776601
These statements are where my script exits and stops executing.

REM Done
exit /b

Open in new window

These are needed because there is a subroutine that follows that, and without the EXIT /B the script would just step into the subroutine as if it was the main code body.

You can add the main logic of my script in yours as needed, and place the subroutine JDATE after all the merged code, witht he EXIT /B right before it.

~bp
0
 

Author Comment

by:Vicki05
ID: 41782126
Hi Bill,

Thanks for the help. I have had times where the folder does not get deleted. I have to reboot then run the batch file again. Do you know what would cause this?
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 41783316
When the directory isn't deleted, do you get the "Removing Dir:" message?

My initial guess would be that another program has that folder in use, so it can't be deleted.

~bp
0
 

Author Comment

by:Vicki05
ID: 41783364
I do not get any message, it just not delete.
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 41784131
I don't see any reason in the code why it would skip folder that match the pattern and date criteria.  You will have to look for folder usage by other programs, or user permission issues, etc.

~bp
0
 

Author Closing Comment

by:Vicki05
ID: 41784218
Thanks for your help. I really appreciate it. I think the folder might have been open as you said.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

801 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