The most basic storage unit of any operating system is a file. Files can contain anything from textual data in this article, a bitmap image of your baby daughter, a MS Excel worksheet containing your finances or a Visual Basic program you are currently working on. Therefore it is very important to know how to manipulate files after they are created.
Common actions that are performed on files are copy, rename, move and delete. Each file has properties such as creation date, last date modified, and attributes such as read only. I have encapsulating the logic for working with files via File System Object (FSO), into a abstract class.
The Manage Files class has the following properties and methods.
- SetFn – Set the full path of a workable file.
- CopyFn – Copy the file to a new destination.
- MoveFn – Move the file to a new location.
- DeleteFn – Remove the file from a current location.
- ExistFn – Test the existance of a file.
- GetFileName – Return the file name w/o path.
- GetDirName – Return the path w/o file name.
- GetLastAccess – Time of this event.
- GetDateCreated – Time of this event
- GetLastModifiied – Time of this event.
- GetFileSizeInBytes – Return the file size.
- GetFileAttributes – Get the attributes such as read only.
- SetFileAttributes – Set the attributes such as read only.
- MakeFileStamp – Return current time in mmddyy – hhmmss format.
The following actions are carried out in the test program to give you a good idea of what can be functionally done.
- Set File Name to file0
- Show file name
- Show directory name
- Show Size
- Show Date Created
- Show Date Accessed
- Show Date Modified
- Show Attributes
- Copy file0 to directory 2 as file1
- Copy file0 to directory 2 as file2
- Copy file0 to directory 2 as file3 with timestamp
- Move file2 to directory 1
- Make file0 read only
- Try to delete file0
A great application of this class would be the archiving of inbound FTP files after uploading to a SQL Server database via an ETL program written in VB Script. Managing files and directories is a key element of any data driven application. Having classes that perform these actions reduces the time to develop a business solution.
The table below has starter code that you can use in your next Visual Basic (VB) Script
|mod-manage-files.vbs||Manage Files Class|
The output below was generated by the execution of the test program.