Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

need help with vbscript

Posted on 2016-09-08
4
Medium Priority
?
30 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 1000 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 1000 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 59

Assisted Solution

by:Bill Prew
Bill Prew earned 1000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

783 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