Xerxes321
asked on
VBScript or batch file to delete folders older than a week
Hello,
I cannot for the life of me find a vbscript or batch file to delete folders older than a week. The script needs to check the date the folder was created and if the folder is older than a week, delete it and anything inside of it.
I'm using it on folders of digital pictures, so I cannot just use a script that checks file dates/times, as the pictures may be a year or more old. It needs to just check the folder created date/time.
Is this possible? Any help is appreciated, thank you!
I cannot for the life of me find a vbscript or batch file to delete folders older than a week. The script needs to check the date the folder was created and if the folder is older than a week, delete it and anything inside of it.
I'm using it on folders of digital pictures, so I cannot just use a script that checks file dates/times, as the pictures may be a year or more old. It needs to just check the folder created date/time.
Is this possible? Any help is appreciated, thank you!
What is your date format?
ASKER
I'm using Windows XP. USA, MM/DD/YYYY
Actually XP USA is
M/d/yyyy
reg query "HKCU\Control Panel\International" /v sShortDate
When the shortdate is set to MM/dd/yyyy the day of the week does not show up when you do a
echo %date%
M/d/yyyy
reg query "HKCU\Control Panel\International" /v sShortDate
When the shortdate is set to MM/dd/yyyy the day of the week does not show up when you do a
echo %date%
ASKER
Sorry, I was just going by what I say when using 'dir' in the command prompt.
! REG.EXE VERSION 3.0
HKEY_CURRENT_USER\Control Panel\International
sShortDate REG_SZ M/d/yyyy
! REG.EXE VERSION 3.0
HKEY_CURRENT_USER\Control Panel\International
sShortDate REG_SZ M/d/yyyy
No problem. I really only needed to know month day year in this case.
Removing subfolders too right? RD /q /s will remove everything.
Change the folder
Set Folder=C:\AT
When everything looks OK. Remove the ECHO from this line
ECHO RD /q /s "%Folder%\%%e"
Removing subfolders too right? RD /q /s will remove everything.
Change the folder
Set Folder=C:\AT
When everything looks OK. Remove the ECHO from this line
ECHO RD /q /s "%Folder%\%%e"
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
Set Folder=C:\AT
call :SubDays 7
for /f "tokens=1,2,3,6,* delims=/ " %%a in ('dir /tc /ad "%Folder%" ^| find "/"') do (
if %%c%%a%%b LSS %Year%%Month:~-2%%Day:~-2% (
ECHO Removing folder "%Folder%\%%e" creation date was %%a/%%b/%%c
ECHO RD /q /s "%Folder%\%%e"
)
)
goto :EOF
:SubDays
Set DaysInMonth=31,28,31,30,31,30,31,31,30,31,30,31
Set Year=%date:~-4%
Set Day=1%date:~-7,2%
Set Month=1%date:~-10,2%
Set /a Day-=%1
:FixDay
If !Day! LEQ 100 (
Set /a Month-=1
if !Month! == 100 (
Set /a Year-=1
Set Month=112
)
Set MCounter=101
Set DaysInThisMonth=
for %%d in (%DaysInMonth%) do if not defined DaysInThisMonth if !MCounter! == !Month! (Set DaysInThisMonth=%%d) else (Set /a MCounter+=1)
if !Month! == 102 (
if %Year:~-2% == 00 (
Set /a Mod=!Year:~0,2! %% 4
if !Mod! == 0 Set /a DaysInThisMonth+=1
) else (
Set /a Mod=!Year! %% 4
if !Mod! == 0 Set /a DaysInThisMonth+=1
)
)
Set /a Day+=!DaysInThisMonth!
)
If !Day! LEQ 100 GOTO FixDay
GOTO :EOF
If you want to go older than x days modify this line
call :SubDays x
call :SubDays x
ASKER
Can it remove any system or hidden files too? We sometimes copy the entire DCIM folder off of the memory card and sometimes it includes hidden preview/thumbnail pictures that haven't played nice with other scripts we've used.
The RD /q /s should remove all hidden and system files.
This folder is not hidden or a system is it?
This folder is not hidden or a system is it?
ASKER
The top level folder is not, no. Some of them may be in sub directories. Does that make a difference?
No the sub directories and files can be hidden system or read only. The top level folder should disappear.
ASKER
I need it so the top level folder doesn't get checked, but rather everything inside of it does.
I have the folders in D:\Folders and when I set it to check that directory, it's checking the timestamp on D:\Folders and not the folders inside of that directory. It's seeing D:\Folders is older than a week and wanting to remove everything inside of it.
I even tried mounting the folder as a network drive and setting the folder as Z:\. But it's wanting to remove Z:\ and not checking the folders inside.
I hope it's a quick fix for you.
I have the folders in D:\Folders and when I set it to check that directory, it's checking the timestamp on D:\Folders and not the folders inside of that directory. It's seeing D:\Folders is older than a week and wanting to remove everything inside of it.
I even tried mounting the folder as a network drive and setting the folder as Z:\. But it's wanting to remove Z:\ and not checking the folders inside.
I hope it's a quick fix for you.
ASKER
Tried everything I could think of to get this to work as is, but it's always checking the timestamp on the folder I set it to, rather than the folders inside of that folder.
ASKER
After playing with this a bit and making a bunch of copies of the batch with the folder set differently in each one, I noticed I'm still going to have a problem.
Let me explain exactly what our machines are doing in the hopes you don't have to waste any more time because I wasn't clear enough in the beginning:
1) On the D: drive, there is a folder called PhotoCenterCache.
2) Inside this folder are a bunch of different folders named in this format: YYYYMMDD - example: 20090126, 20090107, etc.
3) Inside each of those folders are more folders named in this format: Import Media, Import Media_1, Import Media_2, etc.
4) The Import Media folders are the folders that have all of the photos in them. These are the folders that need to be removed if the creation time on the folder is older than a week.
5) Once all of the folders and their files have been cleared out and there's nothing left in one of the YYYYMMDD format folders, that folder can be deleted.
I realized after I was playing with this that even if you can get it working to not delete the folder I set it to check, it's going to see the creation time on the YYYYMMDD folder is possibly older than a week and want to remove everything inside of it. This scenario can only happen if the folder is actually empty. If a file is modified somewhere in a subfolder within the YYYYMMDD folder, does it update the last written/modified date for the YYYYMMDD folder? This I do not know. It might be easier to just check when it's empty and then remove it.
If you need any more clarification before proceeding, please ask. Again, I apologize for trying to generalize my situation, resulting in something that is not going to work properly.
Sample directory structure:
Volume in drive D is Local Disk
Volume Serial Number is
Directory of d:\photocentercache
01/29/2009 07:59 PM <DIR> .
01/29/2009 07:59 PM <DIR> ..
01/30/2009 07:24 PM <DIR> 20090126
0 File(s) 0 bytes
Directory of d:\photocentercache\200901 26
01/30/2009 07:24 PM <DIR> .
01/30/2009 07:24 PM <DIR> ..
01/26/2009 10:48 AM <DIR> Import Media
01/26/2009 10:54 AM <DIR> Import Media_1
01/27/2009 09:54 AM <DIR> Import Media_10
01/27/2009 11:00 AM <DIR> Import Media_11
01/29/2009 08:58 AM <DIR> Import Media_12
01/29/2009 08:57 AM <DIR> Import Media_13
01/29/2009 08:55 AM <DIR> Import Media_14
01/29/2009 08:55 AM <DIR> Import Media_15
01/29/2009 08:55 AM <DIR> Import Media_16
01/29/2009 08:55 AM <DIR> Import Media_17
01/29/2009 08:55 AM <DIR> Import Media_18
01/29/2009 08:54 AM <DIR> Import Media_19
01/26/2009 01:41 PM <DIR> Import Media_2
01/29/2009 08:43 AM <DIR> Import Media_20
01/29/2009 08:43 AM <DIR> Import Media_21
01/29/2009 08:43 AM <DIR> Import Media_22
01/29/2009 08:45 AM <DIR> Import Media_23
01/29/2009 11:23 AM <DIR> Import Media_24
01/29/2009 11:29 AM <DIR> Import Media_25
01/29/2009 11:31 AM <DIR> Import Media_26
01/29/2009 01:46 PM <DIR> Import Media_27
01/29/2009 03:18 PM <DIR> Import Media_28
01/29/2009 04:26 PM <DIR> Import Media_29
01/26/2009 01:12 PM <DIR> Import Media_3
01/30/2009 08:43 AM <DIR> Import Media_30
01/30/2009 09:50 AM <DIR> Import Media_31
01/30/2009 11:53 AM <DIR> Import Media_32
01/30/2009 12:29 PM <DIR> Import Media_33
01/30/2009 01:15 PM <DIR> Import Media_34
01/30/2009 02:09 PM <DIR> Import Media_35
01/30/2009 02:34 PM <DIR> Import Media_36
01/30/2009 05:38 PM <DIR> Import Media_37
01/30/2009 05:36 PM <DIR> Import Media_37.$ed
01/30/2009 05:55 PM <DIR> Import Media_38
01/30/2009 06:32 PM <DIR> Import Media_39
01/26/2009 01:57 PM <DIR> Import Media_4
01/30/2009 07:24 PM <DIR> Import Media_40
01/26/2009 02:39 PM <DIR> Import Media_5
01/26/2009 06:14 PM <DIR> Import Media_6
01/26/2009 07:51 PM <DIR> Import Media_7
01/27/2009 08:22 AM <DIR> Import Media_8
01/27/2009 08:30 AM <DIR> Import Media_9
0 File(s) 0 bytes
Directory of d:\photocentercache\200901 07
01/30/2009 07:24 PM <DIR> .
01/30/2009 07:24 PM <DIR> ..
01/26/2009 10:48 AM <DIR> Import Media
01/26/2009 10:54 AM <DIR> Import Media_1
01/27/2009 09:54 AM <DIR> Import Media_10
01/27/2009 11:00 AM <DIR> Import Media_11
01/29/2009 08:58 AM <DIR> Import Media_12
etc..
Directory of d:\photocentercache\200901 26\Import Media
01/26/2009 10:48 AM <DIR> .
01/26/2009 10:48 AM <DIR> ..
01/26/2009 07:22 AM 36,331 100_0206.jpg
01/26/2009 07:22 AM 111,547 100_0209.jpg
01/26/2009 07:22 AM 132,203 100_0211.jpg
01/19/2009 06:43 PM 1,258,788 100_0213.jpg
01/19/2009 06:44 PM 1,297,067 100_0222.jpg
etc..
Let me explain exactly what our machines are doing in the hopes you don't have to waste any more time because I wasn't clear enough in the beginning:
1) On the D: drive, there is a folder called PhotoCenterCache.
2) Inside this folder are a bunch of different folders named in this format: YYYYMMDD - example: 20090126, 20090107, etc.
3) Inside each of those folders are more folders named in this format: Import Media, Import Media_1, Import Media_2, etc.
4) The Import Media folders are the folders that have all of the photos in them. These are the folders that need to be removed if the creation time on the folder is older than a week.
5) Once all of the folders and their files have been cleared out and there's nothing left in one of the YYYYMMDD format folders, that folder can be deleted.
I realized after I was playing with this that even if you can get it working to not delete the folder I set it to check, it's going to see the creation time on the YYYYMMDD folder is possibly older than a week and want to remove everything inside of it. This scenario can only happen if the folder is actually empty. If a file is modified somewhere in a subfolder within the YYYYMMDD folder, does it update the last written/modified date for the YYYYMMDD folder? This I do not know. It might be easier to just check when it's empty and then remove it.
If you need any more clarification before proceeding, please ask. Again, I apologize for trying to generalize my situation, resulting in something that is not going to work properly.
Sample directory structure:
Volume in drive D is Local Disk
Volume Serial Number is
Directory of d:\photocentercache
01/29/2009 07:59 PM <DIR> .
01/29/2009 07:59 PM <DIR> ..
01/30/2009 07:24 PM <DIR> 20090126
0 File(s) 0 bytes
Directory of d:\photocentercache\200901
01/30/2009 07:24 PM <DIR> .
01/30/2009 07:24 PM <DIR> ..
01/26/2009 10:48 AM <DIR> Import Media
01/26/2009 10:54 AM <DIR> Import Media_1
01/27/2009 09:54 AM <DIR> Import Media_10
01/27/2009 11:00 AM <DIR> Import Media_11
01/29/2009 08:58 AM <DIR> Import Media_12
01/29/2009 08:57 AM <DIR> Import Media_13
01/29/2009 08:55 AM <DIR> Import Media_14
01/29/2009 08:55 AM <DIR> Import Media_15
01/29/2009 08:55 AM <DIR> Import Media_16
01/29/2009 08:55 AM <DIR> Import Media_17
01/29/2009 08:55 AM <DIR> Import Media_18
01/29/2009 08:54 AM <DIR> Import Media_19
01/26/2009 01:41 PM <DIR> Import Media_2
01/29/2009 08:43 AM <DIR> Import Media_20
01/29/2009 08:43 AM <DIR> Import Media_21
01/29/2009 08:43 AM <DIR> Import Media_22
01/29/2009 08:45 AM <DIR> Import Media_23
01/29/2009 11:23 AM <DIR> Import Media_24
01/29/2009 11:29 AM <DIR> Import Media_25
01/29/2009 11:31 AM <DIR> Import Media_26
01/29/2009 01:46 PM <DIR> Import Media_27
01/29/2009 03:18 PM <DIR> Import Media_28
01/29/2009 04:26 PM <DIR> Import Media_29
01/26/2009 01:12 PM <DIR> Import Media_3
01/30/2009 08:43 AM <DIR> Import Media_30
01/30/2009 09:50 AM <DIR> Import Media_31
01/30/2009 11:53 AM <DIR> Import Media_32
01/30/2009 12:29 PM <DIR> Import Media_33
01/30/2009 01:15 PM <DIR> Import Media_34
01/30/2009 02:09 PM <DIR> Import Media_35
01/30/2009 02:34 PM <DIR> Import Media_36
01/30/2009 05:38 PM <DIR> Import Media_37
01/30/2009 05:36 PM <DIR> Import Media_37.$ed
01/30/2009 05:55 PM <DIR> Import Media_38
01/30/2009 06:32 PM <DIR> Import Media_39
01/26/2009 01:57 PM <DIR> Import Media_4
01/30/2009 07:24 PM <DIR> Import Media_40
01/26/2009 02:39 PM <DIR> Import Media_5
01/26/2009 06:14 PM <DIR> Import Media_6
01/26/2009 07:51 PM <DIR> Import Media_7
01/27/2009 08:22 AM <DIR> Import Media_8
01/27/2009 08:30 AM <DIR> Import Media_9
0 File(s) 0 bytes
Directory of d:\photocentercache\200901
01/30/2009 07:24 PM <DIR> .
01/30/2009 07:24 PM <DIR> ..
01/26/2009 10:48 AM <DIR> Import Media
01/26/2009 10:54 AM <DIR> Import Media_1
01/27/2009 09:54 AM <DIR> Import Media_10
01/27/2009 11:00 AM <DIR> Import Media_11
01/29/2009 08:58 AM <DIR> Import Media_12
etc..
Directory of d:\photocentercache\200901
01/26/2009 10:48 AM <DIR> .
01/26/2009 10:48 AM <DIR> ..
01/26/2009 07:22 AM 36,331 100_0206.jpg
01/26/2009 07:22 AM 111,547 100_0209.jpg
01/26/2009 07:22 AM 132,203 100_0211.jpg
01/19/2009 06:43 PM 1,258,788 100_0213.jpg
01/19/2009 06:44 PM 1,297,067 100_0222.jpg
etc..
Hmm... It should be the subfolder. That it was checking. Basically from a command prompt if you do
dir /tc /ad "d:\photocentercache" | find "/"
You should see these folders with their creation dates
20090126
20090107
Try it again with
dir /ad "d:\photocentercache" | find "/"
This should be their modified dates.
But you want to do 1 more level down to RD folder when it was created over 7 days ago.
So the import media folders we'll use rd /q /s which will delete them without prompting. I'll put in a rd to remove 20090126 if it is not empty it will fail because we are not using the /q /s.
dir /tc /ad "d:\photocentercache" | find "/"
You should see these folders with their creation dates
20090126
20090107
Try it again with
dir /ad "d:\photocentercache" | find "/"
This should be their modified dates.
But you want to do 1 more level down to RD folder when it was created over 7 days ago.
So the import media folders we'll use rd /q /s which will delete them without prompting. I'll put in a rd to remove 20090126 if it is not empty it will fail because we are not using the /q /s.
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
Set Folder=C:\AT
call :SubDays 7
for /f "tokens=*" %%p in ('dir /ad /b "%Folder%"') do (
for /f "tokens=1,2,3,6,* delims=/ " %%a in ('dir /tc /ad "%Folder%\%%p" ^| find "/"') do (
if %%c%%a%%b LSS %Year%%Month:~-2%%Day:~-2% (
ECHO Removing folder "%Folder%\%%p\%%e" creation date was %%a/%%b/%%c
ECHO RD /q /s "%Folder%\%%p\%%e"
)
)
ECHO RD "%Folder%\%%p"
)
goto :EOF
:SubDays
Set DaysInMonth=31,28,31,30,31,30,31,31,30,31,30,31
Set Year=%date:~-4%
Set Day=1%date:~-7,2%
Set Month=1%date:~-10,2%
Set /a Day-=%1
:FixDay
If !Day! LEQ 100 (
Set /a Month-=1
if !Month! == 100 (
Set /a Year-=1
Set Month=112
)
Set MCounter=101
Set DaysInThisMonth=
for %%d in (%DaysInMonth%) do if not defined DaysInThisMonth if !MCounter! == !Month! (Set DaysInThisMonth=%%d) else (Set /a MCounter+=1)
if !Month! == 102 (
if %Year:~-2% == 00 (
Set /a Mod=!Year:~0,2! %% 4
if !Mod! == 0 Set /a DaysInThisMonth+=1
) else (
Set /a Mod=!Year! %% 4
if !Mod! == 0 Set /a DaysInThisMonth+=1
)
)
Set /a Day+=!DaysInThisMonth!
)
If !Day! LEQ 100 GOTO FixDay
GOTO :EOF
ASKER
I think it's getting confused. See attached output.
U:\>dir /tc /ad | find "/"
05/04/2008 12:28 PM <DIR> .
05/04/2008 12:28 PM <DIR> ..
01/26/2009 10:41 AM <DIR> 20090126
U:\>dir /ad | find "/"
01/29/2009 07:59 PM <DIR> .
01/29/2009 07:59 PM <DIR> ..
01/31/2009 07:32 PM <DIR> 20090126
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I am waiting a few days to post if it works or not. The folders were manually cleaned up last week, so waiting until some are older than a week to verify everything will work correctly.
I will post back with results by next Tuesday.
I will post back with results by next Tuesday.
Sure. Not a problem.
ASKER
Seems to be working great.
To have it check 2 folders would I just change it to the below? I'll accept as soon as I hear back from you.
To have it check 2 folders would I just change it to the below? I'll accept as soon as I hear back from you.
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
Set Folder=D:\PhotoCenterCache
Set Folder2=C:\Documents and Settings\Administrator\My Documents\My Pictures
call :SubDays 7
for /f "tokens=*" %%p in ('dir /ad /b "%Folder%"') do (
for /f "tokens=1,2,3,6,* delims=/ " %%a in ('dir /tc /ad "%Folder%\%%p" ^| find "/"') do (
if "%%e" neq "." if "%%e" neq ".." if %%c%%a%%b LSS %Year%%Month:~-2%%Day:~-2% (
ECHO Removing folder "%Folder%\%%p\%%e" creation date was %%a/%%b/%%c
RD /q /s "%Folder%\%%p\%%e"
)
)
RD "%Folder%\%%p"
)
for /f "tokens=*" %%p in ('dir /ad /b "%Folder2%"') do (
for /f "tokens=1,2,3,6,* delims=/ " %%a in ('dir /tc /ad "%Folder2%\%%p" ^| find "/"') do (
if "%%e" neq "." if "%%e" neq ".." if %%c%%a%%b LSS %Year%%Month:~-2%%Day:~-2% (
ECHO Removing folder "%Folder2%\%%p\%%e" creation date was %%a/%%b/%%c
RD /q /s "%Folder2%\%%p\%%e"
)
)
RD "%Folder2%\%%p"
)
goto :EOF
:SubDays
Set DaysInMonth=31,28,31,30,31,30,31,31,30,31,30,31
Set Year=%date:~-4%
Set Day=1%date:~-7,2%
Set Month=1%date:~-10,2%
Set /a Day-=%1
:FixDay
If !Day! LEQ 100 (
Set /a Month-=1
if !Month! == 100 (
Set /a Year-=1
Set Month=112
)
Set MCounter=101
Set DaysInThisMonth=
for %%d in (%DaysInMonth%) do if not defined DaysInThisMonth if !MCounter! == !Month! (Set
DaysInThisMonth=%%d) else (Set /a MCounter+=1)
if !Month! == 102 (
if %Year:~-2% == 00 (
Set /a Mod=!Year:~0,2! %% 4
if !Mod! == 0 Set /a DaysInThisMonth+=1
) else (
Set /a Mod=!Year! %% 4
if !Mod! == 0 Set /a DaysInThisMonth+=1
)
)
Set /a Day+=!DaysInThisMonth!
)
If !Day! LEQ 100 GOTO FixDay
GOTO :EOF
ASKER
Thanks. I'll have to try and see if my mod works to check two folders.