ASP FSO file Operation function code (copy file, rename file, delete file, replace string) _ Application Tips

Source: Internet
Author: User
FSO file (file) object properties
DateCreated returns the date and time the folder was created
Datelastaccessed returns the date and time the file was last accessed
DateLastModified returns the date and time the file was last modified
Drive returns the Drive object of the drive where the file resides
Name Sets or returns the names of the files
ParentFolder returns the Folder object for the parent of the file
Path returns the absolute path to the file, using long file names
ShortName Returns a DOS-style 8.3-form filename
ShortPath returns a DOS-style 8.3-form file absolute path
Size returns the file in bytes
Type returns a description string for a file type, if possible
FSO file (file) object method
FSO File Object Method purpose
CopyFile copy one or more files to a new path
CreateTextFile creates a file and returns a TextStream object
DeleteFile Delete a file
OpenTextFile opens the file and returns the TextStream object to read or append

To rename a file:
Copy Code code as follows:

Function ReName (Sourcename,destname)
Dim ofso,ofile
Set Ofso=server.createobject ("Scripting.FileSystemObject")
Set Ofile=ofso.getfile (Server.MapPath (sourceName))
Ofile.name=destname
Set ofso=nothing
Set ofile=nothing
End Function

To delete a file:
Copy Code code as follows:

Function Fsodel (FileName)
Dim fso,f
Set fso = server. CreateObject ("Scripting.FileSystemObject")
F=server. MapPath (FileName)
If FSO. FileExists (f) Then
Fso. DeleteFile F,true
End If
Set F = Nothing
Set fso = Nothing
End Function

To replace a string in a file:
Copy Code code as follows:

Function Fsoreplace (filename,target,repstring)
Dim Objfso,objcountfile,filetempdata
Set objFSO = Server.CreateObject ("Scripting.FileSystemObject")
Set objcountfile = objFSO.OpenTextFile (Server.MapPath (fileName), 1,true)
Filetempdata = Objcountfile.readall
Objcountfile.close
Filetempdata=replace (filetempdata,target,repstring)
Set Objcountfile=objfso.createtextfile (Server.MapPath (fileName), True)
Objcountfile.write Filetempdata
Objcountfile.close
Set objcountfile=nothing
Set objFSO = Nothing
End Function

<%

'*******************************************************
' Function name: CreateFolder (spath)
' Function: Create a table of contents
' Parameter: spath: Created relative directory path
' Return value: Success true, failure false
'*******************************************************
' Response. Write CreateFolder ("/dgsunshine/uploadfile/demo1/")
Function CreateFolder (spath)
On Error Resume Next

Dim Fso,arrfolder,folder,i,j

If spath= "" Then
CreateFolder = False
Exit Function
End If

If Left (spath,1) = "/" Then
folder = "/"
spath = Mid (Spath,2,len (spath))
Else
folder = "./"
End If

If Right (spath,1) = "/" Then spath = Left (Spath,len (spath)-1)

Arrfolder = Split (spath, "/")

Set Fso = Server.CreateObject ("Scripting.FileSystemObject")

For i = 0 to Ubound (arrfolder)
If i = 0 Then
folder = folder & Arrfolder (i) & "/"
Else
folder = folder & Arrfolder (i) & "/"
End If

