VBScript GetFolder Method. Can anyone explain?

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?
LVL 1
donniedarko801Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mvidasCommented:
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
donniedarko801Author Commented:
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
mvidasCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

donniedarko801Author Commented:
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
mvidasCommented:
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
mvidasCommented:
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
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.