Solved Delete wildcard folders after 180 days

June 16, 2018 at 20:20:51
Specs: Windows Vista (2008 Server, 2012 Servers)
Below is my current script, I am brand new to this so I'm not very wise on what to do and what works with what and what doesn't. My requirements is to backup the folder "wstemp" and zip it. Then enter the original folder and delete folders and their contents that begins with "anonymous" that are older than 180 days then come back out delete the backup. However I can't understand how to get the 180 days bit working, any help appreciated!

@ECHO OFF
CScript zip.vbs E:\Dmgr01\wstemp E:\Dmgr01\wstemp-backup.zip
ECHO Deleting anonymous files in wstemp...
ECHO OFF
timeout 5
FOR /D %%X IN (E:\Dmgr01\wstemp\anonymous*) DO RD /S /Q "%%X" d -180
ECHO Deleting backup...
ECHO OFF
timeout 3
del /s /q wstemp-backup.zip
@ECHO OFF


See More: Delete wildcard folders after 180 days

Reply ↓  Report •

✔ Best Answer
June 17, 2018 at 17:00:50
It's okay I figured it out, I just created another vbs script to do the dirty work, it's more clunky looking but eh it works.

BAT SCRIPT
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
@ECHO OFF
REM This calls upon the zip vbs script to zip up the entire contents of wstemp just in case.
CScript zip.vbs H:\File\wstemp H:\File\wstemp-backup.zip
ECHO Deleting anonymous files in wstemp...
ECHO OFF
REM This accesses the delolder vbs file to delete anything in wstemp with wilcard anonymous older than 6 months.
ECHO Deleting backup...
CScript delolder.vbs H:\File H:\File\wstemp
ECHO Deleting backup...
ECHO OFF
del /s /q wstemp-backup.zip
@ECHO OFF
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
ZIP VBS SCRIPT
'Get command-line arguments.
Set objArgs = WScript.Arguments
Set FS = CreateObject("Scripting.FileSystemObject")
InputFolder = FS.GetAbsolutePathName(objArgs(0))
ZipFile = FS.GetAbsolutePathName(objArgs(1))

'Create empty ZIP file.
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)

Set objShell = CreateObject("Shell.Application")

Set source = objShell.NameSpace(InputFolder).Items

objShell.NameSpace(ZipFile).CopyHere(source)

'Required to let the zip command execute
'If this script randomly fails or the zip file is not complete,
'just increase to more than 2 seconds
wScript.Sleep 4000
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
DELOLDER VBS SCRIPT THAT DELETES FOLDER/FILES OLDER THAN 6 MONTHS
Dim deletionDate
Dim fso
Dim oFile
Dim oFolder

Set fso = CreateObject("Scripting.FileSystemObject")

DeleteFiles "H:\File\wstemp",DateAdd("m", -6, Date)


Set oFolder = Nothing
Set oFile = Nothing
Set fso = Nothing

Function DeleteFiles(foldername,cutoffdate)
Set oFolder = fso.GetFolder(foldername)

For Each oFile in oFolder.Files
If ofile.DateCreated < cutoffDate Then
fso.DeleteFile oFile, True
End If
Next

For Each subFolder In oFolder.SubFolders
DeleteFiles subFolder.Path, cutoffdate
If (SubFolder.Files.Count = 0) and (SubFolder.SubFolders.Count=0) And (SubFolder.DateCreated < cutoffdate) Then
call SubFolder.Delete()
End If
Next

End Function



#1
June 16, 2018 at 21:13:29
For clarity, this particular string from the batch file needs a rework to delete any folder and it's contents older than 180 days with the wild card "anonymous" because there are multiple folders with that name with different end dates like "anonymous20180617"

FOR /D %%X IN (E:\Dmgr01\wstemp\anonymous*) DO RD /S /Q "%%X" d -180


Reply ↓  Report •

#2
June 16, 2018 at 22:17:05
Which do you prefer, file-timestamp (file-date) or file-name-date? I'm going with the timestamp:


@echo off & setlocal
if not exist ddif.vbs > ddif.vbs echo wscript.echo datediff(wscript.arguments(0), wscript.arguments(1), wscript.arguments(2))
for /D %%a in (*) do call :ddif %%a %%~ta %date%
goto :eof

:ddif
for /f %%b in ('cscript ddif.vbs d %2 %6') do if %%b geq 180 echo rd /q /s %1