If fso.folderexists (Server.MapPath (Folder) = False Then
Response. Write server. MapPath (folder)
Fso.createfolder (Server.MapPath (Folder))
End If

Next
Set Fso = Nothing

If Err.Number <> 0 Then
Err.Clear ()
CreateFolder = False
Else
CreateFolder = True
End If
End Function


Function GetFile (Paramfilepath)
Set Fso = Server.CreateObject ("Scripting.FileSystemObject")
Set Fso_read = Fso. OpenTextFile (Server.MapPath (Paramfilepath), 1,false,-2)
GetFile = Fso_read.readall
Set Fso_read = Nothing
Set Fso = Nothing
End Function

'*******************************************************
' Function name: CreateFile (Paramfilecontent,paramfilepath)
' Function: Create a file
' Parameters: Paramfilecontent ' contents of File
' Paramfilepath ' filename (excluding path)
' Return value: Success true, failure false
'*******************************************************
Function CreateFile (Paramfilecontent,paramfilepath)
On Error Resume Next
Dim Fso,fwrite

Set Fso = Server.CreateObject ("Scripting.FileSystemObject")
Set fwrite = Fso.createtextfile (Server.MapPath (Paramfilepath), true)

Fwrite.write paramfilecontent
Fwrite.close ()
Set fwrite = Nothing
Set Fso = Nothing

If err.number <> 0 Then
Err.Clear ()
CreateFile = False
Else
CreateFile = True
End If
End Function

'*******************************************************
' Function name: Delfile (FilePath)
' Function: Delete file
' Parameters: FilePath ' file path multiple files with ' | ' Separated
' Return value: Success true, failure false
'*******************************************************
Function Delfile (FilePath)
On Error Resume Next
Dim Fso,arrfile,i

If getsafestr (FilePath, "") = "" Then
CreateFolder = False
Exit Function
End If

Arrfile = Split (FilePath, "|")
Set Fso = Server.CreateObject ("Scripting.FileSystemObject")

For i=0 to UBound (arrfile)
FilePath = Arrfile (i)
If fso.fileexists (Server.MapPath (FilePath)) Then
Fso.deletefile Server.MapPath (FilePath)
End If
Next
Set FSO = Nothing

If ERR Then
Err.Clear ()
Delfile = False
Else
Delfile = True
End If
End Function

'*******************************************************
' Function name: Delfolder (FolderPath)
' Function: Delete directory
' Parameters: FolderPath ' directory path ' multiple directories with ' | ' Separated
' Return value: Success true, failure false
'*******************************************************
Function Delfolder (FolderPath)
On Error Resume Next
Dim Fso,arrfolder,i

If getsafestr (FolderPath, "") = "" Then
Delfolder = False
Exit Function
End If

Arrfolder = Split (FolderPath, "|")
Set Fso = Server.CreateObject ("Scripting.FileSystemObject")

For i=0 to UBound (Arrfolder)
FolderPath = Arrfolder (i)
If fso.folderexists (Server.MapPath (folderpath)) Then
Fso.deletefolder Server.MapPath (FolderPath)
End If
Next

If ERR Then
Err.Clear ()
Delfolder = False
' ShowError Delete directory failed ', ' '
Else
Delfolder = True
End If
End Function


'*******************************************************
' Function name: Isexistfile (FilePath)
' function: To determine whether a file or directory exists
' Parameters: FilePath ' file path multiple files with ' | ' Separated
' Return value: Success true, failure false
'*******************************************************
Function Isexistfile (FilePath)
On Error Resume Next
Dim Fso,arrfile,i

If getsafestr (FilePath, "") = "" Then
Isexistfile = False
End If

Arrfile = Split (FilePath, "|")
Set Fso = Server.CreateObject ("Scripting.FileSystemObject")

For i=0 to UBound (arrfile)
FilePath = Arrfile (i)
If fso.fileexists (Server.MapPath (FilePath)) Then
Isexistfile = True
End If
If fso.folderexists (Server.MapPath (FilePath)) Then
Isexistfile = True
End If
Next
Set FSO = Nothing

If ERR Then
Err.Clear ()
Isexistfile = False
' ShowError ' determines if a file or directory has failed.
Else
Isexistfile = True
End If
End Function


'*******************************************************
' Function name: Delfile (FilePath)
' Function: Delete file or directory
' Parameters: FilePath ' file path multiple files with ' | ' Separated
' Return value: Success true, failure false
'*******************************************************
Function Delfile (FilePath)
On Error Resume Next
Dim Fso,arrfile,i

If getsafestr (FilePath, "") = "" Then
CreateFolder = False
End If

Arrfile = Split (FilePath, "|")
Set Fso = Server.CreateObject ("Scripting.FileSystemObject")

For i=0 to UBound (arrfile)
FilePath = Arrfile (i)
If fso.fileexists (Server.MapPath (FilePath)) Then
Fso.deletefile Server.MapPath (FilePath)
End If
If fso.folderexists (Server.MapPath (FilePath)) Then
Fso.deletefolder Server.MapPath (FilePath)
End If
Next
Set FSO = Nothing

If ERR Then
Err.Clear ()
Delfile = False
' ShowError Delete file or directory failed ', '
Else
Delfile = True
End If
End Function


'*******************************************************
' Function name: RenameFile ((Oldname,newname)
' Function: Renaming a file or directory
' Parameter: Stroldname ' original filename multiple with ' | ' Separated
' strNewName ' new filename multiple with ' | ' Separated
' Above two parameters please keep consistent
' Return value: Success true, failure false
'*******************************************************
Function RenameFile (Stroldname,strnewname)
On Error Resume Next
Dim Fso,arrold,arrnew,i,oldname,newname

Old = Getsafestr (Stroldname, "")
Newfile = Getsafestr (strNewName, "")

If old = "" or Newfile = "" Then
RenameFile = False
Exit Function
End If

Arrold = Split (stroldname, "|")
Arrnew = Split (strNewName, "|")

If UBound (arrold) <> UBound (arrnew) Then
RenameFile = False
Exit Function
End If

Set Fso = Server.CreateObject ("Scripting.FileSystemObject")
For i=0 to UBound (arrold)
Oldname = Server.MapPath (Arrold (i))
NewName = Server.MapPath (Arrnew (i))
If fso.fileexists (oldname) and not fso.fileexists (NewName) Then
Fso. MoveFile Oldname,newname
' RenameFile = True
End If
Next
Set FSO = Nothing

If err.number <> 0 Then
Err.Clear ()
RenameFile = False
Else
RenameFile = True
End If
End Function


'*******************************************************
' Function name: CopyFiles ((tempsource,tempend)
' Function: Copy files or directories
' Parameter: Tempsource ' source filename multiple with ' | ' Separated
' tempend ' destination filename multiple with ' | ' Separated
' Note: The above two parameters are consistent and are full paths,
' has been processed by the Server.MapPath method.
' Return value: Success true, failure false
'*******************************************************
Function CopyFiles (Tempsource,tempend)
On Error Resume Next
Dim Copyfso,arrsource,arrend

CopyFiles = False
Set Copyfso = Server.CreateObject ("Scripting.FileSystemObject")

If Tempsource = "" or Tempend = "" Then
Errraise "Copy file or directory", "Null condition"
CopyFiles = False
Exit Function
End If

Arrsource = Split (Tempsource, "|")
Arrend = Split (tempend, "|")
If UBound (Arrsource) <> UBound (arrend) Then
copyfiles= false
Exit Function
End If

For i=0 to UBound (Arrsource)
SrcName = Arrsource (i)
Tarname = Arrend (i)
IF copyfso.fileexists (SrcName) and not copyfso.fileexists (Tarname) Then
Copyfso.copyfile Srcname,tarname
CopyFiles = True
End If

IF copyfso.folderexists (SrcName) and not copyfso.folderexists (Tarname) Then
Copyfso.copyfolder Srcname,tarname
CopyFiles = True
End If
Next
Set Copyfso = Nothing

If ERR Then
' Err.Clear ()
CopyFiles = False
End If
End Function
%>
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: 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.