zl程序教程

您现在的位置是:首页 >  其他

当前栏目

ASPFSO文件处理函数大全

文件 大全 处理函数
2023-06-13 09:13:45 时间

直接上代码

<%
"建立文件夹函数
FunctionCreateFolder(strFolder)"参数为相对路径
"首选判断要建立的文件夹是否已经存在
DimstrTestFolder,objFSO
strTestFolder=Server.Mappath(strFolder)
SetobjFSO=CreateObject("Scripting.FileSystemObject")
"检查文件夹是否存在
IfnotobjFSO.FolderExists(strTestFolder)Then
"如果不存在则建立文件夹
objFSO.CreateFolder(strTestFolder)
EndIf
SetobjFSO=Nothing
Endfunction

"删除文件夹
FunctionDelFolder(strFolder)"参数为相对路径
strTestFolder=Server.Mappath(strFolder)
SetobjFSO=CreateObject("Scripting.FileSystemObject")
"检查文件夹是否存在
IfobjFSO.FolderExists(strTestFolder)Then
objFSO.DeleteFolder(strTestFolder)
endif
SetobjFSO=Nothing
Endfunction

"创建文本文件
FunctionCreatetextfile(fileurl,filecontent)"参数为相对路径和要写入文件的内容
SetobjFSO=Server.CreateObject("Scripting.FileSystemObject")
Setfout=objFSO.CreateTextFile(Server.MapPath(fileurl))
fout.WriteLinefilecontent
fout.close
SetobjFSO=Nothing
EndFunction

"删除文件(适合所有文件)
FunctionDeltextfile(fileurl)"参数为相对路径
SetobjFSO=CreateObject("Scripting.FileSystemObject")
fileurl=Server.MapPath(fileurl)
ifobjFSO.FileExists(fileurl)then"检查文件是否存在
objFSO.DeleteFile(Server.mappath(fileurl))
endif
SetobjFSO=nothing
EndFunction

"建立图片文件并保存图片数据流
FunctionCreateimage(fileurl,imagecontent)"参数为相对路径和文件内容
SetobjStream=Server.CreateObject("ADODB.Stream")"建立ADODB.Stream对象,必须要ADO2.5以上版本
objStream.Type=1"以二进制模式打开
objStream.Open
objstream.writeimagecontent"将字符串内容写入缓冲
objstream.SaveToFileserver.mappath(fileurl),2"-将缓冲的内容写入文件
objstream.Close()"关闭对象
setobjstream=nothing
EndFunction

"远程获取文件数据
FunctiongetHTTPPage(url)
"OnErrorResumeNext
dimhttp
sethttp=Server.createobject("Microsoft.XMLHTTP")
Http.open"GET",url,false
Http.send()
ifHttp.readystate<>4then
exitfunction
endif
getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312")
sethttp=nothing
IfErr.number<>0then
getHTTPPage="服务器获取文件内容出错"
Err.Clear
EndIf
Endfunction

FunctionBytesToBstr(body,Cset)
dimobjstream
setobjstream=Server.CreateObject("adodb.stream")
objstream.Type=1
objstream.Mode=3
objstream.Open
objstream.Writebody
objstream.Position=0
objstream.Type=2
objstream.Charset=Cset
BytesToBstr=objstream.ReadText
objstream.Close
setobjstream=nothing
EndFunction

"获取图片数据流
Functiongetpic(url)
onerrorresumenext
dimhttp
sethttp=server.createobject("MSXML2.XMLHTTP")"使用xmlhttp的方法来获得图片的内容
Http.open"GET",url,false
Http.send()
ifHttp.readystate<>4then
exitfunction
endif
getpic=Http.responseBody
sethttp=nothing
iferr.number<>0then
getpic="服务器获取文件内容出错"
err.Clear
Endif
EndFunction

