Folders and Folder Objects

Microsoft® Windows® 2000 Scripting Guide

Folders are file system objects designed to contain other file system objects. This does not mean that all folders are alike, however. Instead, folders can vary considerably. Some folders are operating system folders, which generally should not be modified by a script. Some folders are read-only, which means that users can access the contents of that folder but cannot add to, delete from, or modify those contents. Some folders are compressed for optimal storage, while others are hidden and not visible to users.

WMI uses the Win32_Directory class to manage folders. Significantly, the properties and methods available in this class are identical to the properties and methods available in the CIM_DataFile class, the class used to manage files. This means that after you have learned how to manage folders using WMI, you will, without any extra work, also know how to manage files.

Some of the more useful Win32_Directory class properties are shown in Table 11.1. These properties are identical to the properties found in the CIM_DataFile class.

Table 11.1 Win32_Directory Properties

Property

Description

Archive

Boolean value indicating whether the archive bit on the folder has been set. The archive bit is used by backup programs to identify files that should be backed up.

Compressed

Boolean value indicating whether or not the folder has been compressed. WMI recognizes folders compressed using WMI itself or using the graphical user interface; it does not, however, recognize .ZIP files as being compressed.

CompressionMethod

Method used to compress the file system object. Often reported simply as "Compressed."

CreationDate

Date that the file system object was created.

CSName

Name of the computer where the file system object is stored.

Drive

Drive letter of the drive where the file system object is stored.

EightDotThreeFileName

MS-DOS®-compatible name for the folder. For example, the EightDotThreeFileName for the folder C:\Program Files might be C:\Progra~1.

Encrypted

Boolean value indicating whether or not the folder has been encrypted.

EncryptionMethod

Method used to encrypt the file system object. Often reported simply as "Encryption."

Extension

File name extension for the file system object, not including the dot (.) that separates the extension from the file name.

FileName

Name of the file system object, not including the extension.

FileSize

Size of the file system object, in bytes. Although folders possess a FileSize property, the value 0 is always returned. To determine the size of a folder, use the FileSystemObject or add up the size of all the files stored in the folder.

FileType

Type of file system object, based on the extension and the default registration for that extension. For example, an .mdb file is likely to have the file type Microsoft Access Application. An .asp file likely has the file type HTML Document. Folders are typically reported simply as Folder.

FSName

Type of file system (NTFS, FAT, FAT32) installed on the drive where the file or folder is located.

Hidden

Boolean value indicating whether the file system object is hidden.

LastAccessed

Date that the object was last accessed.

LastModified

Date that the object was last modified.

Name

Full path name of the file system object. For example: c:\windows\system32\wbem.

Path

Path for the folder. The path includes the leading and trailing backslashes, but not the drive letter or the folder name. For the folder c:\windows\system32\wbem, the path is \windows\system32\. For the folder c:\scripts, the path is \.

Readable

Boolean value indicating whether you can read items in the folder.

System

Boolean value indicating whether the object is a system folder.

Writeable

Boolean value indicating whether you can write to the folder.

The relationship between the Win32_Directory properties and the folder properties available through Windows Explorer is shown in Figure 11.2.

Figure 11.2 Win32_Directory and Windows Explorer

sas_fil_02c

In addition, the Win32_Directory and CIM_DataFile classes share the same set of methods. Some of the more commonly used methods are shown in Table 11.2.

Table 11.2 Win32_Directory and CIM_DataFile Methods

Method

Description

Copy

Copies the file system object to a new location.

Rename

Renames the file system object.

Delete

Deletes the object from the file system.

Compress

Compresses a file system object using Windows compression.

Uncompress

Uncompresses a file system object that was compressed using Windows compression.

These methods return the values shown in Table 11.3. Because files and folders share the same set of methods, you can copy, delete, or rename both files and folders using the same approach. Similarly, you can easily add error handling to file and folder scripts; the code used to interpret error codes in one script is exactly the same as the code used to interpret error codes in any other file or folder management script.

Table 11.3 WMI File and Folder Return Values

Value

Description

0

Operation completed successfully.

2

Access denied.

8

An unspecified failure occurred.

9

Invalid name.

10

Object already exists.

11

The file system is not NTFS.

12

The platform is not Windows NT®, Windows 2000, or Windows® XP.

13

The drive is not the same.

14

The folder is not empty.

15

A sharing violation has occurred.

16

Invalid file.

17

The user does not have the rights required to perform this operation.

21

Invalid parameter.

In addition to the Win32_Directory class, the Win32_Subdirectory association class is also used to manage files and folders. The Win32_Subdirectory class relates a folder and its immediate subfolders. For example, in the folder structure C:\Scripts\Logs, Logs is a subfolder of Scripts, and Scripts is a subfolder of the root folder C:\. However, Logs is not considered a subfolder of C:\.