Solved

need help with vbscript

Posted on 2016-09-08
4
19 Views
Last Modified: 2016-09-28
hi ,
I am just beginner to vbscript, I am trying to find files in my windows server based on last modified date and particular extension, please help me with this
0
Comment
Question by:hema latha
  • 3
4 Comments
 
LVL 17

Accepted Solution

by:
xtermie earned 250 total points (awarded by participants)
ID: 41791193
For the particular extension try:

Set objfso = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\Test"                                                                'Set your path here
Set objFolder = objfso.GetFolder(objStartFolder)

For Each objFile In objFolder.Files
          If objfso.GetExtensionName(objFile) = "jpeg" Then          'specify your extension here
                                       Wscript.Echo objFile.Name
          End If
Next

Open in new window


and if you would like an output for that, try:
Option Explicit 'force all variables to be declared

Const ForWriting = 2
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")

Dim objTS 'Text Stream Object
Set objTS = objFSO.OpenTextFile("C:\test\Output.txt", ForWriting, True)
Recurse objFSO.GetFolder("C:\test\")                        'set your folder here
objTS.Close()

Sub Recurse(objFolder)
    Dim objFile, objSubFolder

    For Each objFile In objFolder.Files
        If LCase(objFSO.GetExtensionName(objFile.Name)) = "jpeg" Then                  'set your extension here
            objTS.WriteLine(objfile.Path)
        End If
    Next

    For Each objSubFolder In objFolder.SubFolders
        Recurse objSubFolder
    Next
End Sub

Open in new window

0
 
LVL 17

Assisted Solution

by:xtermie
xtermie earned 250 total points (awarded by participants)
ID: 41791202
For the modified data read this other EE question (w answer)
https://www.experts-exchange.com/questions/23899745/VBScript-to-read-file-modified-date.html
0
 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 250 total points (awarded by participants)
ID: 41791429
This should be a decent starting point, see what you can do with this.  Specify the relevant info in the CONST lines at the top.  This will search recursively in all folders under the base folder.  And looks for files modified more than the number of days specified.

' Specify base folder to list files under, age in days of files, and extension to select
Const strDir = "C:\Temp"
Const intAge = 30
Const strExt = "txt"

' Create file system object
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

' Remove old files recursively
ListFiles objFSO.GetFolder(strDir)

Sub ListFiles(objFolder)
    On Error Resume Next

    ' List any files older than specified days from this folder
    For Each objFile In objFolder.Files
        If LCase(objFSO.GetExtensionName(objFile.Path)) = LCase(strExt) Then
            intFileAge = DateDiff("d", objFile.DateLastModified, Now)
            If intFileAge > intAge Then
                Wscript.Echo "File: """ & objFile.Path & """ Age: " & intFileAge & """."
            End If
        End If
    Next

    ' See if we were able to access this folder, if not don't recurse into it
    If Err.Number = 0 Then
        ' Remove all older files in any subfolders of this one
        For Each objSubFolder In objFolder.Subfolders
            ListFiles objSubFolder
        Next

    End If
End Sub

Open in new window

~bp
0
 
LVL 17

Expert Comment

by:xtermie
ID: 41819433
Valid commends and recommedations
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

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…
Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

760 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

20 Experts available now in Live!

Get 1:1 Help Now