"打开文件(文本形式)
FunctionOpenFile(fileurl)"文件相对路径
DimFilename,fso,hndFile
Filename=fileurl
Filename=Server.MapPath(Filename)
Setobjfso=CreateObject("Scripting.FileSystemObject")
Ifobjfso.FileExists(Filename)Then
sethndFile=objfso.OpenTextFile(Filename)
OpenFile=hndFile.ReadAll
Else
OpenFile="文件读取错误"
EndIf
SethndFile=Nothing
Setobjfso=Nothing
EndFunction

"获得文件的后缀名
functiongetFileExtName(fileName)
dimpos
pos=instrrev(filename,".")
ifpos>0then
getFileExtName=mid(fileName,pos+1)
else
getFileExtName=""
endif
endfunction

Dimfso,f,folder
Setfso=Server.CreateObject("scripting.filesystemobject")
"改目录名
Setfolder=fso.getfolder(Server.Mappath("Old"))
folder.name="New""新名字
"改文件名
Setf=fso.getfile(Server.Mappath("Old.asp"))
f.name="New.asp""新名字
"释放


functioncopyfile(l1,l2)"复制文件
onerrorresumenext
dimfs
setfs=createobject("Scripting.FileSystemObject")
fs.copyfileserver.mappath(l1),server.mappath(l2)
setfs=nothing
iferr.number<>0thenerr.clear
endfunction
copyfile("db1.mdb","db2.mdb")

具体的大家可以搜索

FSO操作全集

下面的代码也适合vbs

<scriptlanguage=vbs>
onerrorresumenext
Setfso=CreateObject("Scripting.FileSystemObject")"使FSO组件可以被fso

