Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8158181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:31:38+00:00 2026-06-06T17:31:38+00:00

In VBScript, how do I manage Scripting.FileSystemObjects like objFSO and objFolder for multiple folders/files

  • 0

In VBScript, how do I manage “Scripting.FileSystemObjects” like objFSO and objFolder for multiple folders/files ?

In the “Main” code section, I declare an instance (global) of “Scripting.FileSystemObject”

    Set objFSO = CreateObject("Scripting.FileSystemObject")

Then, I perform some operations, like:

    If objFSO.FileExists(strOutputFilename) Then
        WScript.Echo "Deleting File: " & strOutputFilename
        objFSO.DeleteFile strOutputFilename
    End If

Then, in a loop, I get a folder, and pass it to a function:

    For gintLoop = 0 to (ubound(arraySearchPath))
        wscript.echo "Processing folder:" & arraySearchPath(gintLoop)
        Set objFolderX = objFSO.GetFolder(arraySearchPath(gintLoop))
        Call DoWork (objFolderX, arrayParam1, arrayParam2)
    Next

So far everything is clear…

Now, within the function, I do things like:
a) collect filenames from objFolder

    Set lobjFolder = objFSO.GetFolder(objFolderX.Path)  
    Set lcolFiles = lobjFolder.Files

b) check for existance of files in other (unrelated) paths

c) get the size of various files:

    lcurInputFileSize = CCur(lobjFile.Size)

d) delete various files

e) open files for reading

    For Each lobjFile in lcolFiles
        lstrTargetFile = lobjFolder.Path & "\" & lobjFile.Name
        Set lobjInputFile = objFSO.OpenTextFile(lstrTargetFile, ForReading)
    ...

f) open files for writing

    Set lobjOutputFile = objFSO.OpenTextFile(strOutputFilename, ForAppending, True)

g) call other subs/functions passing various object

h) recursively call the (same) function to process other folders

    For Each lobjSubfolderY in objFolderX.SubFolders
        Call DoWork (lobjSubfolderY, arrayParam1, arrayParam2)
    Next

My concern is that I need to make sure the various uses of FileSystemObjects like folder paths, open files, etc, are not “Stepped-on” by later uses of FileSystemObjects.

Question 1:
Do I need (or is it advised) to have a seperate instance of “Scripting.FileSystemObject” (objFSO) for “Main” and each (or some) sub/function ?

Question 2:
Similarly, how do I manage the various other objects to avoid loosing data ?

Kevin

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:31:39+00:00Added an answer on June 6, 2026 at 5:31 pm

    Q1: No, you do not need multiple instances of Scripting.FileSystemObject.

    The methods on the object are all static.

    In fact, the documentation for the Scripting Runtime Reference indicates that the FSO is a singleton, although it does not use the word:

    You can create only one instance of the FileSystemObject object, regardless of how many times you try to create another.

    from: http://msdn.microsoft.com/en-us/library/2z9ffy99(v=vs.84).aspx

    In my experience, calling WScript.CreateObject("Scripting.FileSystemObject") multiple times does not result in an error. Likely the return value on subsequent calls is just a copy of the originally created FSO.

    As for your Question 2, I don’t get it. I think you are referring to objects that are returned by FSO, objects of type Folder, File, TextStream and so on.

    Treat these like any other stateful object. You can have multiple instances, and you need to pass them as stack-based arguments if you want to do recursion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a VBScript code snippet which converts my xls and xlsx files into
In asp classic and vbscript, you can declare a Const with a hexidecial value,
I'm trying to use FSO = Server.CreateObject(Scripting.FileSystemObject) to copy/save files utilising a ASP utility
Here's my asp code: <%@ Language = Python%> <% def main(): a = Request(btnSubmit)
We inherited a bunch of legacy (10+ years old) VBScript files which use to
in VBScript, I've got a function like: Function InsertPerson(FirstName, LastName, Street1, Street2, City, State,
In VBScript I have code: someLongValue = CLng(DateSerial(2011, 11, 1)) and in that case
Consider the following VBScript which, when run, lists all the files in the current
VBScript (.vbs) files can be run using either cscript.exe or wscript.exe. What's the difference
I have a managed assembly that gets called via COM Interop. Like a VBScript

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.