VB icon

General File System Object Functions

Email
Submitted on: 1/6/2015 6:35:00 PM
By: Vladimir Miho (from psc cd)  
Level: Intermediate
User Rating: By 4 Users
Compatibility: ASP (Active Server Pages)
Views: 567
 
     These are few general functions i have written related to File System Object: - Check if a SPECIFIC folder exists - Check if A SPECIFIC file EXISTS in A SPECIFIC folder - Create A SPECIFIC folder - Delete A SPECIFIC folder - Delete ALL FILES in a SPECIFIC folder - Delete A SPECIFIC file in A SPECIFIC folder Your comments and highly appreciated. And finally thanks to all coders here at PSC for sharing their knowledge and their work!
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: General File System Object Functions
' Description:These are few general functions i have written related to File System Object:
- Check if a SPECIFIC folder exists
- Check if A SPECIFIC file EXISTS in A SPECIFIC folder
- Create A SPECIFIC folder
- Delete A SPECIFIC folder
- Delete ALL FILES in a SPECIFIC folder
- Delete A SPECIFIC file in A SPECIFIC folder
Your comments and highly appreciated. 
And finally thanks to all coders here at PSC for sharing their knowledge and their work!
' By: Vladimir Miho (from psc cd)
'**************************************

<%
'================================================================='
'Check if a SPECIFIC folder exists								 '
'Input:															 '
'		- Path: the path where the folder is suppose to be		 '
'		- FolderName: the name of the folder you want to check for'
'Output:														 '
'		- True: if the folder exists							 '
'		- False: if the folder does not exist					 '
'================================================================='
Function CheckIfFolderExists(byVal Path, byVal FolderName)
	Dim objFSO
	Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
	dim FullPath
	FullPath = Path & FolderName
	if objFSO.FolderExists(FullPath) then
		CheckIfFolderExists = true
	else
		CheckIfFolderExists = false
	end if
	set objFSO = nothing
End Function
'-----------------------------------------------------------------'
'================================================================='
'Checks if A SPECIFIC file EXISTS in A SPECIFIC folder			 '
'Input:															 '
'		- FolderPath: the path of the folder					 '
'		- FileName: the name of the file you want to check for'
'Output:														 '
'		- True: if the file exists								 '
'		- False: if the file does not exist						 '
'================================================================='
Function CheckIfFileExists(byVal FolderPath, byVal FileName)
	CheckIfFileExists = false
	Dim objFSO
	Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
	set objFolder = objFSO.GetFolder(FolderPath)
		for each file in objFolder.files
			if Lcase(file.name) = Trim(LCase(Filename)) then
				CheckIfFileExists = true
			end if
		next
	set objFSO = nothing
End Function
'-----------------------------------------------------------------'
'================================================================='
'Creates A SPECIFIC folder										 '
'Input:															 '
'		- Path: path where you want to create the folder		 '
'		- NewFolderName: the name of the new folder			 '
'Output:														 '
'		- True: if the folder is created successfully			 '
'		- False: if the folder creation failed					 '
'================================================================='
Function CreateNewFolder(byVal Path, byVal NewFolderName)
	Dim objFSO
	Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
	dim FullPath
	FullPath = Path & NewFolderName
	if not objFSO.FolderExists(FullPath) then
		objFSO.CreateFolder(FullPath)
		CreateNewFolder = true
	else
		CreateNewFolder = false
	end if
	set objFSO = nothing
End Function
'-----------------------------------------------------------------'
'================================================================='
'Deletes A SPECIFIC folder										 '
'Input:															 '
'		- Path: path where the folder is						 '
'		- FolderName: the name of the folder you want to delete'
'Output:														 '
'		- True: if the folder is deleted						 '
'		- False: if the folder could not be deleted				 '
'================================================================='
Function DeleteFolder(byVal Path, byVal FolderName)
	Dim objFSO
	Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
	dim FullPath
	FullPath = Path & FolderName
	if objFSO.FolderExists(FullPath) then
		objFSO.DeleteFolder(FullPath)
		DeleteFolder = true
	else
		DeleteFolder = false
	end if
	set objFSO = nothing
End Function
'-----------------------------------------------------------------'
'================================================================='
'Deletes ALL FILES in a SPECIFIC folder							 '
'Input:															 '
'		- Path: path where the folder is						 '
'		- FolderName: the name of the folder where the files you '
'					 want to delete are						 '
'Output: none													 '
'================================================================='
Function DeleteFilesInFolder(byVal Path, byVal FolderName)
	Dim objFSO
	Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
	dim FullPath
	FullPath = Path & FolderName
	set objFolder = objFSO.GetFolder(FullPath)
		for each file in objFolder.files
			file.delete(true)
		next
	set objFSO = nothing
End Function
'-----------------------------------------------------------------'
'================================================================='
'Deletes A SPECIFIC file in A SPECIFIC folder					 '
'Input:															 '
'		- Path: path where the folder is						 '
'		- FolderName: the name of the folder where the file you'
'					 want to delete is							 '
'		- FileName: name of the file you want to delete			 '
'Output:														 '
'		- True: if the file is deleted						 '
'		- False: if the file could not be deleted				 '
'================================================================='
Function DeleteFileInFolder(byVal Path, byVal FolderName, byVal FileName)
	DeleteFileInFolder = False
	Dim objFSO
	Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
	dim FullPath
	FullPath = Path & FolderName 
	set objFolder = objFSO.GetFolder(FullPath)
		for each file in objFolder.files
			if Lcase(file.name) = Trim(LCase(Filename)) then
				file.delete(true)
				DeleteFileInFolder = True
			end if
		next
	set objFSO = nothing
End Function
'-----------------------------------------------------------------'
%>


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.