Reads and saves text file giving its full name
Used a lot in my websites, very handy tool
ReadMethod ...
' =1, reads/saves using ADODB stream, used for UTF-8 Unicode files.
' =2, reads/saves using FileSystemObject, does not work with UTF-8
Edit 2020-10-13: Add ability to avoid error if file is 0 bytes during read
Function ASCII_Read(File2, ReadMethod)
' ReadMethod
' =1, read using ADODB stream, used for UTF-8 Unicode files.
' =2, reads using FileSystemObject, does not work with UTF-8
Rett = ""
If ReadMethod = 1 Then ' ADODB
Dim objStream
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Charset = "utf-8"
objStream.Open
objStream.LoadFromFile (Server.MapPath(File2))
Rett = objStream.ReadText()
objStream.Close
Set objStream = Nothing
ElseIf ReadMethod = 2 Then ' FSO
Dim Fso, oFi
Const ForReading = 1 ' ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateTrue= 1 ' 0 for ASCII, 1 for Unicode, 2 for System default
Set Fso = CreateObject("Scripting.FileSystemObject") ' For VBA
Set oFi = Fso.OpenTextFile(Server.MapPath(File2), ForReading, TristateTrue)
if Not oFi.AtEndOfStream Then Rett = oFi.ReadAll
Set oFi = Nothing
Set Fso = Nothing
End If
ASCII_Read = Rett
End Function
Function ASCII_Save(File2, FileContent, SaveMethod)
' FileContent having the content of the file
' SaveMethod
' =1, save using ADODB stream, does not need Dir(), for UTF-8
' =2, save using Filesystemobject, does not need Dir()
' =3, save using Open+OutPut, replace file if found
' =4, save using Open+Append, appends file if found
If SaveMethod = 1 Then ' ADODB
Dim fsT
Set fsT = CreateObject("ADODB.Stream")
fsT.Type = 2 ' Specify stream type - we want To save text/string data.
fsT.Charset = "utf-8" ' Specify charset For the source text data.
fsT.Open ' Open the stream And write binary data To the object
fsT.WriteText FileContent
fsT.SaveToFile Server.MapPath(File2), 2 'Save binary data To disk
Set fsT = Nothing
ElseIf SaveMethod = 2 Then ' FSO
'Set FSO = CreateObject("scripting.FileSystemObject") ' for VBA
Set myFile = Fso.CreateTextFile(Server.MapPath(File2), true)
myFile.WriteLine(FileContent)
myFile.Close
ElseIf SaveMethod = 3 Then ' Save over
' Close
' Open File2 For Output As #1
' Print #1, FileContent
' Close
ElseIf SaveMethod = 4 Then ' Append
' Close
' Open File2 For Append As #1
' Print #1, FileContent
' Close
End If
End Function
' ReadMethod
' =1, read using ADODB stream, used for UTF-8 Unicode files.
' =2, reads using FileSystemObject, does not work with UTF-8
Rett = ""
If ReadMethod = 1 Then ' ADODB
Dim objStream
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Charset = "utf-8"
objStream.Open
objStream.LoadFromFile (Server.MapPath(File2))
Rett = objStream.ReadText()
objStream.Close
Set objStream = Nothing
ElseIf ReadMethod = 2 Then ' FSO
Dim Fso, oFi
Const ForReading = 1 ' ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateTrue= 1 ' 0 for ASCII, 1 for Unicode, 2 for System default
Set Fso = CreateObject("Scripting.FileSystemObject") ' For VBA
Set oFi = Fso.OpenTextFile(Server.MapPath(File2), ForReading, TristateTrue)
if Not oFi.AtEndOfStream Then Rett = oFi.ReadAll
Set oFi = Nothing
Set Fso = Nothing
End If
ASCII_Read = Rett
End Function
Function ASCII_Save(File2, FileContent, SaveMethod)
' FileContent having the content of the file
' SaveMethod
' =1, save using ADODB stream, does not need Dir(), for UTF-8
' =2, save using Filesystemobject, does not need Dir()
' =3, save using Open+OutPut, replace file if found
' =4, save using Open+Append, appends file if found
If SaveMethod = 1 Then ' ADODB
Dim fsT
Set fsT = CreateObject("ADODB.Stream")
fsT.Type = 2 ' Specify stream type - we want To save text/string data.
fsT.Charset = "utf-8" ' Specify charset For the source text data.
fsT.Open ' Open the stream And write binary data To the object
fsT.WriteText FileContent
fsT.SaveToFile Server.MapPath(File2), 2 'Save binary data To disk
Set fsT = Nothing
ElseIf SaveMethod = 2 Then ' FSO
'Set FSO = CreateObject("scripting.FileSystemObject") ' for VBA
Set myFile = Fso.CreateTextFile(Server.MapPath(File2), true)
myFile.WriteLine(FileContent)
myFile.Close
ElseIf SaveMethod = 3 Then ' Save over
' Close
' Open File2 For Output As #1
' Print #1, FileContent
' Close
ElseIf SaveMethod = 4 Then ' Append
' Close
' Open File2 For Append As #1
' Print #1, FileContent
' Close
End If
End Function
File2, ReadMethod
OR
File2, FileContent, SaveMethod
OR
File2, FileContent, SaveMethod
Views 4,361
Downloads 1,270
CodeID
DB ID
ANmarAmdeen
610
Revisions
v4.0
Tuesday
May
16
2023