Solved

need help with vbscript

Posted on 2016-09-08
4
24 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 18

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 18

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 53

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 18

Expert Comment

by:xtermie
ID: 41819433
Valid commends and recommedations
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 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