Contact Info

Crumbtrail

ActiveXperts.com » Administration » VBScript Collection » Storage » Folders

Folder Management with VBScript

Bind to a Folder Using the Browse for Folder Dialog Box
Create a Folder Using the Shell Object
Copy a Folder Using the Shell Object
Copy a Folder Using WMI
Create a Network Share
Compress a Folder
Copy a Folder
Create a Folder
Delete a Folder
Delete a Folder on the Local Computer
Delete a Network Share
Delete a Published Folder
Enumerate Subfolders Using Recursion
List All the Folders on a Computer
List Folder Attributes
List Folders Based on Creation Date
List the Subfolders of a Folder
List Folder Properties
List Folder Properties
List Network Shares
List the Subfolders of a Folder on the Local Computer
List Shared Folders Published in Active Directory
List Shell Object Verbs
List a Specific Set of Folders
Map All Network Shares to Local Folders
Modify Folder Attributes
Move a Folder Using the Shell Object
Move a Folder Using WMI
Modify a Network Share
Map a Network Share to a Local Folder
Move a Folder
Publish a Shared Folder in Active Directory
Rename a Folder
Rename a Folder on the Local Computer
Search for Folders by Date
Search for Folders Using Wildcards
Search for Specific Published Folders in Active Directory
Uncompress a Folder
Verify that a Folder Exists


You can use any of the VBScript programs below in ActiveXperts Network Monitor. Click here for an explanation about how to include scripts in ActiveXperts Network Monitor.



Bind to a Folder Using the Browse for Folder Dialog Box


Uses the Shell object to display the Browse for Folder dialog box. After a folder has been selected, the script reports whether or not the folder is read-only.
Const WINDOW_HANDLE = 0
Const NO_OPTIONS = 0

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder _
    (WINDOW_HANDLE, "Select a folder:", NO_OPTIONS, "C:\Scripts")       
Set objFolderItem = objFolder.Self