message edited by nbrane


Reply ↓  Report •

#3
June 17, 2018 at 04:11:07
Hi nbrane, thanks for your reply, unfortunately I can't work out how to make it work with my script.


Here is my main script
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
@ECHO OFF
CScript zip.vbs E:\Dmgr01\wstemp E:\Dmgr01\wstemp-backup.zip
ECHO Deleting anonymous files in wstemp...
ECHO OFF
timeout 5
@echo off & setlocal
if not exist ddif.vbs > ddif.vbs echo wscript.echo datediff(wscript.arguments(0), wscript.arguments(1), wscript.arguments(2))
for /D %%a in (*) do call :ddif %%a %%~ta %date%
goto :eof
ECHO Deleting backup...
ECHO OFF
timeout 3
del /s /q wstemp-backup.zip
@ECHO OFF

Here is my vbs script for zipping the backup
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
'Get command-line arguments.
Set objArgs = WScript.Arguments
Set FS = CreateObject("Scripting.FileSystemObject")
InputFolder = FS.GetAbsolutePathName(objArgs(0))
ZipFile = FS.GetAbsolutePathName(objArgs(1))

'Create empty ZIP file.
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)

Set objShell = CreateObject("Shell.Application")

Set source = objShell.NameSpace(InputFolder).Items

objShell.NameSpace(ZipFile).CopyHere(source)

'Required to let the zip command execute
'If this script randomly fails or the zip file is not complete,
'just increase to more than 2 seconds
wScript.Sleep 2000

ddif script you created
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
wscript.echo datediff(wscript.arguments(0), wscript.arguments(1), wscript.arguments(2))
for /f %%b in ('cscript ddif.vbs d %2 %6') do if %%b geq 180 echo rd /q /s %1


Reply ↓  Report •

Related Solutions

#4
June 17, 2018 at 17:00:50
✔ Best Answer
It's okay I figured it out, I just created another vbs script to do the dirty work, it's more clunky looking but eh it works.

BAT SCRIPT
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
@ECHO OFF
REM This calls upon the zip vbs script to zip up the entire contents of wstemp just in case.
CScript zip.vbs H:\File\wstemp H:\File\wstemp-backup.zip
ECHO Deleting anonymous files in wstemp...
ECHO OFF
REM This accesses the delolder vbs file to delete anything in wstemp with wilcard anonymous older than 6 months.
ECHO Deleting backup...
CScript delolder.vbs H:\File H:\File\wstemp
ECHO Deleting backup...
ECHO OFF
del /s /q wstemp-backup.zip
@ECHO OFF
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
ZIP VBS SCRIPT
'Get command-line arguments.
Set objArgs = WScript.Arguments
Set FS = CreateObject("Scripting.FileSystemObject")
InputFolder = FS.GetAbsolutePathName(objArgs(0))
ZipFile = FS.GetAbsolutePathName(objArgs(1))

'Create empty ZIP file.
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)

Set objShell = CreateObject("Shell.Application")

Set source = objShell.NameSpace(InputFolder).Items

objShell.NameSpace(ZipFile).CopyHere(source)

'Required to let the zip command execute
'If this script randomly fails or the zip file is not complete,
'just increase to more than 2 seconds
wScript.Sleep 4000
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
DELOLDER VBS SCRIPT THAT DELETES FOLDER/FILES OLDER THAN 6 MONTHS
Dim deletionDate
Dim fso
Dim oFile
Dim oFolder

Set fso = CreateObject("Scripting.FileSystemObject")

DeleteFiles "H:\File\wstemp",DateAdd("m", -6, Date)


Set oFolder = Nothing
Set oFile = Nothing
Set fso = Nothing

Function DeleteFiles(foldername,cutoffdate)
Set oFolder = fso.GetFolder(foldername)

For Each oFile in oFolder.Files
If ofile.DateCreated < cutoffDate Then
fso.DeleteFile oFile, True
End If
Next

For Each subFolder In oFolder.SubFolders
DeleteFiles subFolder.Path, cutoffdate
If (SubFolder.Files.Count = 0) and (SubFolder.SubFolders.Count=0) And (SubFolder.DateCreated < cutoffdate) Then
call SubFolder.Delete()
End If
Next

End Function


Reply ↓  Report •

Ask Question