Solved

need help with vbscript

Posted on 2016-09-08
4
23 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 52

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

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

Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy cod…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 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