Scripts to manage Folders
Changing Folder AttributesCompressing Folders
Copying a Folder
Copying Folders Using WMI
Copying Folders Using the Shell Object
Creating a Folder
Creating New Folders
Deleting a Folder
Deleting Folders
Enumerating All the Folders on a Computer
Enumerating Folder Attributes
Enumerating Folder Properties
Enumerating Folders Using Dates
Enumerating a Specific Set of Folders
Enumerating Subfolders in a Folder
Finding Folders by Date
Identifying Shell Object Verbs
Moving a Folder
Moving Folders Using the Shell Object
Moving Folders Using WMI
Renaming a Folder
Renaming Folders
Retrieving Folder Properties
Uncompressing Folders
Using the Browse for Folder Dialog Box
Using Recursion to Enumerate Subfolders
Using Wildcards in a Folder Query
Verifying that a Folder Exists
Changing 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. 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
Compressing Folders
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 Wscript.Echo errResults Next
Copying 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
Copying Folders 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") Wscript.Echo errResults Next
Copying Folders 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
Creating 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")
Creating New Folders
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"
Deleting a Folder
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")
Deleting Folders
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 Wscript.Echo errResults Next
Enumerating 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
Enumerating 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
Enumerating 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
Enumerating Folders Using Dates
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/2002", LOCAL_TIME strComputer = "." Set objWMIService = GetObject _ ("winmgmts:" & "!\\" & 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
Enumerating 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
Enumerating Subfolders in a Folder
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
Finding Folders by Date
Finds all the folders created after March 1, 2002. To modify this script, you must modify the following items in the value assigned to the variable dtmTargetDate: 2002 -- Change this to the target year (for example, 1999). 03 -- Change this to the target month (01 for January, 02 for February … 12 for December). 01 -- Change this to the target day (01 for the first day of the month, 02 for the second, etc.). -420 -- To assure the correct results, change this to the offset between your time zone and Greenwich Mean Time. If you do not know the offset, use the script Determining Time Zone Offset from Greenwich Mean Time.
On Error Resume Next dtmTargetDate = "20020301000000.000000-420" strComputer = "." Set objWMIService = GetObject _ ("winmgmts:" & "!\\" & strComputer & "\root\cimv2") Set colFolders = objWMIService.ExecQuery _ ("Select * from Win32_Directory Where CreationDate > '" & _ dtmtargetDate & "'") For Each objFolder in colFolders Wscript.Echo objFolder.Name Next
Identifying 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
Moving a Folder
Demonstration script that uses the FileSystermObject 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"
Moving Folders 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
Moving Folders Using WMI
Uses WMI to move the folder C:\Scripts to C:\Admins\Documents\Archive\VBScript.
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") Wscript.Echo errResults Next
Renaming a Folder
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"
Renaming Folders
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") Wscript.Echo errResults Next
Retrieving 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
Uncompressing Folders
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 Wscript.Echo errResults Next
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:" & "!\\" & 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
Using Recursion to Enumerate Subfolders
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
Using Wildcards in a Folder Query
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
Verifying 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