fso| function
The following is a few in ASP often applied to the FSO operation function, practical fine code, recommended collection!
<%
' function: To determine whether the file name is legitimate
' Isfilename [filename]
' filename cannot contain any of the following characters
' \ / : * ? "< > |
Function Isfilename (sFileName)
Dim Serrorstr, I
Isfilename = TRUE
Serrorstr = Array ("\", "/", ":", "*", "?", "" "", "<", ">", "|")
If Len (sFileName & "") = 0 Then isfilename = false:exit Function
For i = 0 to 8
If InStr (sFileName, Serrorstr (i)) > 0 Then
Isfilename = FALSE
End If
Next
End Function
%>
<%
' function: Deletes a directory. All subdirectories and files under the specified directory are also deleted, in addition to the directory itself. Used to delete a directory tree.
' RD [Drive:]path
' Supports deletion of multilevel directories, supporting relative paths and absolute paths.
"Support with" ... "Specifies the parent directory of the parent directory.
"Requires the path function below
Function RD (ByVal spath)
On Error Resume Next
Dim oFSO
spath = Path (spath) '//here needs the path function
Set oFSO = Server.CreateObject ("Scripting.FileSystemObject")
If ofso.folderexists (spath) Then
Ofso.deletefolder spath, True
RD = True
End If
Set oFSO = Nothing
If err.number > 0 Then
Err.Clear ()
RD = False
Else
RD = True
End If
End Function
%>
<%
' Function: Create a table of contents.
' MD [Drive:]path
' supports creating multilevel directories, supporting relative paths and absolute paths.
"Support with" ... "Specifies the parent directory of the parent directory.
' Need the path function below
Function MD (spath)
On Error Resume Next
Dim Apath, IPath, I, sTmpPath
Dim oFSO
spath = Path (spath) '//here needs the path function
Set oFSO = Server.CreateObject ("Scripting.FileSystemObject")
If ofso.folderexists (spath) Then MD = True:exit Function
Apath = Split (spath, "\")
IPath = UBound (Apath)
sTmpPath = ""
For i = 0 to IPath
sTmpPath = sTmpPath & Apath (i) & "\"
If not ofso.folderexists (sTmpPath) Then
Ofso.createfolder (sTmpPath)
End If
Next
Set oFSO = Nothing
If err.number > 0 Then
Err.Clear ()
MD = False
Else
MD = True
End If
End Function
%>
<%
' function: compute an absolute path to a directory.
' PATH [Drive:]path
' Supports multilevel directories, supporting relative paths and absolute paths.
"Support with" ... "Specifies the parent directory of the parent directory.
Function Path (ByVal spath)
On Error Resume Next
If Len (spath& "") = 0 Then spath = "./"
If Right (spath, 1) = ":" Then spath = spath & "\"
spath = Replace (spath, "/", "\")
spath = ReplaceAll (spath, "\", "\", False)
spath = ReplaceAll (spath, "...", ".. \..., False)
If (InStr (spath, ":") > 0) Then
spath = spath
Else
spath = Server.MapPath (spath)
End If
Path = spath
End Function
%>
<%
' function: To determine if a file already exists.
' Isfileexist (filename)
Public Function isfileexist (ByVal sfilename)
On Error Resume Next
Dim oFSO
sFileName = PATH (sfilename)
Set oFSO = CreateObject ("Scripting.FileSystemObject")
Isfileexist = ofso.fileexists (sfilename)
Set oFSO = Nothing
End Function
%>
<%
' function: To determine if a folder already exists.
' Isfolderexist (filename)
Public Function isfolderexist (ByVal sfoldername)
On Error Resume Next
Dim oFSO
Sfoldername = PATH (sfoldername)
Set oFSO = CreateObject ("Scripting.FileSystemObject")
Isfolderexist = ofso.folderexists (sfoldername)
Set oFSO = Nothing
End Function
%>
<%
Feature: Creates a decimal text file. The
' CreateTextFile (file content, filename)
' file name supports relative and absolute paths.
Support using ... "Specifies the parent directory of the parent directory.
Function CreateTextFile (ByVal stext, ByVal sfilename)
on Error Resume Next
sFileName = Path (sfilename) set oFSO = CreateObject ("Scripting.FileSystemObject")
Set owrite = Ofso.opentextfile (sFileName, 2, True)
Owrite.write stext
Owrite.close
Set ofso = Nothing
Set owrite = Nothing
If err.number > 0 Then
Err. Clear ()
CreateTextFile = False
Else
CreateTextFile = True
End If
End Function
%>