ASP function: Check whether a folder exists. If no folder exists, create a folder.

Source: Internet
Author: User
Tags servervariables

Public Function checkandcreatefolder (Foldername)
FLDR = server. mappath (Foldername)
Set FSO = Createobject ("scripting. FileSystemObject ")
If not FSO. folderexists (FLDR) then
FSO. createfolder (FLDR)
End if
Set FSO = nothing
End Function

Check whether the folder exists. If the folder does not exist, create a folder. This function does not return a value.

Example: checkandcreatefolder ("asp ")
Check whether ASP folders exist in the current directory. If no ASP folder exists, create ASP folders.

Asp fso Functions

'// Provides a common interface for File Processing
Class FileSystemObject
'* Function Description: delete an object
'* Input parameter: Filename -- file relative path
Public Function delfile (filename)
Dim getpath
Getpath = "/"
Set FSO = server. Createobject ("scripting. FileSystemObject ")
Getpath = Replace (getpath & filename ,"//","/")
If FSO. fileexists (server. mappath (getpath) = true then
FSO. deletefile server. mappath (getpath)
End if
Set FSO = nothing
End Function


'* Function Description: determines whether a path exists. A path is created if it does not exist.
'* Input parameter: SaveFilePath -- relative path, for example,/uploadfiles/newsfiles
Public Function createpath (SaveFilePath)
Dim declarepath, fileobj, filepath
Declarepath = "/"

Set fileobj = server. Createobject ("scripting. FileSystemObject ")
For each filepath in Split (SaveFilePath ,"/")
Declarepath = Replace (declarepath & filepath &"/","//","/")
If fileobj. folderexists (server. mappath (declarepath) = false then
Fileobj. createfolder (server. mappath (declarepath) 'create a folder
End if
Set fileobj = nothing
Createpath = declarepath
End Function


'* Function Description: rename a folder
'* Input parameter: getpath -- folder path
'* Input parameter: oldname -- Name of the old folder
'* Input parameter: newname -- New Folder name
Public Function renfolder (getpath, oldname, newname)
If oldname = "" Or newname = "" then
Exit Function
If oldname = newname then exit function
End if
Set FSO = server. Createobject ("scripting. FileSystemObject ")
If FSO. folderexists (server. mappath (getpath & newname) then
Response. Write "<script language = JavaScript> alert ('Directory already exists !! '); This. History. Go (-1); </SCRIPT>"
Response. End ()
End if
'// If the old Folder does not exist, create
If not FSO. folderexists (server. mappath (getpath & oldname) then
Createpath (getpath & oldname)
End if

FSO. movefolder server. mappath (getpath & oldname), server. mappath (getpath & newname)
Set FSO = nothing
'Response. redirect request. servervariables ("http_referer ")
End Function


'* Function Description: Save the current file
'* Input parameter: getpath -- file path
'* Input parameter: getcontent -- saved content
'* Input parameter: GetFile -- saved file name
Public Function saveeditfile (getpath, getcontent, GetFile)
If getcontent = "" Or GetFile = "" Then exit function
Set FSO = server. Createobject ("scripting. FileSystemObject ")
Set cf = FSO. createtextfile (server. mappath (getpath & GetFile), true)
Cf. Write getcontent
Cf. Close
Set cf = nothing
Set FSO = nothing
'Response. redirect request. servervariables ("http_referer ")
End Function

End Class


Related Article

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: 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.