jacksonwsa
asked on
Simple delete folder script
Hello, I'm looking for a script .bat or .vbs (something that will easily work with task scheduler) that will delete a folder that is at least one day old (or anything but the current day) with a specific name. Here's my scenario.
I have many old anti-virus folders getting archived with the date as the folder name here are 3 folder I have currently in my directory and I would like to have a script that deletes the other two - I DO NOT WANT TO DELETE ANY OF THE OTHER FOLDERS IN THAT DIRECTORY. With the 3 folders below it would delete #'s 1. and 2.
1. 20130130 (Date modified 1/31)
2. 20130201 (Date modified 2/2)
3. 20130202 (Date modified 2/3)
I would like to run this script every morning, I'm assuming we can do something like (2013*) for the name piece?
THANKS!
I have many old anti-virus folders getting archived with the date as the folder name here are 3 folder I have currently in my directory and I would like to have a script that deletes the other two - I DO NOT WANT TO DELETE ANY OF THE OTHER FOLDERS IN THAT DIRECTORY. With the 3 folders below it would delete #'s 1. and 2.
1. 20130130 (Date modified 1/31)
2. 20130201 (Date modified 2/2)
3. 20130202 (Date modified 2/3)
I would like to run this script every morning, I'm assuming we can do something like (2013*) for the name piece?
THANKS!
ASKER
This seems pretty complex for something this simple
It is actually not complex. The code above is the vbscript that reads the date all of the folders in the root folder and deletes any "old" folders. This script can be permanent in that you create it once and and just change your calls whether by batch script, command line, or scheduled task to change your variables. I use this all of the time and it makes things super easy.
Example: I have the script on my desktop and I want to delete all folders in my mapped drive g: that are older than 10 days
cscript "%userprofile%\desktop\del eteold.vbs " "G:\" 10
or
Example 2: I have the script on my desktop and I want to delete all folders in my mapped drive g: in the folder test that are older than 21 days
cscript "%userprofile%\desktop\del eteold.vbs " "G:\test" 21
Example: I have the script on my desktop and I want to delete all folders in my mapped drive g: that are older than 10 days
cscript "%userprofile%\desktop\del
or
Example 2: I have the script on my desktop and I want to delete all folders in my mapped drive g: in the folder test that are older than 21 days
cscript "%userprofile%\desktop\del
Hi, try this...
Remove the echo in front of the rd /s /q %%f command if the output is the intended result.
@echo off
setlocal enabledelayedexpansion
cd /d "%~dp0"
set mm=%date:~4,2%
set dd=%date:~7,2%
set yyyy=%date:~10,4%
set yy=%date:~12,2%
if exist !yyyy!!mm!!dd! echo Skipping !yyyy!!mm!!dd!
for /f %%f in ('dir /ad/od/b') do (
if not %%f==!yyyy!!mm!!dd! (
echo rd /s /q %%f
)
)
Remove the echo in front of the rd /s /q %%f command if the output is the intended result.
ASKER
OK, gotcha - thanks PantherTech. I'm going to test this tomorrow after I tried creating a few test folders.
ASKER
Oh I don't know if you noticed my original comment, I also have to have it only select folders that begin with 2013... not just anything older than 1 day. Is there a way I can get that to work?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
cscript "\\server\share$\deleteold
Open in new window