变量调用
getfso=fso.DriveExists("g:\")"判断指定硬盘驱动器是否存在
getfso=fso.GetDrive("c:")"创建自定义的FSO驱动器对象
getfso=fso.GetDriveName("c:\网络程序员伴侣")"返回文件夹的所在盘符
Setgetfso=fso.Drives"创建FSO驱动器集合对象,多配合foreachiin
getfso语句进行穷尽操作,支持所有[驱动器对象属性],并具有Count和Item属性

Setfso=Nothing"释放fso变量与FSO组件的连接资源
</script>

驱动器对象操作

<scriptlanguage=vbs>
onerrorresumenext
Setfso=CreateObject("Scripting.FileSystemObject")"使FSO组件可以被fso变量调用
getfso=fso.GetDrive("c:").FileSystem"返回驱动器对象的文件系统类型
getfso=fso.GetDrive("c:").DriveType"返回驱动器对象的驱动器类型,返回值范围0-5
getfso=fso.GetDrive("c:").DriveLetter"返回驱动器对象的字母
getfso=fso.GetDrive("c:").IsReady"返回指定驱动器是否准备好或是否损坏
getfso=fso.GetDrive("c:").SerialNumber"返回驱动器对象的唯一十进制卷标序号
getfso=fso.GetDrive("c:").ShareName"返回驱动器对象的网络共享名
getfso=fso.GetDrive("c:").VolumeName"返回驱动器对象的卷标名,同时也可以设置其卷标名
getfso=fso.GetDrive("c:").Path"返回驱动器的实际路径,如c:\则返回c:
getfso=fso.GetDrive("c:").RootFolder"返回驱动器对象的跟文件夹
getfso=fso.GetDrive("c:").AvailableSpace"返回驱动器对象的可用容量大小
getfso=fso.GetDrive("c:").FreeSpace"返回驱动器对象的剩余空间大小
getfso=fso.GetDrive("c:").TotalSize"返回驱动器对象的总空间容量大小

Setfso=Nothing"释放fso变量与FSO组件的连接资源
</script>

系统文件夹操作

<scriptlanguage=vbs>
onerrorresumenext
Setfso=CreateObject("Scripting.FileSystemObject")"使FSO组件可以被fso变量调用
getfso=fso.GetFolder("c:\")"创建自定义的FSO文件夹对象
getfso=fso.FolderExists("e:\网络程序员伴侣")"判断指定文件夹是否存在
getfso=fso.GettempName"随机返回WINDOW产生在temp文件夹中的临时文件
getfso=fso.GetparentFolderName("e:\网络程序员伴侣")"返回指定文件夹的父文件夹
fso.CreateFolder"c:\windows\新创建的文件夹路径及名称"
fso.MoveFolder"c:\windows\许移动或改名的文件夹","c:\windows\移动路径和新的文件夹名"
fso.DeleteFolder"c:\windows\要删除的目录名",false/true
fso.CopyFolder"c:\windows\须复制的目录名","c:\windows\得到付值内容的目录名",false/true
Setgetfso=fso.Folders"创建FSO文件夹集合对象,多配合foreachiingetfso语句进行穷尽操作,支持所有[文件夹对象属性],并具有Count和Item属性,具有AddFolders方法
Setgetfso=fso.Files"创建FSO文件夹内文件集合对象,多配合foreachiingetfso语句进行穷尽操作,支持所有[文件对象属性],并具有Count和Item属性
Setfso=Nothing"释放fso变量与FSO组件的连接资源
</script>

文件夹对象操作

<scriptlanguage=vbs>
onerrorresumenext
Setfso=CreateObject("Scripting.FileSystemObject")"使FSO组件可以被fso变量调用
getfso=fso.GetFolder("c:\windows").Name"返回文件夹的名字,这里返回WINDOW
getfso=fso.GetFolder("c:\windows").ShortName"返回文件夹[8.3]格式的名称
getfso=fso.GetFolder("c:\windows").Attributes"返回文件夹的属性,可返回0,1,2,4,8,16,32,64,128
getfso=fso.GetFolder("c:\windows").Size"返回文件夹(包括子文件夹)内文件占用空间大小
getfso=fso.GetFolder("c:\windows").Type"返回文件夹类型信息
getfso=fso.GetFolder("c:\windows").Path"返回文件夹的真实路径
getfso=fso.GetFolder("c:\windows").ShortPath"返回文件夹[8.3]格式的路径
getfso=fso.GetFolder("c:\windows").Drive"返回文件夹所在驱动器
getfso=fso.GetFolder("c:\windows").Files.count"返回文件夹包含的所有文件对象集合
getfso=fso.GetFolder("c:\windows").SubFolders.count"返回文件夹包含的所有子文件夹的对象集合
getfso=fso.GetFolder("c:\windows").ParentFolder"返回文件夹的父文件夹对象
getfso=fso.GetFolder("c:\windows").IsRootFolder"返回文件夹是否为跟文件夹,是返回true否返回false
getfso=fso.GetFolder("c:\windows").DateCreated"返回文件夹的最初创建时间
getfso=fso.GetFolder("c:\windows").DateLastAccessed"返回文件夹最后一次访问时间
getfso=fso.GetFolder("c:\windows").DateLastModified"返回文件夹最近修改的时间
fso.GetFolder("c:\windows").CreateTextFile"新建的文件名及后缀",true/false,true/false
fso.GetFolder("c:\windows\要删除的目录名").Deletetrue
fso.GetFolder("c:\windows\要移动改名的目录名").Move"文件夹将要移动到的路径及自定义文件夹名称"
fso.GetFolder("c:\windows\旧目录").Copy"复制到路径",true/false"在指定路径付值此文件夹,true=覆盖,false相反
Setfso=Nothing"释放fso变量与FSO组件的连接资源
</script>

系统文件操作

<scriptlanguage=vbs>
onerrorresumenext
Setfso=CreateObject("Scripting.FileSystemObject")"使FSO组件可以被fso变量调用
getfso=fso.GetFile("c:\windows\help.html")"得到文件完整路径
getfso=fso.GetFileName("c:\windows\help.html")"得到文件名称和后缀
getfso=fso.GetExtensionName("c:\windows\help.html")"得到文件的文件类型(后缀),不含小数点
getfso=fso.FileExists("c:\windows\文件名.html")"判断文件是否存在
fso.MoveFile"要移动及改名的原文件路径","移动到某路径及自定义新文件名"
fso.DeleteFile"c:\windows\要删除的文件所在路径",false/true
fso.CopyFile"c:\windows\须复制的旧文件.txt","c:\windows\复制后的文件名.jpg",false/true
fso.OpenTextFile"c:\windows\desktop\要打开操作的文件名称.txt",1/2/8,true/false,0/-1/-2"ForReading=1=只读方式,ForWriting=2=可写方式,ForAppending=8=追加方式.true=如果打开的文件不存在则创建该文件.TristateFalse=0=以系统默认方式打开,TristateTrue=-1=以Unicode格式打开文件,TristateUseDefaule=-2=以ASCLL格式打开文件(缺剩值)
fso.CreateTextFile"c:\windows\desktop\新建的文件名称.txt",false/true,false/true"前者true覆盖以存在同名文件,后者为true新文件将以Unicode方式创建,反之=结果相反
Setfso=Nothing"释放fso变量与FSO组件的连接资源
</script>

文件对象操作

<scriptlanguage=vbs>
onerrorresumenext
Setfso=CreateObject("Scripting.FileSystemObject")"使FSO组件可以被fso变量调用
getfso=fso.GetFile("c:\windows\笔记本.txt").Name"返回文件对象的名称包含后缀
getfso=fso.GetFile("c:\windows\笔记本.txt").ShortName"返回文件对象的[8.3]名称包含后缀
getfso=fso.GetFile("c:\windows\笔记本.txt").Attributes"返回文件对象的属性,可返回0,1,2,4,8,16,32,64,128
getfso=fso.GetFile("c:\windows\笔记本.txt").Size"返回文件对象的大小
getfso=fso.GetFile("c:\windows\笔记本.txt").Type"返回文件对象的类型
getfso=fso.GetFile("c:\windows\笔记本.txt").Path"返回文件对象的完整路径
getfso=fso.GetFile("c:\windows\笔记本.txt").ShortPath"返回文件对象的[8.3]的完整路径
getfso=fso.GetFile("c:\windows\笔记本.txt").Drive"返回此文件对象所在的驱动器
getfso=fso.GetFile("c:\windows\笔记本.txt").ParentFolder"返回文件对象所在的文件夹
getfso=fso.GetFile("c:\windows\笔记本.txt").DateCreated"返回文件对象的创建日期
getfso=fso.GetFile("c:\windows\笔记本.txt").DateLastAccessed"返回文件对象的最后访问日期
getfso=fso.GetFile("c:\windows\笔记本.txt").DateLastModified"返回文件对象的最后修改时间
fso.GetFile("c:\windows\笔记本.txt").Copy"复制到的路径和文件名",true/false
fso.GetFile("c:\windows\笔记本.txt").Deletetrue
fso.GetFile("c:\windows\笔记本.txt").Move"移动到的路径及自定义文件名
Setfso=Nothing"释放fso变量与FSO组件的连接资源
</script>

文件对象读写操作

<scriptlanguage=vbs>
onerrorresumenext
Setfso=CreateObject("Scripting.FileSystemObject")"使FSO组件可以被fso变量调用
setlink1=fso.GetFile("c:\windows\笔记本.txt").OpenAsTextStream(1/2/8,0/-1/-2)
open1=link1.AtendOfline"判断文件指针是否以在行的末尾
open2=link1.AtendOfstream"判断文件指针是否一再文件的末尾
open3=link1.Column"返回当前字符所在文件的列号
open4=link1.Line"返回当前字符所在文件的行号
open5=link1.Read(10)"读取文件中指定数量的字符
open6=link1.ReadAll"读取文件中所有的字符
open7=link1.ReadLine"读取文件中一行中含有的字符
open8=link1.Write"字符串""将自定字符串或字符串变量写入文件
open9=link1.WriteLine"字符""将自定字符+一个换行符写入文件
open10=link1.WriteBlankLines10"将指定数量的换行符写入文件
open11=link1.Skip10"使文件指针跳过指定数量的字符
open12=link1.SkipLine"使文件指针跳到下一行
link1.Close"文件操作完毕,销毁文件对象指针
Setfso=Nothing"释放fso变量与FSO组件的连接资源
</script>