ASP FSO文件处理函数大全

代码
<
'建立文件夹函数 
Function CreateFolder(strFolder)'参数为相对路径 
    '首选判断要建立的文件夹是否已经存在 
    Dim strTestFolder,objFSO 
    strTestFolder 
= Server.Mappath(strFolder) 
    
Set objFSO = CreateObject("Scripting.FileSystemObject"
    
'检查文件夹是否存在 
    If not objFSO.FolderExists(strTestFolder) Then 
  
'如果不存在则建立文件夹 
  objFSO.CreateFolder(strTestFolder) 
    
End If 
 
Set objFSO = Nothing 
End function 
'删除文件夹 
Function DelFolder(strFolder)'参数为相对路径 
 strTestFolder = Server.Mappath(strFolder) 
 
Set objFSO = CreateObject("Scripting.FileSystemObject"
 
'检查文件夹是否存在 
 If objFSO.FolderExists(strTestFolder) Then 
  objFSO.DeleteFolder(strTestFolder) 
 
end if 
 
Set objFSO = Nothing 
End function 
'创建文本文件 
Function Createtextfile(fileurl,filecontent)'参数为相对路径和要写入文件的内容 
 Set objFSO = Server.CreateObject("Scripting.FileSystemObject"
 
Set fout = objFSO.CreateTextFile(Server.MapPath(fileurl)) 
 fout.WriteLine filecontent 
 fout.close 
 
Set objFSO = Nothing 
End Function 
'删除文件(适合所有文件) 
Function Deltextfile(fileurl)'参数为相对路径 
 Set objFSO = CreateObject("Scripting.FileSystemObject"
  fileurl 
= Server.MapPath(fileurl) 
  
if objFSO.FileExists(fileurl) then '检查文件是否存在 
   objFSO.DeleteFile(fileurl) 
  
end if 
 
Set objFSO = nothing 
End Function 
'建立图片文件并保存图片数据流 
Function Createimage(fileurl,imagecontent)'参数为相对路径和文件内容 
 Set objStream = Server.CreateObject("ADODB.Stream")   '建立ADODB.Stream对象,必须要ADO 2.5以上版本 
 objStream.Type =1   '以二进制模式打开 
 objStream.Open 
 objstream.write imagecontent   
'将字符串内容写入缓冲 
 objstream.SaveToFile server.mappath(fileurl),2   '-将缓冲的内容写入文件 
 objstream.Close()'关闭对象 
 set objstream=nothing 
End Function 
'远程获取文件数据 
Function getHTTPPage(url)  
 
'On Error Resume Next 
 dim http  
 
set http=Server.createobject("Microsoft.XMLHTTP")  
 Http.open 
"GET",url,false  
 Http.send()  
 
if Http.readystate<>4 then 
  
exit function  
 
end if  
 getHTTPPage
=bytesToBSTR(Http.responseBody,"GB2312"
 
set http=nothing 
 
If Err.number<>0 then  
  getHTTPPage 
= "服务器获取文件内容出错"  
  Err.Clear 
 
End If   
End function 
Function BytesToBstr(body,Cset) 
 
dim objstream 
 
set objstream = Server.CreateObject("adodb.stream"
 objstream.Type 
= 1 
 objstream.Mode 
=3 
 objstream.Open 
 objstream.Write body 
 objstream.Position 
= 0 
 objstream.Type 
= 2 
 objstream.Charset 
= Cset 
 BytesToBstr 
= objstream.ReadText  
 objstream.Close 
 
set objstream = nothing 
End Function 
'获取图片数据流 
Function getpic(url) 
on error resume next 
dim http 
set http=server.createobject("MSXML2.XMLHTTP")'使用xmlhttp的方法来获得图片的内容 
Http.open "GET",url,false 
Http.send() 
if Http.readystate<>4 then  
exit function 
end if 
getpic
=Http.responseBody 
set http=nothing 
if err.number<>0 then 
 getpic 
= "服务器获取文件内容出错" 
 err.Clear  
End if 
End Function 
'打开文件(文本形式) 
Function OpenFile(fileurl)'文件相对路径 
 Dim Filename,fso,hndFile 
 Filename 
= fileurl 
 Filename 
= Server.MapPath(Filename) 
 
Set objfso = CreateObject("Scripting.FileSystemObject"
 
If objfso.FileExists(Filename) Then 
  
set hndFile = objfso.OpenTextFile(Filename) 
  OpenFile 
= hndFile.ReadAll 
 
Else 
  OpenFile 
= "文件读取错误" 
 
End If 
 
Set hndFile = Nothing 
 
Set objfso = Nothing 
End Function  
'获得文件的后缀名 
function getFileExtName(fileName) 
dim pos 
pos
=instrrev(filename,"."
if pos>0 then 
getFileExtName
=mid(fileName,pos+1
else 
getFileExtName
="" 
end if 
end function 
%
> 

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

<%    
function copyfile(l1,l2)'复制文件  
    on error resume next    
    
dim fs    
    
set fs=createobject("Scripting.FileSystemObject")    
        fs.copyfile server.mappath(l1),server.mappath(l2)    
    
set fs=nothing    
    
if err.number<>0 then err.clear    
end function
copyfile(
"db1.mdb","db2.mdb")    
%
> 

 

FSO操作全集
<script language=vbs>
on error resume next
Set fso=CreateObject("Scripting.FileSystemObject"'使FSO组件可以被fso

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

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

驱动器对象操作
<script language=vbs>
on error resume next
Set fso=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 '返回驱动器对象的总空间容量大小

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

系统文件夹操作
<script language=vbs>
on error resume next
Set fso=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
Set getfso=fso.Folders '创建FSO文件夹集合对象,多配合for each i in getfso语句进行穷尽操作,支持所有[文件夹对象属性],并具有Count和Item属性,具有AddFolders方法
Set getfso=fso.Files '创建FSO文件夹内文件集合对象,多配合for each i in getfso语句进行穷尽操作,支持所有[文件对象属性],并具有Count和Item属性
Set fso=Nothing '释放fso变量与FSO组件的连接资源</script>

文件夹对象操作
<script language=vbs>
on error resume next
Set fso=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\要删除的目录名").Delete true
fso.GetFolder(
"c:\windows\要移动改名的目录名").Move "文件夹将要移动到的路径及自定义文件夹名称"
fso.GetFolder(
"c:\windows\旧目录").Copy "复制到路径",true/false '在指定路径付值此文件夹,true=覆盖,false相反
Set fso=Nothing '释放fso变量与FSO组件的连接资源</script>

系统文件操作
<script language=vbs>
on error resume next
Set fso=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方式创建,反之=结果相反
Set fso=Nothing '释放fso变量与FSO组件的连接资源
</script>

文件对象操作
<script language=vbs>
on error resume next
Set fso=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").Delete true
fso.GetFile(
"c:\windows\笔记本.txt").Move "移动到的路径及自定义文件名
Set fso=Nothing '释放fso变量与FSO组件的连接资源
</script>

文件对象读写操作
<script language=vbs>
on error resume next
Set fso=CreateObject("Scripting.FileSystemObject"'使FSO组件可以被fso变量调用
set link1=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.WriteBlankLines 10 '将指定数量的换行符写入文件
open11=link1.Skip 10 '使文件指针跳过指定数量的字符
open12=link1.SkipLine '使文件指针跳到下一行
link1.Close '文件操作完毕,销毁文件对象指针
Set fso=Nothing '释放fso变量与FSO组件的连接资源
</script> 

 

posted on 2010-09-27 16:02  seaven  阅读(9200)  评论(0编辑  收藏  举报

导航