C++/VB - an easy script to delete folders on a network share that are older than 14 days
Asked By cnknsd
08-Nov-07 08:09 PM

I have read extensively regarding using VBSCRIPT to automate the
deletion of files and folders locally and on network shares.
Everything is so confusing, and I do not understand why...
I got saddled with directory maintenance on of a share that is used to
transfer data for upgrades. I need to make sure that no directories/
folders reside on the share that are older than 14 days. So I want to
automate this task...Can someone provide me with a script that will
delete all folders/directories on a share wether they are empty or not
that are older than 14 days?
I have found loads of examples for deleteing files that work great but
I cannot get the folders to delete.
Please Help!
Here is an example of a script that is simple for files but I cannot
figure out how to change it to delete folders instead of files. any
help?
Option Explicit
on error resume next
Dim oFSO
Dim sDirectoryPath
Dim oFolder
Dim oFileCollection
Dim oFile
Dim iDaysOld
'Customize values here to fit your needs
iDaysOld = 14
Set oFSO = CreateObject("Scripting.FileSystemObject")
sDirectoryPath = "\\MyServer\MyFolder"
set oFolder = oFSO.GetFolder(sDirectoryPath)
set oFileCollection = oFolder.Files
'Walk through each file in this folder collection.
'If it is older than 2 weeks (14) days, then delete it.
For each oFile in oFileCollection
If oFile.DateLastModified < (Date() - iDaysOld) Then
oFile.Delete(True)
End If
Next
'Clean up
Set oFSO = Nothing
Set oFolder = Nothing
Set oFileCollection = Nothing
Set oFile = Nothing
OFSO
(1)
SDirectoryPath
(1)
CreateObject
(1)
IDaysOld
(1)
Folderlist
(1)
Noofdays
(1)
OFolder
(1)
Icheck
(1)
replied...

