文件操作的CLASS(VB)

 

<%
class cls_fso 
    
public objfso 
    
private sub class_initialize() 
        
set objfso = server.createobject("scripting.filesystemobject"
    
end sub 
    
private sub class_terminate() 
        
set objfso = nothing 
    
end sub 
    
    
'文件是否存在
    public function reportfilestatus(filename) 
        
dim msg 
        msg 
= -1 
        
if (objfso.fileexists(filename)) then 
            msg 
= 1 
        
else 
            msg 
= -1 
        
end if 
        reportfilestatus 
= msg 
    
end function 

    
'=======文件操作======== 
    '取文件大小
    public function getfilesize(filename) 
        
dim f 
        
if reportfilestatus(filename) = 1 then 
            
set f = objfso.getfile(filename) 
            getfilesize 
= f.size 
        
else 
            getfilesize 
= -1 
        
end if 
    
end function 

    
'刪除文件
    public function deleteafile(filespec) 
        
if reportfilestatus(filespec) = 1 then 
            objfso.deletefile(filespec) 
            deleteafile 
= 1 
        
else 
            deleteafile 
= -1 
        
end if 
    
end function 

    
'顯示文件列表
    public function showfilelist(folderspec) 
        
dim f, f1, fc, s 
        
if reportfolderstatus(folderspec) = 1 then 
            
set f = objfso.getfolder(folderspec) 
            
set fc = f.files 
            
for each f1 in fc 
                s 
= s & f1.name 
                s 
= s & "|" 
            
next 
            showfilelist 
= s 
        
else 
            showfilelist 
= -1 
        
end if 
    
end function 

    
'復制文件
    public function copyafile(sourcefile, destinationfile) 
        
dim myfile 
        
if reportfilestatus(sourcefile) = 1 then 
            
set myfile = objfso.getfile(sourcefile) 
            myfile.copy (destinationfile) 
            copyafile 
= 1 
        
else 
            copyafile 
= -1 
        
end if 
    
end function 

    
'移動文件
    public function moveafile(sourcefile,destinationfile) 
        
if reportfilestatus(sourcefile) = 1 and reportfilestatus(destinationfileorpath) = -1 then 
            objfso.movefile sourcefile,destinationfileorpath 
            moveafile 
= 1 
        
else 
            moveafile 
= -1 
        
end if 
    
end function 


    
'文件創建日期
    public function showdatecreated(filespec) 
        
dim f 
        
if reportfilestatus(filespec) = 1 then 
            
set f = objfso.getfile(filespec) 
            showdatecreated 
= f.datecreated 
        
else 
            showdatecreated 
= -1 
        
end if 
    
end function 

    
'文件屬性
    public function getattributes(filename) 
        
dim f 
        
dim strfileattributes 
        
if reportfilestatus(filename) = 1 then 
            
set f = objfso.getfile(filename) 
            
select case f.attributes 
                
case 0 strfileattributes = "普通文件,沒有設置任何屬性." 
                
case 1 strfileattributes = "只讀文件,可讀寫. " 
                
case 2 strfileattributes = "隱藏文件,可讀寫." 
                
case 4 strfileattributes = "系統文件,可讀寫." 
                
case 16 strfileattributes = "文件夾或目錄,只讀." 
                
case 32 strfileattributes = "上次備份後已更改的文件,可讀寫." 
                
case 1024 strfileattributes = "鏈接或是快捷方式,只讀." 
                
case 2048 strfileattributes = "壓縮文件,只讀." 
            
end select 
            getattributes 
= strfileattributes 
        
else 
            getattributes 
= -1 
        
end if 
    
end function 

    
'最後一次訪問,最後一次修改時間
    public function showfileaccessinfo(filename,infotype) 
        
'//功能:顯示文件創建信息 
        '//形參:文件名,信息類別.
        '// 1 -----創建時間
        '// 2 -----上次訪問時間
        '// 3 -----上次修改時間 
        '// 4 -----文件路徑
        '// 5 -----文件名稱 
        '// 6 -----文件類型 
        '// 7 -----文件大小 
        '// 8 -----父目錄 
        '// 9 -----根目錄 
        dim f, s 
        
if reportfilestatus(filename) = 1 then 
            
set f = objfso.getfile(filename) 
            
select case infotype 
                
case 1 s = f.datecreated 
                
case 2 s = f.datelastaccessed 
                
case 3 s = f.datelastmodified 
                
case 4 s = f.path 
                
case 5 s = f.name 
                
case 6 s = f.type 
                
case 7 s = f.size 
                
case 8 s = f.parentfolder 
                
case 9 s = f.rootfolder 
            
end select 
            showfileaccessinfo 
= s 
        
else 
            showfileaccessinfo 
= -1 
        
end if 
    
end function 

    
'寫文件
    public function writetxtfile(filename,textstr,writeorappendtype) 
        
const forreading = 1, forwriting = 2 , forappending = 8 
        
dim f, m 
        
select case writeorappendtype 
            
case 1'文件進行寫操作
                set f = objfso.opentextfile(filename, forwriting, true
                f.write textstr 
                f.close 
                
if reportfilestatus(filename) = 1 then 
                    writetxtfile 
= 1 
                
else 
                    writetxtfile 
= -1 
                
end if 
            
case 2'文件尾進行寫操作 
                if reportfilestatus(filename) = 1 then 
                    
set f = objfso.opentextfile(filename, forappending) 
                    f.write textstr 
                    f.close 
                    writetxtfile 
= 1 
                
else 
                    writetxtfile 
= -1 
                
end if 
            
end select 
    
end function 

    
'讀文本文件
    public function readtxtfile(filename) 
        
const forreading = 1, forwriting = 2 
        
dim f, m 
        
if reportfilestatus(filename) = 1 then 
            
set f = objfso.opentextfile(filename, forreading) 
            m 
= f.readline 
            readtxtfile 
= m 
            f.close 
        
else 
            readtxtfile 
= -1 
        
end if 
    
end function 

    
    
'=======目錄操作======== 
    '目錄是否存在
    public function reportfolderstatus(fldr) 
        
dim msg 
        msg 
= -1 
        
if (objfso.folderexists(fldr)) then 
            msg 
= 1 
        
else 
            msg 
= -1 
        
end if 
        reportfolderstatus 
= msg 
    
end function 
    
'取目錄大小
    public function getfoldersize(foldername) 
        
dim f 
        
if reportfolderstatus(foldername) = 1 then 
            
set f = objfso.getfolder(foldername) 
            getfoldersize 
= f.size 
        
else 
            getfoldersize 
= -1 
        
end if 
    
end function 

    
'創建新的目錄
    public function createfolderdemo(foldername) 
        
dim f 
        
if reportfolderstatus(folderspec) = 1 then 
            createfolderdemo 
= -1 
        
else 
            
set f = objfso.createfolder(foldername) 
            createfolderdemo 
= 1 
        
end if 
    
end function 

    
'刪除目錄
    public function deleteafolder(folderspec) 
        response.write folderspec 
        
if reportfolderstatus(folderspec) = 1 then 
            objfso.deletefolder (folderspec) 
            deleteafolder 
= 1 
        
else 
            deleteafolder 
= -1 
        
end if 
    
end function 

    
'顯示目錄列表
    public function showfolderlist(folderspec) 
        
dim f, f1, fc, s 
        
if reportfolderstatus(folderspec) = 1 then 
            
set f = objfso.getfolder(folderspec) 
            
set fc = f.subfolders 
            
for each f1 in fc 
                s 
= s & f1.name 
                s 
= s & "|" 
            
next 
            showfolderlist 
= s 
        
else 
            showfolderlist 
= -1 
        
end if 
    
end function 

    
'目錄復制
    public function copyafolder(sourcefolder,destinationfolder) 
        objfso.copyfolder sourcefolder,destinationfolder 
        copyafolder 
= 1 
        copyafolder 
= -1 
    
end function 


    
'目錄進行移動
    public function moveafolder(sourcepath,destinationpath) 
        
if reportfolderstatus(sourcepath)=1 and reportfolderstatus(destinationpath)=0 then 
            objfso.movefolder sourcepath, destinationpath 
            moveafolder 
= 1 
        
else 
            moveafolder 
= -1 
        
end if 
    
end function 


    
'目錄創建信息
    public function showfolderaccessinfo(foldername,infotype) 
        
'//功能:顯示目妹創建時信息 
        '//形參:目錄名,信息類別 
        '// 1 -----創建時間 
        '// 2 -----上次訪問時間 
        '// 3 -----上次修改時間 
        '// 4 -----目錄路徑
        '// 5 -----目錄名稱 
        '// 6 -----目錄類型 
        '// 7 -----目錄大小 
        '// 8 -----父目錄
        '// 9 -----根目錄 
        dim f, s 
        
if reportfolderstatus(foldername) = 1 then 
            
set f = objfso.getfolder(foldername) 
            
select case infotype 
                
case 1 s = f.datecreated 
                
case 2 s = f.datelastaccessed 
                
case 3 s = f.datelastmodified 
                
case 4 s = f.path 
                
case 5 s = f.name 
                
case 6 s = f.type 
                
case 7 s = f.size 
                
case 8 s = f.parentfolder 
                
case 9 s = f.rootfolder 
            
end select 
            showfolderaccessinfo 
= s 
        
else 
            showfolderaccessinfo 
= -1 
        
end if 
    
end function 

    
'遍歷目錄
    public function displayleveldepth(pathspec) 
        
dim f, n ,path 
        
set f = objfso.getfolder(pathspec) 
        
if f.isrootfolder then 
            displayleveldepth 
="指寫的文件夾是根文件夾."&rootfolder 
        
else 
            
do until f.isrootfolder 
                path 
= path & f.name &"<br>" 
                
set f = f.parentfolder 
                n 
= n + 1 
            
loop 
            displayleveldepth 
="指寫的文件夾是嵌套級為:" & n & " 的文件夾.<br>" & path 
        
end if 
    
end function 

    
'========磁盤操作======== 
    '驅動器是否存在
    public function reportdrivestatus(drv) 
        
dim msg 
        msg 
= -1 
        
if objfso.driveexists(drv) then 
            msg 
= 1 
        
else 
            msg 
= -1 
        
end if 
        reportdrivestatus 
= msg 
    
end function 

    
'可用的返回類型包括fat,ntfs,cdfs.
    public function showfilesystemtype(drvspec) 
        
dim d 
        
if reportdrivestatus(drvspec) = 1 then 
            
set d = objfso.getdrive(drvspec) 
            showfilesystemtype 
= d.filesystem 
        
else 
            showfilesystemtype 
= -1 
        
end if 
    
end function 
end class 
%
>
posted @ 2008-01-15 08:37  Athrun  阅读(391)  评论(0编辑  收藏  举报