Asp fso file operation function code (copy a file, rename a file, delete a file, and replace a string)

Source: Internet
Author: User

FSO File object attributes
DateCreated returns the creation date and time of the folder.
DateLastAccessed returns the date and time when the last file was accessed.
DateLastModified returns the date and time when the last file was modified.
Drive returns the Drive object of the Drive where the file is located.
Name: Specifies the Name of the returned object.
ParentFolder returns the Folder object of the parent Folder of the file.
Path: returns the absolute Path of the file. You can use a long file name.
ShortName returns the DOS-style 8.3 format file name.
Restore path returns the absolute path of a DOS-style 8.3 file.
Size returns the Size (in bytes) of the file)
If possible, a description string of the file Type is returned.
FSO File object Method
Usage of the FSO object Method
CopyFile: copy one or more files to the new path.
CreateTextFile: Creates a file and returns a TextStream object.
DeleteFile: delete an object
OpenTextFile open the file and return the TextStream object for reading or appending

Rename a file:Copy codeThe Code is 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

Delete an object:Copy codeThe Code is 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

Replace the string in the file:Copy codeThe Code is 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)
'Usage: create a directory
'Parameter: sPath: relative directory path created
'Return value: true for success, false for failure
'*************************************** ****************
'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)
'Usage: create a file
'Parameter: paramfilecontent' File Content
'Paramfilepath' file name (excluding the path)
'Return value: true for success, false for failure
'*************************************** ****************
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)
'For use: delete an object
'Parameter: filepath' multiple files are separated by' |'
'Return value: true for success, false for failure
'*************************************** ****************
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)
'For use: delete a directory
'Parameter: folderpath' directory path' multiple directories are separated by "|"
'Return value: true for success, false for failure
'*************************************** ****************
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 "failed to delete directory ",""
Else
DelFolder = true
End If
End Function

'*************************************** ****************
'Function name: IsExistFile (FilePath)
'Usage: Determine whether a file or directory exists
'Parameter: filepath' multiple files are separated by' |'
'Return value: true for success, false for failure
'*************************************** ****************
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 "failed to judge whether a file or directory exists ",""
Else
IsExistFile = true
End If
End Function

'*************************************** ****************
'Function name: DelFile (FilePath)
'For use: delete files or directories
'Parameter: filepath' multiple files are separated by' |'
'Return value: true for success, false for failure
'*************************************** ****************
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 "failed to delete the file or directory ",""
Else
DelFile = true
End If
End Function

'*************************************** ****************
'Function name: ReNameFile (oldName, newName)
'For use: rename a file or directory
'Parameter: stroldname' multiple original file names are separated by "|"
'Strnewname' multiple new file names are separated by "|"
'The above two parameters must be consistent
'Return value: true for success, false for failure
'*************************************** ****************
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)
'Usage: copying files or directories
'Parameter: tempsource' multiple source file names are separated by "|"
Multiple 'tempend' object names are separated by "|"
'Note: the preceding two parameters must be consistent and all are complete paths,
'Has been processed by the Server. MapPath Method
'Return value: true for success, false for failure
'*************************************** ****************
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 "copying files or directories", "condition is blank"
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.