Solved

VBScript for removing files from folder

Posted on 2009-07-14
8
468 Views
Last Modified: 2012-05-07
Hi All Experts

I would like to use Vbscript to remove some files from folder. These files are log files created automatically by application daily, I only want to keep 14 days log files and remove rest of them.

Anyone can tell me how to write a VBScript for this purpose? I am quite a new person to VB script.

Any help would be most appreciated.

Best Regards
0
Comment
Question by:mxgong
  • 5
  • 3
8 Comments
 
LVL 29

Expert Comment

by:mass2612
ID: 24856836
Hi,

I have a script to cleanup log files on a particular directory for one of our apps. Maybe this will work for you. test it out and let us know if you have any ???

Lines you need to edit are: -
 iDaysOld = 14
      sDirPath = "D:\" ' drive letter of where the directory to clean up is
      sDirName = "foldername" ' directory where the log files you need to cleanup are




' COMMENT: This script will search a speicified directory for folders older than XX days and delete them

'

'==========================================================================
 

'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

Const ForWriting = 2 ' creats a constant for writing to file

Const Forappending = 8 ' creates a constant for appending to file
 

' objOutputFile.WriteLine "Account name" & "," & "Alternate Recipient"

      iDaysOld = 14

      sDirPath = "D:\" ' drive letter of where the directory to clean up is

      sDirName = "foldername" ' directory where the log files you need to cleanup are

      Set fs = CreateObject("scripting.filesystemobject")

      sDirectoryPath = sDirPath & sDirName '"C:\temp"

      Set fo = fs.GetFolder(sDirectoryPath)

      Set folders = fo.SubFolders

	  OutputFile = sDirName & ".log"	' Output file with server details
 

	  Set objFSO = CreateObject("Scripting.FileSystemObject")

' Set objInputFile = objFSO.OpenTextFile(InputFile,1,FALSE) ' opens text file object

	  Set objOutputFile = objFSO.OpenTextFile(OutputFile, forWriting, True) ' opens output file for writing      

      strFolderList = "The following Folders are found under " & sDirectoryPath

	  strDelFolderList = "The following Folders are older than " & iDaysOld & " day will be deleted from " & sDirectoryPath

'Walk through each file in this folder collection. 

'If it is older than 3 weeks (10) days, then delete it.

		For Each folder In folders

		strFolderList = strFolderList & vbcrlf & folder.Name & vbtab & folder.DateLastModified

            If folder.DateLastModified < (Date() - iDaysOld) Then

                  strDelFolderList = strDelFolderList & vbcrlf & folder.Name & vbtab & folder.DateLastModified

                  folder.Delete(True)

            End If

      Next

'WScript.Echo strFolderList

'WScript.Echo strDelFolderList

objOutputFile.WriteLine Now & vbcrlf

objOutputFile.WriteLine "### Cleaning folder " & sDirectoryPath & " removing folders older than " & iDaysOld & " ### " & vbcrlf & vbcrlf

objOutputFile.WriteLine strFolderList & vbcrlf

objOutputFile.WriteLine strDelFolderList

Wscript.echo "Script completed see log file - " & OutputFile & vbcrlf

'Clean up

      Set oFSO = Nothing

      Set oFolder = Nothing

      Set oFileCollection = Nothing

      Set oFile = Nothing

Open in new window

0
 
LVL 29

Expert Comment

by:mass2612
ID: 24864980
Eeek. Sorry i think I posed the wrong script above will search for folders in a directory and delete the old ones. I'll try to post the files one soon. If you don't here from me today then request this call to be delete refund and re-post. Once again sorry about that.

0
 
LVL 29

Expert Comment

by:mass2612
ID: 24865101
I very quickly fixed this one up and tested it try it out it should suit your need. this will delete files that have not been modified for 14 days.

Set Directory = Fso.GetFolder("C:\temp\test")         ' place for directory details here
If DateDiff("D", Modified.DateLastModified, Now) > 14 Then Modified.Delete ' this line means 14 days

Dim Fso

Dim Directory

Dim Modified

Dim Files
 

Set Fso = CreateObject("Scripting.FileSystemObject")

Set Directory = Fso.GetFolder("C:\temp\test")         ' place for directory details here

Set Files = Directory.Files
 

For Each Modified in Files

If DateDiff("D", Modified.DateLastModified, Now) > 14 Then Modified.Delete

Next

Open in new window

0
 

Author Comment

by:mxgong
ID: 24865224
Thanks Mass2612....

I will try it today and get it back to you ASAP.

Cheers
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:mxgong
ID: 24865291
Hi Mass2612

Your script is working perfectly. But I may need a small change. (I am sorry I didn't mention my question clearly. )

In my log folder, you can see the attachment in the below. I only want to remove log filename "access.2009-xx-xx" which is over 14 days. I want to keep file:boot, process, server, and all server.log.2009-xx-xx which could be over 14 days.

Could you please help me to change the script?

I appreciate to your help and time.

Cheers
logfile.JPG
0
 
LVL 29

Accepted Solution

by:
mass2612 earned 500 total points
ID: 24865709
Try this. This will delete files older than 14 days with the string access.2009 in the filename. Well in theory make sure you test it somewhere safe first.

This is the line of note: -
if Instr(1,Modified.Name,"access.2009",1) then Modified.Delete

Dim Fso

Dim Directory

Dim Modified

Dim Files
 

Set Fso = CreateObject("Scripting.FileSystemObject")

Set Directory = Fso.GetFolder("C:\temp\test")

Set Files = Directory.Files
 

For Each Modified in Files

If DateDiff("D", Modified.DateLastModified, Now) > 14 Then 

	if Instr(1,Modified.Name,"access.2009",1) then Modified.Delete

	End if

Next

Open in new window

0
 
LVL 29

Expert Comment

by:mass2612
ID: 24874771
Morning - did this end up working ok for you?
0
 

Author Closing Comment

by:mxgong
ID: 31603602
Thanks for your help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help with VBScript adding Signature 3 33
Call VB Script silently for 2 batch files 7 42
If not exist vbs error 4 39
change the windows script file to BAT 10 29
In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

914 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now