[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VBScript GetFolder Method. Can anyone explain?

Posted on 2006-04-06
10
Medium Priority
?
9,340 Views
Last Modified: 2013-12-25
Hello experts,

I found this piece of code here in the forum:

Sub ShowFolderList(folderspec)
    Dim fs, f, f1, fc, s
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(folderspec)
    Set fc = f.SubFolders
    For Each f1 in fc
        s = s & f1.name
        s = s &  vbCrLf
    Next
    MsgBox s
End Sub

Can anyone explain how to get this code to return whatever subfolders are available from within the location it is runing from? Also, what is folderspec?
0
Comment
Question by:donniedarko801
  • 4
  • 2
7 Comments
 
LVL 35

Expert Comment

by:mvidas
ID: 16396141
Hi donniedarko,

The .GetFolder method is actually a method of the FileSystemObject (part of the scripting library) rather than a VBScript method, but I understand what you're asking.  It returns a Folder object (also part of the scripting library).  folderspec can be either an absolute path or a relative path, though using an absolute path helps you understand exactly what folder it is pulling.  You can get information on this method (and other parts of the filesystemobject) at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/867057a0-50cf-4613-84be-28bf908b23a7.asp
"folderspec" is just the argument name that the MS programmers decided to use, short for "specific folder".  I'd be happy to give you an example of how it is used, but the above link does, and I'm guessing you have one anyways where you got the above from.  Feel free to ask more questions about it though!

Matt
0
 
LVL 1

Author Comment

by:donniedarko801
ID: 16396299
Thanks Matt. I understand the absolute path but what would the code look like if it was a ralative path I had in mind? I guess what I mean is, is there a way for the code to "know" what folder it finds itself in and upon executed display all subfolders within that folder? Just trying to make it as dynamic as possible. Thanks for any ideas.
0
 
LVL 35

Accepted Solution

by:
mvidas earned 2000 total points
ID: 16396358
I understand.. also it looks like your question changed from when I first looked at it, you must have edited it between me loading it and posted to it.

With that sub above, you could easily use this to see the subdirectories of the active folder:
 ShowFolderList "."

Not quite what you were looking for, I don't think.  However, If your directory structure that you'll run this from always has an "archive" folder, and you want to see in there, you could use:
 ShowFolderList "archive"

And it will return the contents of the subfolder 'archive'

As "." means 'current directory' in dos (similarly, ".." means 'parent folder')


If you wanted to get a listing of the subdirectories of the folder that the .vbs file is located in, you could use:
 VBSFoldr =  Left(WScript.ScriptFullName, Len(WScript.ScriptFullName) - Len(WScript.ScriptName))
 ShowFolderList VBSFoldr


Another way of getting the current folder (since "." works in FSO but you'd have to use fs.GetFolder(".").Path ):
 Set WshShell = CreateObject("WScript.Shell")
 ActiveFoldr = WshShell.CurrentDirectory
 
The 'ActiveFoldr' variable now contains the current directory path.
Hope that explains it (and probably a lot more), but please ask if not!
Matt
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:donniedarko801
ID: 16396623
Alright Matt. Here's what I got. This code looks up SubFolders inside of folders it currently finds itself in which is 99% of what I wanted! You already won my 500 points. I was wondeing one thing... What if the subfolders it finds also contain folders in them?   Recursively is what I have in mind. Sorry to ask so much, hehe.

Sub Main()
    Dim fs, f, f1, fc, s

    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(".")
   Set fc = f.SubFolders
    For Each f1 in fc
        s = s & f1.name
        s = s &  vbCrLf
    Next
    MsgBox s
End Sub
0
 
LVL 35

Expert Comment

by:mvidas
ID: 16396669
Really not a problem! What I do to do this recursively (I only have a moment so I'll give you some untested code, hopefully you can see what I'm doing. I'll be happy to help you later on tonight if you need it, but I have to run in a minute) is to create a function to return what I need.  I would do something like:

dim x, f, fs
set fs=createobject('scripting.filesystemobject")
set f = fs.getfolder(".")
x=retrievesubfolders(f)
msgbox x

function RetrieveSubfolders(fld)
 dim sf, s
 s=fld.path & ": " & vbcrlf
 for each sf in fld
  s = s & sf.path & vbcrlf
 next

 for each sf in fld
  s=s &  retrievesubfolders(sf) & vbcrlf
 next
 retrievesubfolders=s
 set sf=nothing
 set s=nothing
end function

That way it calls itself for each subfolder, and will keep doing that as long as subfolders exist.  the resulting msgbox SHOULD contain a list of all subfolders.  I can't guarantee it works as-is, but hopefully you get the jist.  I'll be back on later if you need more, don't hesitate to ask if you think of anything else.

Matt
0
 
LVL 35

Expert Comment

by:mvidas
ID: 16396674
after a quick review, you should delete this line:
 s=fld.path & ": " & vbcrlf

It was part of my first idea, which I changed halfway through
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16396849
Here is a recursive search that writes the names of all files older than 90 days to a file.

Just another way to look at basically the same code...

Dim fso, OlderThanDate, logFileName, logFile, startFolder

OlderThanDate = DateAdd("d", -90, Date)
startFolder = "C:\Documents and Settings\Michael\My Documents\1 VB Code\2 VBScript Testing"
logFileName = "c:\" & CStr(Replace(FormatDateTime(Now, vbShortDate), "/", "-", 1, -1, 1)) & ".txt"

Set fso = CreateObject("Scripting.FileSystemObject")
Set logFile = fso.CreateTextFile(logFileName, True)

logFile.WriteLine "Recursive Search Starting In: " & startFolder
logFile.WriteLine "For Files Older Than: " & OlderThanDate
logFile.WriteLine "------------------------------"

SearchForFiles startFolder, OlderThanDate, logFile

Function SearchForFiles(folderName, BeforeDate, fileObject)
    Dim folder, file, fileCollection, folderCollection, subFolder

    Set folder = fso.GetFolder(folderName)
    Set fileCollection = folder.Files
    For Each file In fileCollection
        If file.DateLastModified < BeforeDate Then
            fileObject.WriteLine file.Path
            fileObject.WriteLine file.DateLastModified
            fileObject.WriteLine "------------------------------"
        End If
    Next

    Set folderCollection = folder.SubFolders
    For Each subFolder In folderCollection
       SearchForFiles subFolder.Path, BeforeDate, logFile
    Next
End Function
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

830 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