objPath = objFolderItem.Path
objPath = Replace(objPath, "\", "\\")

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

Set colFiles = objWMIService.ExecQuery _
    ("Select * from Win32_Directory where name = '" & objPath & "'")

For Each objFile in colFiles
    Wscript.Echo "Readable: " & objFile.Readable
Next
	

Create a Folder Using the Shell Object


Uses the Shell object to create a new folder named C:\Archive.
ParentFolder = "C:\" 

set objShell = CreateObject("Shell.Application")
set objFolder = objShell.NameSpace(ParentFolder) 
objFolder.NewFolder "Archive"
	

Copy a Folder Using the Shell Object


Uses the Shell object to copy the folder C:\Scripts to D:\Archives. Displays the Copying Files progress dialog as the folder is being copied.
Const FOF_CREATEPROGRESSDLG = &H0&

ParentFolder = "D:\Archive" 
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.NameSpace(ParentFolder) 
objFolder.CopyHere "C:\Scripts", FOF_CREATEPROGRESSDLG
	

Copy a Folder Using WMI


Uses WMI to copy the folder C:\Scripts to D:\Archive.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colFolders = objWMIService.ExecQuery( _
    "Select * from Win32_Directory where Name = 'c:\\Scripts'")

For Each objFolder in colFolders
    errResults  = objFolder.Copy("D:\Archive")
Next
	

Create a Network Share


Creates a shared folder named FinanceShare, setting the maximum number of simultaneous connections to 25 and adding a share description.
Const FILE_SHARE = 0
Const MAXIMUM_CONNECTIONS = 25

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

Set objNewShare = objWMIService.Get("Win32_Share")

errReturn = objNewShare.Create _
    ("C:\Finance", "FinanceShare", FILE_SHARE, _
        MAXIMUM_CONNECTIONS, "Public share for the Finance group.")
	

Compress a Folder


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

Set colFolders = objWMIService.ExecQuery _
    ("Select * from Win32_Directory where name = 'c:\\Scripts'")

For Each objFolder in colFolders
    errResults = objFolder.Compress
Next
	

Copy a Folder


Demonstration script that uses the FileSystemObject to copy a folder to a new location. Script must be run on the local computer.
Const OverWriteFiles = TRUE

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFolder "C:\Scripts" , "C:\FSO" , OverWriteFiles
	

Create a Folder


Demonstration script that uses the FileSystemObject to create a folder. Script must be run on the local computer.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.CreateFolder("C:\FSO")
	

Delete a Folder


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

Set colFolders = objWMIService.ExecQuery _
    ("Select * from Win32_Directory where Name = 'c:\\Scripts'")

For Each objFolder in colFolders
    errResults = objFolder.Delete
Next
	

Delete a Folder on the Local Computer


Demonstration script that uses the FileSystemObject to delete a folder. Script must be run on the local computer.
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFolder("C:\FSO")
	

Delete a Network Share


Stops sharing a shared folder named FinanceShare. This does not delete the folder from the local hard drive, but simply stops making it available over the network.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colShares = objWMIService.ExecQuery _
    ("Select * from Win32_Share Where Name = 'FinanceShare'")

For Each objShare in colShares
    objShare.Delete
Next
	

Delete a Published Folder


Deletes a published folder named FinanceShare from Active Directory. This does not delete the share itself, it simply removes it from Active Directory.
Set objContainer = GetObject("LDAP://CN=FinanceShare, " _
    & "OU=Finance, DC=fabrikam, DC=com")

objContainer.DeleteObject (0)
	

Enumerate Subfolders Using Recursion


Demonstration script that uses the FileSystemObject to recursively list all the subfolders (and their subfolders) within a folder. Script must be run on the local computer.
Set FSO = CreateObject("Scripting.FileSystemObject")
ShowSubfolders FSO.GetFolder("C:\Scripts")

Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
        Wscript.Echo Subfolder.Path
        ShowSubFolders Subfolder
    Next
End Sub
	

List All the Folders on a Computer


Returns a list of all the folders on a computer. This can take 15 minutes or more to complete, depending on the number of folders on the computer.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colFolders = objWMIService.ExecQuery("Select * from Win32_Directory")

For Each objFolder in colFolders
    Wscript.Echo objFolder.Name
Next
	

List Folder Attributes


Demonstration script that uses the FileSystemObject to enumerate the attributes of a folder. Script must be run on the local computer.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\FSO")

If objFolder.Attributes AND 2 Then
    Wscript.Echo "Hidden folder."
End If    
If objFolder.Attributes AND 4 Then
    Wscript.Echo "System folder."
End If    
If objFolder.Attributes AND 16 Then
    Wscript.Echo "Folder."
End If  
If objFolder.Attributes AND 32 Then
    Wscript.Echo "Archive bit set."
End If
If objFolder.Attributes AND 2048 Then
    Wscript.Echo "Compressed folder."
End If
	

List Folders Based on Creation Date


Lists all the folders on a computer that were created after 3/1/2002.
Const LOCAL_TIME = TRUE

Set dtmTargetDate = CreateObject("WbemScripting.SWbemDateTime")
Set dtmConvertedDate = CreateObject("WbemScripting.SWbemDateTime")
dtmTargetDate.SetVarDate "3/1/2004", LOCAL_TIME

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

Set colFolders = objWMIService.ExecQuery _
    ("Select * from Win32_Directory Where " _
        & "CreationDate > '" & dtmTargetDate & "'")

For each objFolder in colFolders
    dtmConvertedDate.Value = objFolder.CreationDate 
    Wscript.Echo objFolder.Name & VbTab & _
        dtmConvertedDate.GetVarDate(LOCAL_TIME)
Next
	

List the Subfolders of a Folder


Uses a WMI Associators of query to list all the subfolders of the folder C:\Scripts.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colSubfolders = objWMIService.ExecQuery _
    ("Associators of {Win32_Directory.Name='c:\scripts'} " _
        & "Where AssocClass = Win32_Subdirectory " _
            & "ResultRole = PartComponent")

For Each objFolder in colSubfolders
    Wscript.Echo objFolder.Name
Next
	

List Folder Properties


Demonstration script that uses the FileSystemObject to enumerate the properties of a folder. Script must be run on the local computer.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\Scripts")

Wscript.Echo "Date created: " & objFolder.DateCreated
Wscript.Echo "Date last accessed: " & objFolder.DateLastAccessed
Wscript.Echo "Date last modified: " & objFolder.DateLastModified
Wscript.Echo "Drive: " & objFolder.Drive
Wscript.Echo "Is root folder: " & objFolder.IsRootFolder
Wscript.Echo "Name: " & objFolder.Name
Wscript.Echo "Parent folder: " & objFolder.ParentFolder
Wscript.Echo "Path: " & objFolder.Path
Wscript.Echo "Short name: " & objFolder.ShortName
Wscript.Echo "Short path: " & objFolder.ShortPath
Wscript.Echo "Size: " & objFolder.Size
Wscript.Echo "Type: " & objFolder.Type
	

List Folder Properties


Lists the properties of the folder C:\Scripts.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colFolders = objWMIService. _
    ExecQuery("Select * from Win32_Directory where name = 'c:\\Scripts'")

For Each objFolder in colFolders
    Wscript.Echo "Archive: " & objFolder.Archive
    Wscript.Echo "Caption: " & objFolder.Caption
    Wscript.Echo "Compressed: " & objFolder.Compressed
    Wscript.Echo "Compression method: " & objFolder.CompressionMethod
    Wscript.Echo "Creation date: " & objFolder.CreationDate
    Wscript.Echo "Encrypted: " & objFolder.Encrypted
    Wscript.Echo "Encryption method: " & objFolder.EncryptionMethod
    Wscript.Echo "Hidden: " & objFolder.Hidden
    Wscript.Echo "In use count: " & objFolder.InUseCount
    Wscript.Echo "Last accessed: " & objFolder.LastAccessed
    Wscript.Echo "Last modified: " & objFolder.LastModified
    Wscript.Echo "Name: " & objFolder.Name
    Wscript.Echo "Path: " & objFolder.Path
    Wscript.Echo "Readable: " & objFolder.Readable
    Wscript.Echo "System: " & objFolder.System
    Wscript.Echo "Writeable: " & objFolder.Writeable
Next
	

List Network Shares


Lists all the shared folders on a computer.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colShares = objWMIService.ExecQuery("Select * from Win32_Share")

For each objShare in colShares
    Wscript.Echo "Allow Maximum: " & objShare.AllowMaximum   
    Wscript.Echo "Caption: " & objShare.Caption   
    Wscript.Echo "Maximum Allowed: " & objShare.MaximumAllowed
    Wscript.Echo "Name: " & objShare.Name   
    Wscript.Echo "Path: " & objShare.Path   
    Wscript.Echo "Type: " & objShare.Type   
Next
	

List the Subfolders of a Folder on the Local Computer


Demonstration script that uses the FileSystemObject to return the folder name and size for all the subfolders in a folder. Script must be run on the local computer.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\FSO")
Set colSubfolders = objFolder.Subfolders

For Each objSubfolder in colSubfolders
    Wscript.Echo objSubfolder.Name, objSubfolder.Size
Next
	

List Shared Folders Published in Active Directory


Lists all the shared folders that have been published in Active Directory.
Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = "Select Name, unCName, ManagedBy from " _
    & "'LDAP://DC=Fabrikam,DC=com' where objectClass='volume'"
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst

Do Until objRecordSet.EOF
    Wscript.Echo "Share Name: " & objRecordSet.Fields("Name").Value
    Wscript.Echo "UNC Name: " & objRecordSet.Fields("uNCName").Value
    Wscript.Echo "Managed By: " & objRecordSet.Fields("ManagedBy").Value
    objRecordSet.MoveNext
Loop
	

List Shell Object Verbs


Returns a list of Shell object verbs (context menu items) for the Recycle Bin.
Const RECYCLE_BIN = &Ha&

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.NameSpace(RECYCLE_BIN) 
Set objFolderItem = objFolder.Self      
Set colVerbs = objFolderItem.Verbs

For i = 0 to colVerbs.Count - 1
    Wscript.Echo colVerbs.Item(i)
Next
	

List a Specific Set of Folders


Returns a list of all the hidden folders on a computer.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService.ExecQuery _
    ("Select * from Win32_Directory Where Hidden = True")

For Each objFile in colFiles
    Wscript.Echo objFile.Name
Next
	

Map All Network Shares to Local Folders


Uses a WMI Associators of query to return the local path of a network share named Scripts.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colShares = objWMIService.ExecQuery("Select * From Win32_Share")

For Each objShare in colShares
    Set colAssociations = objWMIService.ExecQuery _
        ("Associators of {Win32_Share.Name='" & objShare.Name & "'} " _
            & " Where AssocClass=Win32_ShareToDirectory")
    For Each objFolder in colAssociations
        Wscript.Echo objShare.Name & vbTab & objFolder.Name
    Next
Next
	

Modify Folder Attributes


Demonstration script that uses the FileSystemObject to check if a folder is hidden and, if it is not, hides it. Script must be run on the local computer.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\FSO")

If objFolder.Attributes = objFolder.Attributes AND 2 Then
    objFolder.Attributes = objFolder.Attributes XOR 2 
End If
	

Move a Folder Using the Shell Object


Uses the Shell object to move the folder C:\Scripts to D:\Archives. Displays the Copying Files progress dialog as the folder is being moved.
Const FOF_CREATEPROGRESSDLG = &H0&

TargetFolder = "D:\Archive" 
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.NameSpace(TargetFolder) 
objFolder.MoveHere "C:\Scripts", FOF_CREATEPROGRESSDLG
	

Move a Folder Using WMI


strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colFolders = objWMIService.ExecQuery _ ("Select * from Win32_Directory where name = 'c:\\Scripts'") For Each objFolder in colFolders errResults = objFolder.Rename("C:\Admins\Documents\Archive\VBScript") Next
Uses WMI to move the folder C:\Scripts to C:\Admins\Documents\Archive\VBScript.

Modify a Network Share


Accesses a shared folder named FinanceShare, changes the maximum number of simultaneous connections to 50, and provides a new share description.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colShares = objWMIService.ExecQuery _
    ("Select * from Win32_Share Where Name = 'FinanceShare'")

For Each objShare in colShares
    errReturn = objShare.SetShareInfo(50, _
        "Public share for HR administrators and the Finance Group.")
Next
	

Map a Network Share to a Local Folder


Uses a WMI Associators of query to return the local path of all the network shares on a computer.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colShares = objWMIService.ExecQuery _
    ("Associators of {Win32_Share.Name='Scripts'} Where " _
        & "AssocClass=Win32_ShareToDirectory")

For Each objFolder in colShares
    Wscript.Echo objFolder.Name
Next
	

Move a Folder


Demonstration script that uses the FileSystemObject to move a folder from one location to another. Script must be run on the local computer.
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFolder "C:\Scripts" , "M:\helpdesk\management"
	

Publish a Shared Folder in Active Directory


Publishes a shared folder in Active Directory, assigning the folder a description and three keywords.
Set objComputer = GetObject _
    ("LDAP://OU=Finance, DC=fabrikam, DC=com")

Set objShare = objComputer.Create("volume", "CN=FinanceShare")

objShare.Put "uNCName", "\\atl-dc-02\FinanceShare"
objShare.Put "Description", "Public share for users in the Finance group."
objShare.Put "Keywords", Array("finance", "fiscal", "monetary") 
objShare.SetInfo
	

Rename a Folder


Renames the folder C:\Scripts to C:\Repository.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colFolders = objWMIService.ExecQuery _
    ("Select * from Win32_Directory where name = 'c:\\Scripts'")

For Each objFolder in colFolders
    errResults = objFolder.Rename("C:\Script Repository")
Next
	

Rename a Folder on the Local Computer


Demonstration script that uses the FileSystemObject to rename a folder. Script must be run on the local computer.
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFolder "C:\FSO\Samples" , "C:\FSO\Scripts"
	

Search for Folders by Date


Finds all the folders created after March 1, 2002.
On Error Resume Next

dtmTargetDate = "20020301000000.000000-420"

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

Set colFolders = objWMIService.ExecQuery _
    ("Select * from Win32_Directory Where CreationDate > '" & _
        dtmtargetDate & "'")
 
For Each objFolder in colFolders
    Wscript.Echo objFolder.Name 
Next
	

Search for Folders Using Wildcards


Returns a list of all the folders whose path begins with C:\S.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colFolders = objWMIService.ExecQuery _
    ("Select * from Win32_Directory where Name Like '%c:\\S%'")

For Each objFolder in colFolders
    Wscript.Echo "Name: " & objFolder.Name
Next
	

Search for Specific Published Folders in Active Directory


Searches Active Directory for any shared folders that have the keyword "finance."
On Error Resume Next

Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = "Select Name, unCName, ManagedBy from "
    & "'LDAP://DC=Reskit,DC=com'" _
        & " where objectClass='volume' and Keywords = 'finance*'"
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst

Do Until objRecordSet.EOF
    Wscript.Echo "Share Name: " & objRecordSet.Fields("Name").Value
    Wscript.Echo "UNC Name: " & objRecordSet.Fields("uNCName").Value
    Wscript.Echo "Managed By: " & objRecordSet.Fields("ManagedBy").Value
    objRecordSet.MoveNext
Loop
	

Uncompress a Folder


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

Set colFolders = objWMIService.ExecQuery _
    ("Select * from Win32_Directory where name = 'c:\\Scripts'")

For Each objFolder in colFolders
    errResults = objFolder.Uncompress
Next
	

Verify that a Folder Exists


Demonstration script that uses the FileSystemObject to verify that a folder exists. If the folder does exist, the script binds to that folder. Script must be run on the local computer.
Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FolderExists("C:\FSO") Then
    Set objFolder = objFSO.GetFolder("C:\FSO")
Else
    Wscript.Echo "Folder does not exist."
End If