script to find all files in folder/subfolders

Discussion in 'Scripting' started by speedstic, Feb 22, 2008.

  1. speedstic

    speedstic Guest

    Hello All,

    I am trying to write a script to generate a log of all the files in a
    folder, including sub-folders but I can't get the subfolder part to work.
    Here is what i have so far...
    strComputer = "."
    logFile = "c:\test.txt"
    count = 0

    Const ForWriting = 2
    Const ForAppending = 8

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objCreateFile = objFSO.CreateTextFile(LogFile)
    Set objCreateFile = objFSO.OpenTextFile(LogFile, ForWriting)
    objCreateFile.WriteLine "Starting search at " & date() & ", " & time() & "..."

    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

    Set colFileList = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='c:\test\'} Where " _
    & "ResultClass = CIM_DataFile")

    For Each objFile In colFileList
    Set colFiles = objWMIService.ExecQuery _
    ("Select * from CIM_Datafile")
    file_name = "Filename: " & objFile.Name
    last_access = " Last Modified: " & objFile.LastModified
    file_size = "Size: " & objFile.FileSize
    count = count+1
    Set objEditFile = objFSO.OpenTextFile(LogFile, ForAppending)
    objEditFile.WriteLine count & ": " & file_name & ", " & last_access & ", "
    & file_size

    Wscript.Echo count

    It works for finding the files in a folder, but not the files inside
    Any help would be greatly appreciated!
    speedstic, Feb 22, 2008
    1. Advertisements

  2. The first question I would ask in this case is whether there is a
    good reason to use a VB Script to perform the task. If there
    isn't then this single, simple command line would do the job
    very nicely:

    dir "c:\program files" /s /ta /a-d | find "/"
    Pegasus \(MVP\), Feb 22, 2008
    1. Advertisements

  3. speedstic

    speedstic Guest

    what I would like the script to eventually do, is scan all files in a certain
    folder (including subfolders) and if the file has not been modified in 1
    year, then copy the file to a new location before deleting it, while writing
    to a txt file which files have been moved, their size, and the number of
    files moved.
    I haven't been able to move forward since i can't seem to get this part
    speedstic, Feb 22, 2008
  4. As I mentioned before: This type of task is easily handled by
    existing Command Line commands. Unless you have a strong
    reason to re-invent the wheel in VB Script, using a batch file
    is the usual solution. Try this one:

    @echo off
    set Age=365
    robocopy d:\temp d:\HoldingPen *.txt /S /move /MinAge:%Age% > c:\Move.txt

    You can download robocopy.exe from here:
    Pegasus \(MVP\), Feb 22, 2008
  5. speedstic

    urkec Guest

    If you want to use VBScript and WMI for this you need to use a recursive
    function. Here is a sample:

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")

    Call ListFiles ("C:\scripts")


    Sub ListFiles (strFolderName)

    Set ChildItemList = objWMIService.ExecQuery _
    ("Associators Of {Win32_Directory.Name=""" _
    & Replace (strFolderName, "\", "\\") _
    & """} Where Role = GroupComponent")

    For Each objItem in ChildItemList
    If objItem.Path_.Class = "Win32_Directory" Then
    Call ListFiles (objItem.Name)
    WScript.Echo "Folder" & objItem.Name
    ElseIf objItem.Path_.Class = "CIM_DataFile" Then
    WScript.Echo "File " & objItem.Name
    End If

    End Sub

    There is also a sample here, maybe you can use it:

    Is there a reason you want to use WMI for this? The command line option does
    look simpler.
    urkec, Feb 23, 2008
  6. speedstic

    J Ford Guest

    :/>DIR /TW /S
    J Ford, Aug 14, 2008
  7. Looks like spam dressed up as freeware.
    Pegasus \(MVP\), Jan 10, 2009
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.