Check files DateLastModified only in First Level SubFolders

Posted on 2009-07-16
Last Modified: 2012-05-07
I am fairly new to vbscript, but learning every day. I need a vbscript that will check only in the first level subfolder from the root folder. Then it checks the DateLastModified on all files in that folder. If a file meets criteria, then copy (or preferably move) that folder (and its contents) to another folder. I have the following code, but it checks All Subfolders.
Set objFSO = CreateObject("Scripting.FileSystemObject")

objRootFolder = "W:\CasesT"

Set objFolder = objFSO.GetFolder(objRootFolder)

ShowSubfolders objFSO.GetFolder(objRootFolder)

Sub ShowSubFolders(Folder)

    For Each Subfolder in Folder.SubFolders

        Set objFolder = objFSO.GetFolder(folder.Path)

        Set colFiles = objFolder.Files

  	a = 0

    For Each objFile in colFiles

	dtmDate = objFile.DateLastModified

	fileDate = DateDiff("m", dtmDate, Date)

	If fileDate < 1 Then

	a = 1

	Exit For

	End If


	If a = 1 Then

	objFSO.CopyFolder objFolder.Path , "W:\Cases2\"

	End If

        ShowSubFolders Subfolder


End Sub

Open in new window

Question by:usdcaz
LVL 38

Accepted Solution

Shift-3 earned 500 total points
ID: 24873059
This should do it.

strSource = "W:\CasesT"

strDest = "W:\Cases2"

intMonths = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFolder = objFSO.GetFolder(strSource)

For Each objSubfolder in objFolder.SubFolders

    For Each objFile in objSubfolder.Files

        dtmDate = objFile.DateLastModified


        If DateDiff("m", dtmDate, Date) < intMonths Then

            objFSO.CopyFolder objSubfolder.Path, strDest & "\"

            Exit For

        End If



Open in new window


Author Closing Comment

ID: 31604365
Great, worked perfectly! Thanks you for your help!

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Welcome to part one of a multi-part tutorial series, VBScript for Windows System Administrators.  The goal of this series is to teach non-programmers how to write useful VBS code to automate their environment, and perform tasks faster, and in a more…
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…
Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

12 Experts available now in Live!

Get 1:1 Help Now