' 创建FileSystemObject对象
Set objFSO = CreateObject ("Scripting.FileSystemObject")
For intCode = 65 To 90 'ANSI codes for 'A' to 'Z'
strLetter = Chr(intCode) 'Chr:返回与指定的 ANSI 字符代码相对应的字符 。
If objFSO.DriveExists(strLetter) Then '判断相应的盘符是否存在,如存在输出
Reporter.ReportEvent micDone,"Show Drive”,” Found drive " & strLetter
End If
Next
Function hanshu(strpath)
Dim objfso,objset
‘ 创建FileSystemObjec对象
Set objfso = CreateObject("Scripting.FileSystemObject")
‘ 判断在指定的路径中是否有
If Not objfso.FolderExists(strpath) Then
objset = objfso.CreateFolder(strpath)
else
objset = objfso.DeleteFolder(strpath)
End If
hanshu = objset
End Function
Dim objfso
'创建FileSystemObject对象
Set objfso = createobject("Scripting.filesystemobject")
'使用GetParentFolderName方法来返回上一层文件夹
msgbox (objfso.GetParentFolderName("C:/WINDOWS/addins"))
Dim fso,tempName
Set fso = CreateObject("Scripting.FileSystemObject")
'GetSpecialFolder()中的参数2,返回一个名为Temp的临时文件夹
Set tempName = fso.GetSpecialFolder(2)
msgbox tempName
Sub MoveFolders(sourcepath,destination)
Dim fso
'创建FileSystemObject对象
Set fso = CreateObject("Scripting.FileSystemObject")
'使用MoveFolder方法,将testing文件夹的内容移动到123文件夹下
fso.MoveFolder sourcepath,destination
End Sub
Dim sourcepath
sourcepath = "C:/testing"
Call FolderAttributes(sourcepath)
Sub FolderAttributes(sourcepath)
Dim fso,objset
Set fso = CreateObject("Scripting.FileSystemObject")
'使用GetFolder方法来返回Folder对象
Set objset = fso.getFolder(sourcepath)
'将testing中的所有内容复制到123文件夹中
objset.Copy("d:/123")
End Sub
备注:如果目标文件夹不存在,则脚本在运行是将自动创建
Delete的使用
方法名:Delete(force)
说明:删除文件夹及里面的所有内容 。详细说明请见表5
示例:
Dim sourcepath
sourcepath = "C:/testing"
Call FolderAttributes(sourcepath)
Sub FolderAttributes(sourcepath)
Dim fso,objset
Set fso = CreateObject("Scripting.FileSystemObject")
'使用GetFolder方法来返回Folder对象
Set objset = fso.getFolder(sourcepath)
'将testingw文件夹及文件夹里所有的内容全部删除
objset.Delete(True)
End Sub
Dim sourcepath
sourcepath = "C:/testing"
Call FolderAttributes(sourcepath)
Sub FolderAttributes(sourcepath)
Dim fso,objset
Set fso = CreateObject("Scripting.FileSystemObject")
'使用GetFolder方法来返回Folder对象
Set objset = fso.getFolder(sourcepath)
'使用Move()将testing文件夹及里面所有的内容,移动到目标文件夹123中
objset.Move("C:/123")
End Sub
Dim sourcepath
sourcepath = "C:/testing"
Call FolderAttributes(sourcepath)
Sub FolderAttributes(sourcepath)
Dim fso,objset
Set fso = CreateObject("Scripting.FileSystemObject")
'使用GetFolder方法来返回Folder对象
Set objset = fso.getFolder(sourcepath)
'在testing文件夹下,使用CreateTextFile()创建myClass2.doc文件
objset.CreateTextFile "myClass2.doc",true
End Sub
Dim objfso,objset
'创建FileSystemObject对象
Set objfso = createobject("Scripting.FileSystemObject")
'返回C:/testing的Folder的对象
Set objset = objfso.GetFolder("C:/testing")
Dim sourcepath
sourcepath = "C:/testing"
Call FolderAttributes(sourcepath)
Sub FolderAttributes(sourcepath)
Dim fso,objset
Set fso = CreateObject("Scripting.FileSystemObject")
'使用GetFolder方法来返回Folder对象
Set objset = fso.getFolder(sourcepath)
'通过Folder对象来访问Folder属性
reporter.ReportEvent micDone ,"测试Folder属性","文件夹所在的磁盘为:" & objset.Drive & " 文件夹所创建的日期:" & objset.DateCreated & " 文件夹的名字为:" & objset.Name
End Sub
SubFolers属性的使用
示例:
Dim sourcepath
sourcepath = "C:/Program Files/Common Files"
Call FolderAttributes(sourcepath)
Sub FolderAttributes(sourcepath)
Dim fso,objset,objFolders,FolderName,i
i = 0
Set fso = CreateObject("Scripting.FileSystemObject")
'使用GetFolder方法来返回Folder对象
Set objset = fso.getFolder(sourcepath)
'SubFolders属性将返回所有子文件夹对应的Folder集合
Set objFolders = objset.SubFolders
'遍历Folder集合,统计出共有多少个文件夹,及相关文件夹的名字
For Each objFolder in objFolders
i = i +1
'vbcr为换行符
FolderName = FolderName + objFolder.Name + vbcr
Next
msgbox ("共有" & i &"个文件夹,文件夹的名字为:" & FolderName)
End Sub
Files属性的使用
示例:
Dim objFso,objGetFolder,intCount,strFileName
intCount = 0
'创建FileSystemObject对象
Set objFso= CreateObject("Scripting.FileSystemObject")
'使用GetFolder()获得文件夹对象
Set objGetFolder = objFso.GetFolder("C:/test")
'遍历Files集合并显示文件夹中所有的文件名
For Each strFile in objGetFolder.Files
intCount = intCount + 1
strFileName =strFileName & "第" & intCount & "个文件夹名为:" & strFile.Name & vbcr
Next
msgbox strFileName
Dim sourcepath,targetpath
sourcepath = "C:/testing/*.txt"
targetpath = "C:/123/"
Call FolderAttributes(sourcepath,targetpath)
Sub FolderAttributes(sourcepath,targetpath)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
'将testing文件夹下所有扩展名名为.txt的文件,复制到123文件夹下
fso.CopyFile sourcepath,targetpath
fso.MoveFile sourcepath,targetpath
End Sub
Dim sourcepath
sourcepath = "C:/testing/ myClass2.doc "
Call FolderAttributes(sourcepath)
Sub FolderAttributes(sourcepath)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
'在testing文件夹下,使用CreateTextFile()创建myClass2.doc文件
fso.CreateTextFile sourcepath,true
'删除testing文件夹下,所有扩展名为.txt的文件
fso.DeleteFile sourcepath,true
End Sub
Dim sourcepath
sourcepath = "C:/testing/test.txt"
Call FolderAttributes(sourcepath)
Sub FolderAttributes(sourcepath)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
'判断C:/testing/test.txt是否有存在,如果存在返回“true”;否则返回“false”
If fso.FileExists(sourcepath) Then
msgbox "true"
else
msgbox "false"
End If
End Sub
Dim sourcepath
sourcepath = "C:/testing/test.txt"
Call FolderAttributes(sourcepath)
Sub FolderAttributes(sourcepath)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
'返回文件名test, 扩展名 txt, 文件名+扩展名 test.txt
Msgbox (fso.GetBaseName(sourcepath))
Msgbox (fso. GetExtensionName(sourcepath))
Msgbox (fso.GetFileName(sourcepath))
End Sub
Dim sourcepath
sourcepath = "C:/testing/778899.txt"
Call FolderAttributes(sourcepath)
Sub FolderAttributes(sourcepath)
Dim fso,objFile
Set fso = CreateObject("Scripting.FileSystemObject")
'调用GetFile()返回File对象,通过File对象来访问其下的属性
set objFile = fso.GetFile(sourcepath)
reporter.ReportEvent micDone,"访问file的属性","该文件所在的盘符为:" & objFile.Drive & " 文件名为:" & objFile.Name
End Sub
Dim fso, tempfile
'创建FileSystemObject对象
Set fso = CreateObject("Scripting.FileSystemObject")
'调用CreateTempFile函数,并将对象赋值给tempfile
Set tempfile = CreateTempFile
'在临时文件中写入字符
tempfile.WriteLine "世界你好"
'关闭对象
tempfile.Close
Function CreateTempFile
Dim tfolder, tname, tfile
Const TemporaryFolder = 2
'GetSpecialFolder()中的参数2,返回一个名为Temp的临时文件夹
Set tfolder = fso.GetSpecialFolder(TemporaryFolder)
'随机生成一个临时文件名
tname = fso.GetTempName
'在Temp临时文件夹下面,创建一个临时文件
Set tfile = tfolder.CreateTextFile(tname)
'将结果赋值给函数
Set CreateTempFile = tfile
End Function
备注:GetTempName 方法不创建文件,该方法仅提供临时文件名 。示例中的的结果,默认保存在C:/Documents and Settings/Administrator/Local Settings/Temp
Sub OpenTextFileTest(sepc,intForAppending)
Dim fso,objTStream
'创建FileSystemObject对象
Set fso = CreateObject("Scripting.FileSystemObject")
'参数true表示:如果文件不存在,则创建文件 。最后OpenTextFile()将返回TextStream对象
Set objTStream = fso.OpenTextFile(sepc,intForAppending,true)
'利用TextStream对象来将字符串写入文件
objTStream.write "hello word!"
'释放TextStream对象
objTStream.close
End Sub
Dim sourcepath
sourcepath = "C:/testing/778899.txt"
Call FolderAttributes(sourcepath)
Sub FolderAttributes(sourcepath)
Dim fso,objFile
Set fso = CreateObject("Scripting.FileSystemObject")
'调用GetFile()返回File对象,通过File对象来访问其下的属性
set objFile = fso.GetFile(sourcepath)
reporter.ReportEvent micDone,"访问file的属性","该文件所在的盘符为:" & objFile.Drive & " 文件名为:" & objFile.Name
End Sub
Dim strPath,strText
strPath = "C:/testing.txt"
strText = "This is Test !" & vbCrLf & “hello word !”
'调用函数
Call CreateFile(strPath,strText)
Sub CreateFile(strPath,strText)
Dim objFso,objStream
'创建FileSystemObject对象
Set objFso = CreateObject("Scripting.FileSystemObject")
'使用FileSystemObject对象的CreateTextFile(),来返回一个TextStream对象句柄
Set objStream = objFso.CreateTextFile(strPath,True)
'三个Write的意思分别为:在文本中写入字符、写入带换行符的字符、写入3个换行符
objStream.Write(strText)
‘objStream.WriteLine(strText)
‘objStream. WriteBlankLines 3
'关闭TextStream对象
objStream.Close
End Sub
Sub CreateFile(strPath,strText)
Dim objFso,objStream
'创建FileSystemObject对象
Set objFso = CreateObject("Scripting.FileSystemObject")
'使用FileSystemObject对象的CreateTextFile(),来返回一个TextStream对象句柄
Set objStream = objFso.CreateTextFile(strPath,True)
'写入字符
objStream.WriteLine(strText)
Set objStream = objFso.OpenTextFile(strPath,1,true)
msgbox (objStream.ReadLine)
'msgbox (objStream.ReadAll)
' msgbox (objStream.Read(Len(strText)))
'关闭TextStream对象
objStream.Close
End Sub
Dim strPath,strText
strPath = "C:/testing.txt"
'调用函数
Call CreateFile(strPath)
Sub CreateFile(strPath)
Dim objFso,objStream
'创建FileSystemObject对象
Set objFso = CreateObject("Scripting.FileSystemObject")
'使用FileSystemObject对象的CreateTextFile(),来返回一个TextStream对象句柄
Set objStream = objFso.CreateTextFile(strPath,True)
'在文本中写入字符
objStream.Write "This is Test !" & vbCrLf & "hello word !"
'以只读的方式打开文件
Set objStream = objFso.OpenTextFile(strPath,1,true)
'读取文件时跳过5个字符;或者跳过当前行,读取下一行
objStream.Skip(5)
'objStream.SkipLine
'读取文本内容
msgbox objStream.ReadAll
'关闭TextStream对象
objStream.Close
End Sub
Dim strPath,strText
strPath = "C:/testing.txt"
'调用函数
Call CreateFile(strPath)
Sub CreateFile(strPath)
Dim objFso,objStream,str
'创建FileSystemObject对象
Set objFso = CreateObject("Scripting.FileSystemObject")
'以只读的方式打开文件,如果文件不存在则创建它
Set objStream = objFso.OpenTextFile(strPath,1,true)
'如果当前的指针不在行末,则读取文本内容
Do While objStream.AtEndOfLine <> true
‘Do While objStream.AtEndOfStream <> true
str = str + objStream.Read(1)
Loop
msgbox str
'关闭TextStream对象
objStream.Close
End Sub
Sub TestTextStream(strPath)
Dim objFso,objTStream,str
Set objFso = CreateObject("Scripting.FileSystemObject")
'以只读的方式打开文件
Set objTStream = objFso.OpenTextFile(strPath,1)
'如果当前的指针不在整个文档的末尾,读取文本的所有内容
Do While objTStream.AtEndOfStream <> true
objTStream.ReadAll
str = str + "共有" & objTStream.Line & "行数据,光标最后所在列号为:" & objTStream.Column & vbCrLf
Loop
'打印信息
print str
End Sub
FileSystemObject实例代码
创建文件
set fso = server.CreateObject("Scripting.FileSystemObject")
set f = fso.CreateTextFile("C:\test.txt", true) '第二个参数表示目标文件存在时是否覆盖
f.Write("写入内容")
f.WriteLine("写入内容并换行")
f.WriteBlankLines(3) '写入三个空白行(相当于在文本编辑器中按三次回车)
f.Close()
set f = nothing
set fso = nothing
打开并读文件
set fso = server.CreateObject("Scripting.FileSystemObject")
set f = fso.OpenTextFile("C:\test.txt", 1, false) '第二个参数 1 表示只读打开,第三个参数表示目标文件不存在时是否创建
f.Skip(3) '将当前位置向后移三个字符
f.SkipLine() '将当前位置移动到下一行的第一个字符,注意:无参数
response.Write f.Read(3) '从当前位置向后读取三个字符,并将当前位置向后移三个字符
response.Write f.ReadLine() '从当前位置向后读取直到遇到换行符(不读取换行符),并将当前位置移动到下一行的第一个字符,注意:无参数
response.Write f.ReadAll() '从当前位置向后读取,直到文件结束,并将当前位置移动到文件的最后
if f.atEndOfLine then
response.Write("一行的结尾!")
end if
if f.atEndOfStream then
response.Write("文件的结尾!")
end if
f.Close()
set f = nothing
set fso = nothing
打开并写文件
set fso = server.CreateObject("Scripting.FileSystemObject")
set f = fso.OpenTextFile("C:\test.txt", 2, false) '第二个参数 2 表示重写,如果是 8 表示追加
f.Write("写入内容")
f.WriteLine("写入内容并换行")
f.WriteBlankLines(3) '写入三个空白行(相当于在文本编辑器中按三次回车)
f.Close()
set f = nothing
set fso = nothing
判断文件是否存在
set fso = server.CreateObject("Scripting.FileSystemObject")
if fso.FileExists("C:\test.txt") then
response.Write("目标文件存在")
else
response.Write("目标文件不存在")
end if
set fso = nothing
移动文件
set fso = server.CreateObject("Scripting.FileSystemObject")
call fso.MoveFile("C:\test.txt", "D:\test111.txt") '两个参数的文件名部分可以不同
set fso = nothing
复制文件
set fso = server.CreateObject("Scripting.FileSystemObject")
call fso.CopyFile("C:\test.txt", "D:\test111.txt") '两个参数的文件名部分可以不同
set fso = nothing
删除文件
set fso = server.CreateObject("Scripting.FileSystemObject")
fso.DeleteFile("C:\test.txt")
set fso = nothing
创建文件夹
set fso = server.CreateObject("Scripting.FileSystemObject")
fso.CreateFolder("C:\test") '目标文件夹的父文件夹必须存在
set fso = nothing
判断文件夹是否存在
set fso = server.CreateObject("Scripting.FileSystemObject")
if fso.FolderExists("C:\Windows") then
response.Write("目标文件夹存在")
else
response.Write("目标文件夹不存在")
end if
set fso = nothing
删除文件夹
set fso = server.CreateObject("Scripting.FileSystemObject")
fso.DeleteFolder("C:\test") '文件夹不必为空
set fso = nothing
检测驱动器C盘是否存在
Set fso = Server.CreateObject("Scripting.FileSystemObject")
fso.DriveExists("c:")
获取文件路径的驱动器名
Set fso=Server.CreateObject("Scripting.FileSystemObject")
p=fso.GetDriveName(Server.MapPath("aqa33"))
Response.Write("驱动器名称是:" & p)
set fs=nothing
取得某个指定的路径的父文件夹的名称
Set fso=Server.CreateObject("Scripting.FileSystemObject")
p=fso.GetParentFolderName(Server.MapPath("aqa331.asp"))
Response.Write("父文件夹名称是:" & p)
set fs=nothing
取得指定路径中的最后一个成分的文件扩展名
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Response.Write(fs.GetExtensionName(Server.MapPath("aqa33.asxd")))
set fs=nothing
取得指定路径中的最后一个成分的文件名
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Response.Write(fs.GetFileName(Server.MapPath("aqa33.asxd")))
set fs=nothing
返回在指定的路径中文件或者文件夹的基本名称 。
Set fso=Server.CreateObject("Scripting.FileSystemObject")
Response.Write(fso.GetBaseName("c:\windows\cursors\abc.cur"))
Response.Write("<br />")
Response.Write(fso.GetBaseName("c:\windows\cursors\"))
Response.Write("<br />")
Response.Write(fso.GetBaseName("c:\windows\"))
set fso=nothing