I found this script one day when i was looking to do the same thing. I made
a few changes from the original such as coding in the location of the folder
that held the subfolders, as well as autmoating it to detect folders/files
longer than a specific number of days. Now, I used this in conjunction with
a backup script I made, but the general functionality is the same. Just
make sure to change the location of the folders, as well as the number of
days it is compairing for. I have it set to 120 right now. it will also
make/append a log file to tell you which folders it deleted. Might take
some more changes to get it tweaked for your use, but here it is..
'
'delete folders specified in a number of days, also specified in the script.
'
On error resume next
Dim Directory
Dim Noofdays
Dim FSO
Dim FSO2
Dim LogFile
Dim Folderlist
Dim folders
Directory ="\\server\share\folder\subfolder\"
Noofdays=cint(120)
LogFile="\\server\share\folder\subfolder\deleted.txt"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set FSO2 = CreateObject("Scripting.FileSystemObject")
Set oFSO = CreateObject("Scripting.FilesyStemObject")
'
If oFSO.FileExists(Logfile) Then
Const ForAppending = 8
Set ofile = oFSO.OpenTextFile(LogFile, ForAppending, True)
Else
Set oFile = oFSO.CreateTextFile(logfile, true)
End If
ofile.writeline "Delete Folders older than 120 days Started --> " & now()
Set Folderlist = FSO.GetFolder(Directory)
Set folders = Folderlist.SubFolders
For Each d In Folders
' msgbox d.name
' msgbox d.size
' msgbox d.dateCreated
' msgbox d.dateLastModified
' msgbox d.dateLastAccessed
tempdirectory = Directory & d.name
If datediff("d",d.dateCreated,now()) > Noofdays Then
FSO2.DeleteFolder(tempdirectory )
ofile.writeline "Deleting Folder...." & tempdirectory
if err.number <>0 then
ofile.writeline cstr(now()) & " " & Err.description
err.clear
end if
End If
Next
ofile.writeline "Delete Folders older than 120 days Completed --> " &now()
ofile.writeline "--------------------------------------------"
ofile.close
cnknsd replied...
That is excelent..and in fact I can understand that script. Thank you
for posting that for me...You Rock!
cnknsd replied...
Ok, the script file works locally but it will not delete remotely, I
get permission denied error when I try to delete the folders on the
server share.
Does anyne have any suggestions? to resolve this? I do have full
permissions to the share.
replied...
I've not had a problem with the script running in our environment, be sure
to check both your ntfs and your share permissions on the share that houses
the files you're going to delete. Also, check the names of the files - you
might have to make a modification if they include spaces in the name.
fwitw.
Doug
cnknsd replied...
I have had them checked and they are the way that they should, when I
check what is actually running I find that the NT AUTHAURITY\Local
Service is the operational entity. Does anyone know how to get the
script to run under my credentials?
replied...
your credentials shouldn't be an issue if you have full control rights to
the folder (both ntfs and share permissions). Another option would be that
a program is running and currently using those folders (vscan, backup,
etc???). Or, try this... make a set of folders on your drive, modify the
script to point to those folders and maybe delete in 1 day instead of 40 or
whatever. Then run the script against that set of folders to see if you are
still having the issue. Chances are it will work just fine, in which case
there's either something using those folders and therefore stopping you from
deleting them, or you don't have the proper permissions. Do you have any
'special' permissions to those folders? Do they inherit the permissions
from the parent folder?
Gerard van Heck replied to
Hello the script works perfect, is it possible to make it so that different directory's can be checked ? I have about 15 directory's with differend archives were i want to use this script for. Already thanks, Gerard
login script, and I need to make it conditional. Currently I have the following: Set oFSO = CreateObject("Scripting.FileSystemObject") If oFSO.FileExists("c: \ Folder \ File.exe") Then oFSO.GetFile("C: \ Folder \ File.exe").Attributes = 0 'In case it is read-only oFSO.CopyFile \ server \ share \ file.exe, "c: \ Folder \ ", True End If I need to do the is not important, but the variable is. THanks, Michael VBScript Discussions Scripting.FileSystemObject (1) WScript.CreateObject (1) OFSO.FileExists (1) WScript.Shell (1) OFSO.CopyFile (1) OFSO.GetFile (1) ObjWshShell.Environment (1) OFSO (1) Here is how you can
target file back to the folder of links. Here is my attempt. Any suggestions? Set oFSO = CreateObject("Scripting.FileSystemObject") Set oFolderOfLinks = oFSO.GetFolder("C: \ Documents and Settings \ Dan \ Desktop \ Links") Set colFiles = oFolderOfLinks.Files For Each oFile strcomp(right(oFile.name, 4), ".lnk", vbTextCompare) = 0 Then Set oLink = oFile.TargetPath Set oLinkedFile = oFSO.GetFile(oLink) oLink.Copy oFolderOfLinks End If Next VBScript Discussions OFSO (1) OFolderOfLinks (1) OFile (1) StrFolder (1) OLink (1) ColFiles (1) CreateObject (1) FileSystemObject (1) strFolder = "C: \ xy \ " Set oFSO = CreateObject("Scripting.FileSystemObject") Set oFolderOfLinks = oFSO.GetFolder(strFolder) Set WshShell = CreateObject ("WScript.Shell") Set colFiles = oFolderOfLinks
I am getting the following error on the 4th line of the code: 'Object required: 'oFSO" I have tried different things and it does not work. Thanks, SA Sample code - -- -- -- -- -- -- -- -- -- dim folderP, oFile, oFSO, fab folderP = Application.PrimaryDirectory 'gives path of the project folder C: \ temp \ Projects \ 21038Ax1aDx" set oFile = oFSO.GetFile(folderP) fab = mid(oFile.Name, 2, 4) VBScript Discussions Excel (1) Word (1) VBA (1) VBSscript (1) OFSO (1) PrimaryDirectory (1) CurrentDirectory (1) CreateObject (1) You forgot to create the oFSO object: Set oFSO = CreateObject("Scripting.FileSystemObject") You will also need to create an Application object. Hi, i am missing
Can anybody help me? Here is the scipt Const FOR_READING = 1 Const FOR_WRITING = 2 Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject") Dim sLogFile : sLogFile = "replicatie1.txt" Dim oLogFile : Set oLogFile = oFSO.OpenTextFile (sLogFile, FOR_WRITING, true) filePath = "replicatie.txt" srchFor = "Failed" Set fs = CreateObject("Scripting.FileSystemObject") Set fl = fs.OpenTextFile(filePath, 1) Dim rslt1 intCount = 0 Do Until fl vbNewLine Loop intCount = intCount + 1 wscript.Echo rslt1 & " " & CStr(intcount) VBScript Discussions Scripting.FileSystemObject (1) OFSO.OpenTextFile (1) OFile.OpenAsTextStream (1) OMTS.Count (1) VbTextCompare (1) OMTS (1) VbNewLine (1) OFSO (1) You're searching for the word "Failed" in a test line that contains only in different methods to solve your problem, you may try this: Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" ) Dim sFSpec : sFSpec = ". \ access.log" Dim oFile : Set oFile = oFS.GetFile( sFSpec ) Dim