Results 1 to 10 of 10

Thread: script to find all files in folder/subfolders

  1. #1
    speedstic Guest

    script to find all files in folder/subfolders

    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...
    --beginning--
    strComputer = "."
    logFile = "c:\test.txt"
    count = 0

    Const ForWriting = 2
    Const ForAppending = 8

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

    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
    objEditFile.Close
    Next

    Wscript.Echo count
    --end--

    It works for finding the files in a folder, but not the files inside
    subfolders.
    Any help would be greatly appreciated!

  2. #2
    Pegasus \(MVP\) Guest

    Re: script to find all files in folder/subfolders


    "speedstic" <speedstic@discussions.microsoft.com> wrote in message
    news:4C39387F-4448-46E7-BF69-24B61A2CDF26@microsoft.com...
    > 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...
    > --beginning--
    > strComputer = "."
    > logFile = "c:\test.txt"
    > count = 0
    >
    > Const ForWriting = 2
    > Const ForAppending = 8
    >
    > Set objFSO = CreateObject("Scripting.FileSystemObject")
    > Set objCreateFile = objFSO.CreateTextFile(LogFile)
    > objCreateFile.Close
    > Set objCreateFile = objFSO.OpenTextFile(LogFile, ForWriting)
    > objCreateFile.WriteLine "Starting search at " & date() & ", " & time() &
    > "..."
    > objCreateFile.Close
    >
    > 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
    > objEditFile.Close
    > Next
    >
    > Wscript.Echo count
    > --end--
    >
    > It works for finding the files in a folder, but not the files inside
    > subfolders.
    > Any help would be greatly appreciated!


    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 "/"



  3. #3
    speedstic Guest

    Re: script to find all files in folder/subfolders

    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
    working.

    "Pegasus (MVP)" wrote:

    >
    > "speedstic" <speedstic@discussions.microsoft.com> wrote in message
    > news:4C39387F-4448-46E7-BF69-24B61A2CDF26@microsoft.com...
    > > 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...
    > > --beginning--
    > > strComputer = "."
    > > logFile = "c:\test.txt"
    > > count = 0
    > >
    > > Const ForWriting = 2
    > > Const ForAppending = 8
    > >
    > > Set objFSO = CreateObject("Scripting.FileSystemObject")
    > > Set objCreateFile = objFSO.CreateTextFile(LogFile)
    > > objCreateFile.Close
    > > Set objCreateFile = objFSO.OpenTextFile(LogFile, ForWriting)
    > > objCreateFile.WriteLine "Starting search at " & date() & ", " & time() &
    > > "..."
    > > objCreateFile.Close
    > >
    > > 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
    > > objEditFile.Close
    > > Next
    > >
    > > Wscript.Echo count
    > > --end--
    > >
    > > It works for finding the files in a folder, but not the files inside
    > > subfolders.
    > > Any help would be greatly appreciated!

    >
    > 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 "/"
    >
    >
    >


  4. #4
    Pegasus \(MVP\) Guest

    Re: script to find all files in folder/subfolders

    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:
    download:
    http://www.microsoft.com/downloads/d...displaylang=en


    "speedstic" <speedstic@discussions.microsoft.com> wrote in message
    news:792AFB69-6EDE-4DA6-BBBE-BF85DFC245D1@microsoft.com...
    > 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
    > working.
    >
    > "Pegasus (MVP)" wrote:
    >
    >>
    >> "speedstic" <speedstic@discussions.microsoft.com> wrote in message
    >> news:4C39387F-4448-46E7-BF69-24B61A2CDF26@microsoft.com...
    >> > 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...
    >> > --beginning--
    >> > strComputer = "."
    >> > logFile = "c:\test.txt"
    >> > count = 0
    >> >
    >> > Const ForWriting = 2
    >> > Const ForAppending = 8
    >> >
    >> > Set objFSO = CreateObject("Scripting.FileSystemObject")
    >> > Set objCreateFile = objFSO.CreateTextFile(LogFile)
    >> > objCreateFile.Close
    >> > Set objCreateFile = objFSO.OpenTextFile(LogFile, ForWriting)
    >> > objCreateFile.WriteLine "Starting search at " & date() & ", " & time()
    >> > &
    >> > "..."
    >> > objCreateFile.Close
    >> >
    >> > 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
    >> > objEditFile.Close
    >> > Next
    >> >
    >> > Wscript.Echo count
    >> > --end--
    >> >
    >> > It works for finding the files in a folder, but not the files inside
    >> > subfolders.
    >> > Any help would be greatly appreciated!

    >>
    >> 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 "/"
    >>
    >>
    >>




  5. #5
    urkec Guest

    Re: script to find all files in folder/subfolders

    "speedstic" wrote:

    > 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
    > working.


    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
    Next

    End Sub

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

    http://www.microsoft.com/technet/scr...5/hey0405.mspx

    Is there a reason you want to use WMI for this? The command line option does
    look simpler.

    --
    urkec

  6. #6
    Join Date
    Aug 2008
    Posts
    2
    Hey urkec,
    I need to get the last modified date from tons of folder and files, sub-folders too... What syntax do you recommend?

  7. #7
    Join Date
    Aug 2008
    Posts
    2

    Help

    Hey urkec,
    I need to get the last modified date from tons of folder and files, sub-folders too... What syntax do you recommend?

    Quote Originally Posted by urkec View Post
    "speedstic" wrote:

    > 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
    > working.


    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
    Next

    End Sub

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

    http://www.microsoft.com/technet/scr...5/hey0405.mspx

    Is there a reason you want to use WMI for this? The command line option does
    look simpler.

    --
    urkec

  8. #8
    J Ford Guest

    Re: script to find all files in folder/subfolders


    :/>DIR /TW /S

    "jimbiddle" wrote:

    >
    > Hey urkec,
    > I need to get the last modified date from tons of folder and files,
    > sub-folders too... What syntax do you recommend?
    >
    >
    > --
    > jimbiddle
    > ------------------------------------------------------------------------
    > jimbiddle's Profile: http://forums.techarena.in/members/jimbiddle.htm
    > View this thread: http://forums.techarena.in/server-scripting/919270.htm
    >
    > http://forums.techarena.in
    >
    >


  9. #9
    Join Date
    Jan 2009
    Posts
    1

    Re: script to find all files in folder/subfolders==>Just one command will do this

    The following one command in biterscripting will list all files within a folder and all subfolders. Let's assume the name of the folder is C:/folder.

    find -r -n "*" "C:/folder" ($ftype=="f")

    The above command will do it. -r means recursive (look in subfolders), -n (show name), "C:/folder" is the top folder where search will begin, ($ftype=="f") means show flat files (don't show subfolders). You can add other conditions.

    For example, ($ftype=="f") AND ($fsize > 1000000) means show files whose size is greater that 1 million bytes.

    Check out the help page for the find command by downloading biterscripting from http://www.biterscripting.com. Also check out one of their sample scripts for searching within files at http://www.biterscripting.com/Download/SS_FindStr.txt .

    Jenni

  10. #10
    Pegasus \(MVP\) Guest

    Spam

    Looks like spam dressed up as freeware.



Similar Threads

  1. Replies: 7
    Last Post: 24-09-2011, 11:04 PM
  2. How to totally delete defunct WINDOWS folder/subfolders?
    By IT Lite in forum Windows XP Support
    Replies: 7
    Last Post: 06-02-2011, 04:52 AM
  3. Home folder creation via script
    By CHRITOPHER in forum Active Directory
    Replies: 3
    Last Post: 15-12-2008, 10:39 PM
  4. Search Companion text search folder doesn't find all files
    By Hrishia in forum Windows XP Support
    Replies: 2
    Last Post: 27-11-2008, 10:16 PM
  5. Problem After Creating Home Folder with vbs script
    By Susan Bradley in forum Active Directory
    Replies: 3
    Last Post: 18-08-2008, 09:33 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,941,482.19956 seconds with 17 queries