Common functions for FSO operation

Source: Internet
Author: User
Tags exit end functions parent directory relative
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
%>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.