Using the FileSystemObject to Retrieve Text From a File - Classic ASP

Another section of old ASP code from 10+ years ago. This example retrieves the contents of a text file. With the security placed on most web hosting companies now, its unlikely this code will work in a hosted environment.

' * ==================================================================================================
' Saves the contents of a string as a file
Function RetrieveFile(sFile,sFolder)
	
	Dim oFSO, sFolderLocation, bFileExists, sFileName, oTextStream
	Dim sHTML
	
	bFileExists = false
	Set oFSO = server.CreateObject("SCRIPTING.FileSystemObject")
	If sFolder <> "" Then sFolderLocation = Server.MapPath(sFolder) & "\"
	sFileName = sFolderLocation & sFile 
	bFileExists = oFSO.FileExists(sFileName)
		
	If bFileExists = True Then
		
		Set oTextStream = oFSO.OpenTextFile(sFileName)
		sHTML = oTextStream.ReadAll
		Set oTextStream = Nothing

	Else

		sHTML = "NO FILE TO RETRIEVE"

	End If

	Set oFolder = Nothing
	Set oFSO = nothing

	RetrieveFile = sHTML

End Function

' * ==================================================================================================

AuthorInfo

Joe is a User Experience Engineer, Front End Specialist and Application Developer for Bridgeway Software, Inc and part-time consultant for NGO and non-profit organizations in the Houston area. Joe is also a part-time Portrait Photographer at MEDIA.24Moves.com and organizer of the HoustonPhotowalks.com photography club. He and his wife Marty run the AutismHouston.com Support and Playdate group.

-

@ext237

-

@drupaltips

-

Flickr

-

LinkedIn

-

Ports

-

Contact