File or Folder Exists - Create and Remove a Folder - May 2009

The tip-of-the-month shows how to check if a file or folder exists and create or remove a folder. Two classes are contained in module cFileFolderExists.vb. The classes are cExistsFile and cExistsFolder. The tip is based upon code I found at FreeVBCode.Com. Thanks.

I created an Asp.Net, VB.Net project to demonstrate the class methods. The demo:

File Exists - Enter the fully qualified file name then click 'Test if the File Exists' to determine the existence of the file. Upon execution the file described in this tip is tested for existence.

Folder Exists and Compression Status - Enter a folder name then click 'Test if the Folder Exists and Is Compressed' to check the existence of the folder and to determine if the folder is compressed. I needed to know if a folder was compressed before creating SQL Server 2005 files in the folder. SQL Server only supports data files contained in uncompressed folders. Upon execution the folder containing this tip is used - App.Path.

Create Folders - Enter the name of a folder then click 'Create a New Folder' to create the folder. No part of the folder needs to exists. The routine will create the folder and all subfolders. Upon execution 'temp\temp\temp' folders are created under 'App.Path'.

Remove Folders - If 'Remove Recursive' is checked, click 'Remove the Folder and Subfolders' to remove the specified folder and all subfolders. Use caution. For example you do not want to remove the Windows folder - although I suspect the removal will fail. For obvious reasons I did not test using the Windows folder. If 'Remove Recursive' is not checked then removing a non empty folder is not allowed and an error message is issued.

cExistsFile - Methods

Methods Description Returns Sample
.Exists(sFileFullPath) Does the File Exist True or False Dim oEF As New cExistsFile
oEF.Exists("c:\AutoExec.bat")
.DoesFileExist(sFileFullPath) Alias for .Exists True or False  

cExistsFolder - Methods

Methods Description Returns Sample
.Exists(sFolder) Does the Folder Exist True or False Dim oEF As New cExistsFolder
oEF.Exists("c:\Windows")
.DoesFolderExist(sFolder) Alias for .Exists True or False  
.IsFolderCompressed(sFolder) Is the folder compressed. True or False  
.CreateFolder(sFolder) Create the folder. True or False Dim oEF As New cExistsFolder
oEF.CreateFolder("c:\xxx\xxx\xxx")
.DeleteFolder(sFolder[, bRecursive]) Remove the folder and if bRecursive is True then remove subfolders. bRecursive = True is the default. True or False Dim oEF As New cExistsFolder
oEF.DeleteFolder("c:\xxx")

Download VB.Net Code

Download - VS2005 VB.Net Zipped File  Size 
File Folder Exists Project FileFolderExists.zip 27K

Previous Tip   Tip Index    Next

Home

Updated 2011/04/16