Below is a detailed tutorial on operating the FSO file system that the editor has shared with you. Interested friends, let’s learn about it with the editor!
Implement functions:
The file (folder) directory list provides the search for files and folders below the directory.
File writing, creation, and deletion provide the operation of editing and deleting files (folders)
Creating folders/files is set for creating folders (files).
Upload files You can simulate FTP uploads, with unlimited file size and type.
If you are interested, you will not bear any consequences if you have any problems. Here I say that I don’t have much time to surf the Internet and often can’t take care of it. I saw that people often ask this question recently, so I posted it, hoping it would be helpful.
upfso.asp//Control uploaded files
The code copy is as follows:
@importurl("admin.css");
Server.ScriptTimeOut=999
'up_filetype="RAR,ZIP,SWF,JPG,PNG,GIF,DOC,TXT,CHM,PDF,ACE,JPG,MP3,WMA,WMV,bmp"
IFRequest.QueryString("yes")="upload"Then
path=Trim(request("path"))
'response.write(path&"---")
'response.End
DimFSO, FSOIsOK, F_FileName, mode
F_FileName=Trim(request("nn"))
mode=killint(Trim(request("mode")),0,0,2)
FSOIsOK=1
SetFSO=Server.CreateObject("Scripting.FileSystemObject")
IfErr0Then
Err.Clear
FSOIsOK=0
EndIf
DimD_Name,F_Name
IfFSOIsOK=1Then
IfInStr(1,path,":/")=0Then
path=Replace(Lcase(path),"/","/")
path=server.mappath(path)
path=Replace(path&"/","//","/")
Else
path=Replace(Lcase(path),"/","/")
path=Replace(path&"/","//","/")
EndIf
ifnotfso.folderexists(path)Then
response.write "Basic path search failed, return" ='#000080'>
response.End
EndIf
EndIf
SetFSO=Nothing
DimFileUP
SetFileUP=NewUpload_File
FileUP.GetDate(-1)
DimF_FileType,F_File
SetF_File=FileUP.File("File")
IfLen(F_FileName)
IfLen(F_FileName)
response.write("Empty file, please return") ='#000080'> ='javascript:history.go(-1);'>
response.End
EndIf
'F_FileType=Ucase(F_File.FileExt)
'IFF_File.FileSize>90000Then
'Response.Write("Size exceeds limit") ='javascript:history.go(-1);'>
'exitsub
IFIsvalidFileName(F_FileName)=FalseThen
Response.Write("Incorrect name") ='#000080'> ='javascript:history.go(-1);'>
Else
DimFileIsExists
SetFSO=Server.CreateObject("Scripting.FileSystemObject")
FileIsExists=FSO.FileExists(path&F_FileName)
IfFileIsExists=TrueAndmode1Then
fso.deletefile(path&F_FileName)
Response.Write("The file already exists, has been deleted;") ='#000080'>
F_File.SaveToFilepath&F_FileName
Response.Write(" Click here to continue uploading:"&path&F_FileName&" ='#000080'> ")
ElseIfFileIsExists=TrueAndmode=1Then
Response.Write("The file already exists, you chose not to overwrite") ='#000080'>
Else
F_File.SaveToFilepath&F_FileName
Response.Write(" Click here to continue uploading:"&path&F_FileName&" ='#000080'> ")
EndIf
EndIF
SetF_File=Nothing
SetFileUP=Nothing
Else
Dimpath,nn,mmode
nn=Trim(request("nn"))
mmode=Trim(request("mode"))
path=Replace(request("path"),"//","/")
Ifpath=""Thenpath="../newup/"
Response.Write(" ")
EndIF
'Experience name
FunctionIsvalidFileName(File_Name)
IsvalidFileName=False
Dimre,reStr
Setre=newRegExp
re.IgnoreCase=True
re.Global=True
re.Pattern="[^_/.a-zA-Z/d]"
reStr=re.Replace(File_Name,"")
IfFile_Name=reStrThenIsvalidFileName=True
Setre=Nothing
EndFunction
%>
upload.asp//Upload class
The code copy is as follows:
DimoUpFileStream
ClassUpload_File
DimForm, File, Err
PrivateSubClass_Initialize
Err=-1
EndSub
PrivateSubClass_Terminate
'ClearVariables&Objects
IfErr
oUpFileStream.Close
Form.RemoveAll
File.RemoveAll
SetForm=Nothing
SetFile=Nothing
SetoUpFileStream=Nothing
EndIf
EndSub
PublicSubGetDate(RetSize)
'DefineVariables
DimRequestBinDate,sStart,bCrLf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,oFileInfo
DimiFileSize, sFilePath, sFileType, sFormvalue, sFileName
DimiFindStart,iFindEnd
DimiFormStart,iFormEnd,sFormName
IfRequest.TotalBytes
Err=1
ExitSub
EndIf
IfRetSize>0Then
IfRequest.TotalBytes>RetSizeThen
Err=2
ExitSub
EndIf
EndIf
SetForm=Server.CreateObject("Scripting.Dictionary")
Form.CompareMode=1
SetFile=Server.CreateObject("Scripting.Dictionary")
File.CompareMode=1
SettStream=Server.CreateObject("Adodb.Stream")
SetoUpFileStream=Server.CreateObject("Adodb.Stream")
oUpFileStream.Type=1
oUpFileStream.Mode=3
oUpFileStream.Open
oUpFileStream.WriteRequest.BinaryRead(Request.TotalBytes)
oUpFileStream.Position=0
RequestBinDate=oUpFileStream.Read
iFormEnd=oUpFileStream.Size
bCrLf=chrB(13)&chrB(10)
'GetSeperators
sStart=MidB(RequestBinDate,1,InStrB(1,RequestBinDate,bCrLf)-1)
iStart=LenB(sStart)
iFormStart=iStart+2
'SplitItems
Do
iInfoEnd=InStrB(iFormStart,RequestBinDate,bCrLf&bCrLf)+3
tStream.Type=1
tStream.Mode=3
tStream.Open
oUpFileStream.Position=iFormStart
oUpFileStream.CopyTotStream,iInfoEnd-iFormStart
tStream.Position=0
tStream.Type=2
tStream.Charset="UTF-8"
sInfo=tStream.ReadText
'Getformitemname
iFormStart=InStrB(iInfoEnd,RequestBinDate,sStart)-1
iFindStart=InStr(22,sInfo,"name=""",1)+6
iFindEnd=InStr(iFindStart,sInfo,"""",1)
sFormName=Mid(sinfo,iFindStart,iFindEnd-iFindStart)
'Ifit'safile
IfInStr(45,sInfo,"filename=""",1)>0Then
SetoFileInfo=newFileInfo
'GetFileattributes
iFindStart=InStr(iFindEnd,sInfo,"filename=""",1)+10
iFindEnd=InStr(iFindStart,sInfo,"""",1)
sFileName=Mid(sinfo,iFindStart,iFindEnd-iFindStart)
oFileInfo.FileName=Mid(sFileName,InStrRev(sFileName,"/")+1)
oFileInfo.FilePath=Left(sFileName,InStrRev(sFileName,"/"))
oFileInfo.FileExt=Mid(sFileName,InStrRev(sFileName,".")+1)
iFindStart=InStr(iFindEnd,sInfo,"Content-Type:",1)+14
iFindEnd=InStr(iFindStart,sInfo,vbCr)
oFileInfo.FileType=Mid(sinfo,iFindStart,iFindEnd-iFindStart)
oFileInfo.FileStart=iInfoEnd
oFileInfo.FileSize=iFormStart-iInfoEnd-2
oFileInfo.FormName=sFormName
file.addsFormName,oFileInfo
Else
'Ifit'sformitem
tStream.Close
tStream.Type=1
tStream.Mode=3
tStream.Open
oUpFileStream.Position=iInfoEnd
oUpFileStream.CopyTotStream,iFormStart-iInfoEnd-2
tStream.Position=0
tStream.Type=2
tStream.Charset="UTF-8"
sFormvalue=tStream.ReadText
IfForm.Exists(sFormName)Then
Form(sFormName)=Form(sFormName)&","&sFormValue
Else
Form.AddsFormName,sFormvalue
EndIf
EndIf
tStream.Close
iFormStart=iFormStart+iStart+2
'Exitatendoffile
LoopUntil(iFormStart+2)=iFormEnd
RequestBinDate=""
SettStream=Nothing
EndSub
EndClass
'GetFileInfo
ClassFileInfo
DimFormName, FileName, FilePath, FileSize, FileType, FileStart, FileExt
PrivateSubClass_Initialize
FileName=""
FilePath=""
FileSize=0
FileStart=0
FormName=""
FileType=""
FileExt=""
EndSub
'SaveFileMethod
PublicFunctionSaveToFile(FullPath)
DimoFileStream,ErrorChar,i
OnErrorResumeNext
SetoFileStream=CreateObject("Adodb.Stream")
oFileStream.Type=1
oFileStream.Mode=3
oFileStream.Open
oUpFileStream.position=FileStart
oUpFileStream.copytooFileStream,FileSize
oFileStream.SaveToFileFullPath,2
oFileStream.Close
SetoFileStream=Nothing
EndFunction
'GetFileContent
PublicFunctionGetDate
oUpFileStream.Position=FileStart
GetDate=oUpFileStream.Read(FileSize)
EndFunction
EndClass
%>
Core Functions
The code copy is as follows:
DimtheInstalledObjects(17)
theInstalledObjects(0)="MSWC.AdRotator"
theInstalledObjects(1)="MSWC.BrowserType"
theInstalledObjects(2)="MSWC.NextLink"
theInstalledObjects(3)="MSWC.Tools"
theInstalledObjects(4)="MSWC.Status"
theInstalledObjects(5)="MSWC.Counters"
theInstalledObjects(6)="IISSample.ContentRotator"
theInstalledObjects(7)="IISSample.PageCounter"
theInstalledObjects(8)="MSWC.PermissionChecker"
theInstalledObjects(9)="Scripting.FileSystemObject"
theInstalledObjects(10)="adodb.connection"
theInstalledObjects(11)="SoftArtisans.FileUp"
theInstalledObjects(12)="SoftArtisans.FileManager"
theInstalledObjects(13)="JMail.SMTPMail"
theInstalledObjects(14)="CDONTS.NewMail"
theInstalledObjects(15)="Persits.MailSender"
theInstalledObjects(16)="LyfUpload.UploadFile"
theInstalledObjects(17)="Persits.Upload.1"
Dimfso
IfIsObjInstalled(theInstalledObjects(9))Then
Setfso=Server.CreateObject("Scripting.FileSystemObject")
EndIf
FunctionIsObjInstalled(strClassString)
OnErrorResumeNext
IsObjInstalled=False
Err=0
DimxTestObj
SetxTestObj=Server.CreateObject(strClassString)
If0=ErrThenIsObjInstalled=True
SetxTestObj=Nothing
Err=0
EndFunction
'Check component version
PublicFunctiongetver(Classstr)
OnErrorResumeNext
DimxTestObj
SetxTestObj=Server.CreateObject(Classstr)
IfErrThen
getver=""
else
getver=xTestObj.version
endif
SetxTestObj=Nothing
EndFunction
'Experience name
FunctionIsvalidFileName(File_Name)
IsvalidFileName=False
Dimre,reStr
Setre=newRegExp
re.IgnoreCase=True
re.Global=True
re.Pattern="[^_/.a-zA-Z/d]"
reStr=re.Replace(File_Name,"")
IfFile_Name=reStrThenIsvalidFileName=True
Setre=Nothing
EndFunction
'File writing
Functionwriteto(xmlfloder,xmlfile,content,mode)
writeto=false
IfNotIsObjInstalled(theInstalledObjects(9))ThenExitFunction
mode=killint(mode,0,0,2)
xmlfloder=server.mappath(xmlfloder)
Setfso=Server.CreateObject("Scripting.FileSystemObject")
ifnotfso.folderexists(xmlfloder)Then
fso.createfolder(xmlfloder)
EndIf
xmlfile=replace(xmlfloder&"/","//","/")&xmlfile
'response.write(warn_red(xmlfile))
Dimfsoxml
Ifffso.fileexists(xmlfile)Andmode=1Then' does not write
ExitFunction
elseIfffso.fileexists(xmlfile)Andmode=2Then'Rewrite
Setfsoxml=fso.opentextfile(xmlfile,2)
fsoxml.writeline(content)
fsoxml.close
writeto=true
ElseIfffso.fileexists(xmlfile)Andmode=8Then'Add
Setfsoxml=fso.opentextfile(xmlfile,8)
fsoxml.writeline(content)
fsoxml.close
writeto=true
ElseIfffso.fileexists(xmlfile)Then
Setfsoxml=fso.opentextfile(xmlfile,2)'Rewrite
fsoxml.writeline(content)
fsoxml.close
writeto=true
Else
Setfsoxml=fso.createtextfile(xmlfile)'Create
fsoxml.writeline(content)
fsoxml.close
writeto=true
EndIf
EndFunction
'Delete the file
Functiondelaspfile(x)
OnErrorResumeNext
delaspfile=False
IfNotfileexitornot(x)Then
ExitFunction
Else
fso.deletefileserver.mappath(x)
delaspfile=True
Endif
EndFunction
'The file exists
Functionfileexitornot(file)
OnErrorResumeNext
Dimf_re_file
f_re_file=true
Ifnotfso.fileexists(server.MapPath(file))Thenf_re_file=False
Iferr0Thenf_re_file=False
fileexitornot=f_re_file
EndFunction
'Error suppression, print error
Functionshow_err(err)
OnErrorResumeNext
Iferr.Number0Then
Response.Clear
Dimerr_mess
err_mess=" Error occurred:
Error Number: "&err.Number&"
Error message: "&err.Description&"
Error file: "&err.Source&"
Error line: "&err.Line&" (not supported)
"&err
response.write(err_mess)
Endif
EndFunction
'warn:
Functionwarn_red(mess)
warn_red=" Track:"&mess&"
" =red>
EndFunction
'FSO file directory
Functionshowallfile(path)
'OnErrorResumeNext
path=Replace(path,"//","/")
setfso=CreateObject("Scripting.FileSystemObject")
DimuploadPath, uploadfolder, objSubFolders, allfiles, fileitem, objSubFolder,
sFileName
IfInStr(1,path,":/")=0Then
path=Replace(path,"/","/")
uploadPath=server.mappath(path)
Else
path=Replace(path,"/","/")
uploadPath=path
EndIf
response.write(warn_red(uploadPath))
ifnotfso.folderexists(uploadPath)Then
response.writewarn_red("Path lookup failed")
ExitFunction
EndIf
Setuploadfolder=fso.GetFolder(uploadPath)
Ifuploadfolder.isrootfolderThen
response.write(" root directory
")
Else
response.write(" parent directory:
"&uploadfolder.parentfolder&"
")
EndIf
response.write(" Directory size:"&int(uploadfolder.size/1024)&"KB
")
setobjSubFolders=uploadfolder.Subfolders
Dimfso_mes
fso_mes="
foreachobjSubFolderinobjSubFolders
fso_mes=fso_mes&"
next
setallfiles=uploadfolder.Files
foreachfileiteminallfiles
fso_mes=fso_mes&"
Next
fso_mes=fso_mes&"
"
response.write(fso_mes)
response.writedeltext(uploadPath,1)
EndFunction
'File Properties
Functionfilepro(name)
name=Replace(name,"//","/")
Dim whichfile
IfInStr(1,name,":/")=0Then
name=Replace(name,"/","/")
whichfile=server.mappath(name)
Else
name=Replace(name,"/","/")
whichfile=name
EndIf
Setfso=CreateObject("Scripting.FileSystemObject")
IfNotfso.fileexists(whichfile)Then
response.write(warn_red("The file does not exist or has no access permission"))
ExitFunction
EndIf
Dimf2,s_mess
Setf2=fso.GetFile(whichfile)
s_mess=" parent directory:"&f2.parentfolder&
"
"
s_mess=s_mess&"File name: "&f2.name&"
"
s_mess=s_mess&"File short path name: "&f2.shortPath&"
"
s_mess=s_mess&"File physical address: "&f2.Path&"
"
s_mess=s_mess&"File attribute: "&f2.Attributes&"
"
s_mess=s_mess&"File size:"&f2.size&"
"
s_mess=s_mess&"File type: "&f2.type&"
"
s_mess=s_mess&"File creation time: "&f2.DateCreated&"
"
s_mess=s_mess&"Latest access time:"&f2.DateLastAccessed&"
"
s_mess=s_mess&"Latest modified time:"&f2.DateLastModified&"
"
response.write(s_mess)
Ifkillint(Trim(request("type")),0,0,2)0Then
showtext(whichfile)
EndIf
response.writedeltext(whichfile,0)
EndFunction
'
SUBshowtext(files)
dimiStr,adosText,strap
setadosText=Server.CreateObject("ADODB.Stream")
adosText.mode=3
adosText.type=2
adosText.charset="gb2312"
'adosText.charset="big5"
adosText.open
IfInStr(1,files,":/")=0Then
files=Replace(files,"/","/")
files=server.mappath(files)
Else
files=Replace(files,"/","/")
files=files
EndIf
adosText.loadFromFile(files)
strasp=adosText.ReadText()
adosText.close
setadosText=nothing%>
Functiondeltext(file,mode)
Dimdeltext_mess
deltext_mess=""
SelectCasekillint(mode,0,0,2)
Case0:
deltext_mess=deltext_mess&"File Operation: Properties Edit Mobile Copy Rename Delete " =red> =red>
Case1:
deltext_mess=deltext_mess&"Folder operation: list creation directory manual file upload file move copy rename and delete " =red>
EndSelect
deltext_mess=deltext_mess&""
deltext=deltext_mess
EndFunction
The above is a detailed tutorial on FSO operating the file system. I believe you have learned it. For more related content, please continue to pay attention to the Wuxin Technology Channel.