DesktopX: Using the File System Object

From WinCustomize Wiki

Jump to: navigation, search
DesktopX: Using the File System Object
Original Author:Skarn
Date Created:August 24, 2006
Application:DesktopX
Programs Used:DesktopX

Contents

[edit] Introduction

The File System Object gives access to various file management and information functions.

The following will hopefully give you some clues to using the FSO with DesktopX.

[edit] Getting the FSO 'into' a DesktopX Object

To start using the FSO you need to give your script access to it by loading it in using the VBScript Set function.

When I am finished with an object which is loaded using the Set command, I make a habit of setting it back to Nothing when I'm done with it.

Dim fso

Sub your_fso_subroutine
 Set fso = CreateObject("Scripting.FileSystemObject")

 'Your fso commands here

 Set fso = Nothing
End Sub

[edit] Playing with a single file object

Dim fso

Sub your_fso_subroutine
 Set fso = CreateObject("Scripting.FileSystemObject")

 'Use the fso.CreateTextFile method to create a file for our script purposes  
 fso.CreateTextFile("c:\testfile.txt")

 'Create a file object that points to this file.
 Set o_file = fso.GetFile("c:\testfile.txt")
 
 'o_file now contains an object reference to the file specified in the quotes.
 'You can call on the properties of the file using 'o_file' similar to how you
 'would with a DesktopX object.  
 
 'Some properties
 name = o_file.Name                  'returns file name as string
 date_created = o_file.DateCreated   'returns date created as string
 
 'A method
 o_file.Copy("c:\windows\temp\")     'makes a copy of the file linked to o_file to the quoted directory
  
 Set o_file = Nothing
 Set fso = Nothing
End Sub

There are many other properties and methods you can access through an FSO file object. You can do the same with individual folders using the GetFolder method.

[edit] Using the fso. object on it's own

You don't need to create a file or folder object to carry out queries/commands. The fso object has a number of methods and properties of it's own you can invoke to carry out file management tasks.

The following creates a text file, checks if it exists and if it does, copies it to the windows directory, then deletes both copies.

Dim fso

Sub your_fso_subroutine
 Set fso = CreateObject("Scripting.FileSystemObject")
 
 fso.CreateTextFile("c:\testfile.txt", True)
 
 If fso.FileExists("c:\testfile.txt") = True then 
  fso.CopyFile "c:\testfile.txt","c:\windows\"
  fso.DeleteFile("c:\testfile.txt")
  fso.DeleteFile("c:\windows\testfile.txt")
 End If

 Set o_file = Nothing
 Set fso = Nothing
End Sub

[edit] FSO MSDN library

Although it can be a little overwhelming, all the FSO methods and properties, as well as the subset objects (like a file or folder object) that can be created with an FSO object are documented on the MSDN library