FileSystemObject 示例代码
程序员文章站
2022-08-28 10:40:39
在本节描述的示例代码,提供真实的例子来示范在 filesystemobject 对象模式中可用的许多功能。该代码显示了如何一起使用对象模式的所有功能,以...
在本节描述的示例代码,提供真实的例子来示范在 filesystemobject 对象模式中可用的许多功能。该代码显示了如何一起使用对象模式的所有功能,以及如何在您自己的代码中有效地使用这些功能。
请注意,由于该代码是极一般的,所以要使该代码能够真正在您的机器上运行,可能需要一些其他代码和小小的变更。这些改变之所以必要,是因为在 active server pages 和 windows scripting host 之间,为输入和输出给用户采用了不同的方法。
要在 active server pages 上运行该代码,则采取以下步骤:
创建一个标准的 web 页,后缀名为 .asp。
把下面的示例代码复制到 <body>...</body> 标记之间的文件中。
把所有代码包装器到 <%...%> 标记内。
把 option explicit 语句从当前位置移动到 html 页的最顶部,甚至在 <html> 开始标记前。
把 <%...%> 标记放置在 option explicit 语句周围,以保证它在服务器端运行。
把下面的代码添加到示例代码末尾:
sub print(x)
response.write "<pre><font face=""宋体"" size=""1"">"
response.write x
response.write "</font></pre>"
end sub
main
前面的代码增加一个将在服务器端运行,但在客户端显示结果的打印过程。要在 windows scripting host 上运行该代码,则把下面的代码添加到示例代码的末尾:
sub print(x)
wscript.echo x
end sub
main
下面就是示例代码:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' filesystemobject 示例代码
'copyright 1998 microsoft corporation。 保留所有权利。
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''optionexplicit''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''
' 对于代码质量:
' 1) 下面的代码有许多字符串操作,用"&"运算符来把短字符串连接在一起。由于
' 字符串连接是费时的,所以这是一种低效率的写代码方法。无论如何,它是
' 一种非常好维护的写代码方法,并且在这儿使用了这种方法,因为该程序执行
' 大量的磁盘操作,而磁盘操作比连接字符串所需的内存操作要慢得多。
' 记住这是示范代码,而不是产品代码。
'
' 2) 使用了 "option explicit",因为访问声明过的变量,比访问未声明的变量要
' 稍微快一些。它还能阻止在代码中发生错误,例如,把 drivetypecdrom 误拼
' 成了 drivetypecdorm 。
'
' 3) 为了使代码更可读,该代码中没有错误处理。虽然采取了防范措施,来保证代码
' 在普通情况下没有错误,但文件系统是不可预知的。在产品代码中,使用
' on error resume next 和 err 对象来捕获可能发生的错误。
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 一些容易取得的全局变量
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
dim tabstop
dim newlineconst testdrive = "c"
const testfilepath = "c:\test"'''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''
' 由 drive.drivetype 返回的常数
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
const drivetyperemovable = 1
const drivetypefixed = 2
const drivetypenetwork = 3
const drivetypecdrom = 4
const drivetyperamdisk = 5''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''
' 由 file.attributes 返回的常数
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
const fileattrnormal = 0
const fileattrreadonly = 1
const fileattrhidden = 2
const fileattrsystem = 4
const fileattrvolume = 8
const fileattrdirectory = 16
const fileattrarchive = 32
const fileattralias = 64
const fileattrcompressed = 128''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''
' 用来打开文件的常数
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
const openfileforreading = 1
const openfileforwriting = 2
const openfileforappending = 8 '''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''
' showdrivetype
' 目的:
' 生成一个字符串,来描述给定 drive 对象的驱动器类型。
' 示范下面的内容
' - drive.drivetype
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
function showdrivetype(drive) dim s
select case drive.drivetype
case drivetyperemovable
s = "removable"
case drivetypefixed
s = "fixed"
case drivetypenetwork
s = "network"
case drivetypecdrom
s = "cd-rom"
case drivetyperamdisk
s = "ram disk"
case else
s = "unknown"
end select showdrivetype = send function''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''
' showfileattr
' 目的:
' 生成一个字符串,来描述文件或文件夹的属性。
' 示范下面的内容
' - file.attributes
' - folder.attributes
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''function showfileattr(file) ' file 可以是文件或文件夹 dim s
dim attr
attr = file.attributes if attr = 0 then
showfileattr = "normal"
exit function
end if if attr and fileattrdirectory then s = s & "directory "
if attr and fileattrreadonly then s = s & "read-only "
if attr and fileattrhidden then s = s & "hidden "
if attr and fileattrsystem then s = s & "system "
if attr and fileattrvolume then s = s & "volume "
if attr and fileattrarchive then s = s & "archive "
if attr and fileattralias then s = s & "alias "
if attr and fileattrcompressed then s = s & "compressed " showfileattr = send function''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''
' generatedriveinformation
' 目的:
' 生成一个字符串,来描述可用驱动器的当前状态。
' 示范下面的内容
' - filesystemobject.drives
' - iterating the drives collection
' - drives.count
' - drive.availablespace
' - drive.driveletter
' - drive.drivetype
' - drive.filesystem
' - drive.freespace
' - drive.isready
' - drive.path
' - drive.serialnumber
' - drive.sharename
' - drive.totalsize
' - drive.volumename
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''function generatedriveinformation(fso) dim drives
dim drive
dim s set drives = fso.drives
s = "number of drives:" & tabstop & drives.count & newline & newline ' 构造报告的第一行。
s = s & string(2, tabstop) & "drive"
s = s & string(3, tabstop) & "file"
s = s & tabstop & "total"
s = s & tabstop & "free"
s = s & tabstop & "available"
s = s & tabstop & "serial" & newline ' 构造报告的第二行。
s = s & "letter"
s = s & tabstop & "path"
s = s & tabstop & "type"
s = s & tabstop & "ready?"
s = s & tabstop & "name"
s = s & tabstop & "system"
s = s & tabstop & "space"
s = s & tabstop & "space"
s = s & tabstop & "space"
s = s & tabstop & "number" & newline ' 分隔行。
s = s & string(105, "-") & newline for each drive in drives
s = s & drive.driveletter
s = s & tabstop & drive.path
s = s & tabstop & showdrivetype(drive)
s = s & tabstop & drive.isready if drive.isready then
if drivetypenetwork = drive.drivetype then
s = s & tabstop & drive.sharename
else
s = s & tabstop & drive.volumename
end if
s = s & tabstop & drive.filesystem
s = s & tabstop & drive.totalsize
s = s & tabstop & drive.freespace
s = s & tabstop & drive.availablespace
s = s & tabstop & hex(drive.serialnumber)
end if s = s & newline next generatedriveinformation = send function''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''
' generatefileinformation
' 目的:
' 生成一个字符串,来描述文件的当前状态。
' 示范下面的内容
' - file.path
' - file.name
' - file.type
' - file.datecreated
' - file.datelastaccessed
' - file.datelastmodified
' - file.size
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''function generatefileinformation(file) dim s s = newline & "path:" & tabstop & file.path
s = s & newline & "name:" & tabstop & file.name
s = s & newline & "type:" & tabstop & file.type
s = s & newline & "attribs:" & tabstop & showfileattr(file)
s = s & newline & "created:" & tabstop & file.datecreated
s = s & newline & "accessed:" & tabstop & file.datelastaccessed
s = s & newline & "modified:" & tabstop & file.datelastmodified
s = s & newline & "size" & tabstop & file.size & newline generatefileinformation = send function''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' generatefolderinformation
' 目的:
' 生成一个字符串,来描述文件夹的当前状态。
' 示范下面的内容
' - folder.path
' - folder.name
' - folder.datecreated
' - folder.datelastaccessed
' - folder.datelastmodified
' - folder.size
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''function generatefolderinformation(folder) dim s s = "path:" & tabstop & folder.path
s = s & newline & "name:" & tabstop & folder.name
s = s & newline & "attribs:" & tabstop & showfileattr(folder)
s = s & newline & "created:" & tabstop & folder.datecreated
s = s & newline & "accessed:" & tabstop & folder.datelastaccessed
s = s & newline & "modified:" & tabstop & folder.datelastmodified
s = s & newline & "size:" & tabstop & folder.size & newline generatefolderinformation = send function''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' generateallfolderinformation
' 目的:
' 生成一个字符串,来描述一个文件夹和所有文件及子文件夹的当前状态。
' 示范下面的内容
' - folder.path
' - folder.subfolders
' - folders.count
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''function generateallfolderinformation(folder) dim s
dim subfolders
dim subfolder
dim files
dim file s = "folder:" & tabstop & folder.path & newline & newline
set files = folder.files if 1 = files.count then
s = s & "there is 1 file" & newline
else
s = s & "there are " & files.count & " files" & newline
end if if files.count <> 0 then
for each file in files
s = s & generatefileinformation(file)
next
end if set subfolders = folder.subfolders if 1 = subfolders.count then
s = s & newline & "there is 1 sub folder" & newline & newline
else
s = s & newline & "there are " & subfolders.count & " sub folders" & newline & newline
end if if subfolders.count <> 0 then
for each subfolder in subfolders
s = s & generatefolderinformation(subfolder)
next
s = s & newline
for each subfolder in subfolders
s = s & generateallfolderinformation(subfolder)
next
end if generateallfolderinformation = send function'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''
' generatetestinformation
' 目的:
' 生成一个字符串,来描述 c:\test 文件夹和所有文件及子文件夹的当前状态。
' 示范下面的内容
' - filesystemobject.driveexists
' - filesystemobject.folderexists
' - filesystemobject.getfolder
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''function generatetestinformation(fso) dim testfolder
dim s if not fso.driveexists(testdrive) then exit function
if not fso.folderexists(testfilepath) then exit function set testfolder = fso.getfolder(testfilepath) generatetestinformation = generateallfolderinformation(testfolder) end function'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''
' deletetestdirectory
' 目的:
' 清理 test 目录。
' 示范下面的内容
' - filesystemobject.getfolder
' - filesystemobject.deletefile
' - filesystemobject.deletefolder
' - folder.delete
' - file.delete
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''sub deletetestdirectory(fso) dim testfolder
dim subfolder
dim file
' 有两种方法可用来删除文件: fso.deletefile(testfilepath & "\beatles\octopusgarden.txt") set file = fso.getfile(testfilepath & "\beatles\bathroomwindow.txt")
file.delete ' 有两种方法可用来删除文件夹:
fso.deletefolder(testfilepath & "\beatles")
fso.deletefile(testfilepath & "\readme.txt")
set testfolder = fso.getfolder(testfilepath)
testfolder.deleteend sub'''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''
' createlyrics
' 目的:
' 在文件夹中创建两个文本文件。
' 示范下面的内容
' - filesystemobject.createtextfile
' - textstream.writeline
' - textstream.write
' - textstream.writeblanklines
' - textstream.close
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''sub createlyrics(folder) dim textstream
set textstream = folder.createtextfile("octopusgarden.txt")
textstream.write("octopus' garden ") ' 请注意,该语句不添加换行到文件中。
textstream.writeline("(by ringo starr)")
textstream.writeblanklines(1)
textstream.writeline("i'd like to be under the sea in an octopus' garden in the shade,")
textstream.writeline("he'd let us in, knows where we've been -- in his octopus' garden in the shade.")
textstream.writeblanklines(2)
textstream.close set textstream = folder.createtextfile("bathroomwindow.txt")
textstream.writeline("she came in through the bathroom window (by lennon/mccartney)")
textstream.writeline("")
textstream.writeline("she came in through the bathroom window protected by a silver spoon")
textstream.writeline("but now she sucks her thumb and wanders by the banks of her own lagoon")
textstream.writeblanklines(2)
textstream.closeend sub''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' getlyrics
' 目的:
' 显示 lyrics 文件的内容。
' 示范下面的内容
' - filesystemobject.opentextfile
' - filesystemobject.getfile
' - textstream.readall
' - textstream.close
' - file.openastextstream
' - textstream.atendofstream
' - textstream.readline
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''function getlyrics(fso) dim textstream
dim s
dim file ' 有多种方法可用来打开一个文本文件,和多种方法来从文件读取数据。
' 这儿用了两种方法来打开文件和读取文件: set textstream = fso.opentextfile(testfilepath & "\beatles\octopusgarden.txt", openfileforreading)
s = textstream.readall & newline & newline
textstream.close set file = fso.getfile(testfilepath & "\beatles\bathroomwindow.txt")
set textstream = file.openastextstream(openfileforreading)
do while not textstream.atendofstream
s = s & textstream.readline & newline
loop
textstream.close getlyrics = s
end function'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''
' buildtestdirectory
' 目的:
' 创建一个目录分层结构来示范 filesystemobject。
' 以这样的次序来创建分层结构:
' c:\test
' c:\test\readme.txt
' c:\test\beatles
' c:\test\beatles\octopusgarden.txt
' c:\test\beatles\bathroomwindow.txt
' 示范下面的内容
' - filesystemobject.driveexists
' - filesystemobject.folderexists
' - filesystemobject.createfolder
' - filesystemobject.createtextfile
' - folders.add
' - folder.createtextfile
' - textstream.writeline
' - textstream.close
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''function buildtestdirectory(fso) dim testfolder
dim subfolders
dim subfolder
dim textstream
' 排除(a)驱动器不存在,或(b)要创建的目录已经存在的情况。 if not fso.driveexists(testdrive) then
buildtestdirectory = false
exit function
end if if fso.folderexists(testfilepath) then
buildtestdirectory = false
exit function
end if set testfolder = fso.createfolder(testfilepath) set textstream = fso.createtextfile(testfilepath & "\readme.txt")
textstream.writeline("my song lyrics collection")
textstream.close set subfolders = testfolder.subfolders
set subfolder = subfolders.add("beatles")
createlyrics subfolder
buildtestdirectory = trueend function''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''
' 主程序
' 首先,它创建一个 test 目录,以及一些子文件夹和文件。
' 然后,它转储有关可用磁盘驱动器和 test 目录的某些信息,
' 最后,清除 test 目录及其所有内容。
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''sub main dim fso ' 设立全局变量。
tabstop = chr(9)
newline = chr(10)
set fso = createobject("scripting.filesystemobject") if not buildtestdirectory(fso) then
print "test directory already exists or cannot be created. cannot continue."
exit sub
end if print generatedriveinformation(fso) & newline & newline
print generatetestinformation(fso) & newline & newline
print getlyrics(fso) & newline & newline
deletetestdirectory(fso)end sub
请注意,由于该代码是极一般的,所以要使该代码能够真正在您的机器上运行,可能需要一些其他代码和小小的变更。这些改变之所以必要,是因为在 active server pages 和 windows scripting host 之间,为输入和输出给用户采用了不同的方法。
要在 active server pages 上运行该代码,则采取以下步骤:
创建一个标准的 web 页,后缀名为 .asp。
把下面的示例代码复制到 <body>...</body> 标记之间的文件中。
把所有代码包装器到 <%...%> 标记内。
把 option explicit 语句从当前位置移动到 html 页的最顶部,甚至在 <html> 开始标记前。
把 <%...%> 标记放置在 option explicit 语句周围,以保证它在服务器端运行。
把下面的代码添加到示例代码末尾:
sub print(x)
response.write "<pre><font face=""宋体"" size=""1"">"
response.write x
response.write "</font></pre>"
end sub
main
前面的代码增加一个将在服务器端运行,但在客户端显示结果的打印过程。要在 windows scripting host 上运行该代码,则把下面的代码添加到示例代码的末尾:
sub print(x)
wscript.echo x
end sub
main
下面就是示例代码:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' filesystemobject 示例代码
'copyright 1998 microsoft corporation。 保留所有权利。
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''optionexplicit''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''
' 对于代码质量:
' 1) 下面的代码有许多字符串操作,用"&"运算符来把短字符串连接在一起。由于
' 字符串连接是费时的,所以这是一种低效率的写代码方法。无论如何,它是
' 一种非常好维护的写代码方法,并且在这儿使用了这种方法,因为该程序执行
' 大量的磁盘操作,而磁盘操作比连接字符串所需的内存操作要慢得多。
' 记住这是示范代码,而不是产品代码。
'
' 2) 使用了 "option explicit",因为访问声明过的变量,比访问未声明的变量要
' 稍微快一些。它还能阻止在代码中发生错误,例如,把 drivetypecdrom 误拼
' 成了 drivetypecdorm 。
'
' 3) 为了使代码更可读,该代码中没有错误处理。虽然采取了防范措施,来保证代码
' 在普通情况下没有错误,但文件系统是不可预知的。在产品代码中,使用
' on error resume next 和 err 对象来捕获可能发生的错误。
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 一些容易取得的全局变量
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
dim tabstop
dim newlineconst testdrive = "c"
const testfilepath = "c:\test"'''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''
' 由 drive.drivetype 返回的常数
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
const drivetyperemovable = 1
const drivetypefixed = 2
const drivetypenetwork = 3
const drivetypecdrom = 4
const drivetyperamdisk = 5''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''
' 由 file.attributes 返回的常数
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
const fileattrnormal = 0
const fileattrreadonly = 1
const fileattrhidden = 2
const fileattrsystem = 4
const fileattrvolume = 8
const fileattrdirectory = 16
const fileattrarchive = 32
const fileattralias = 64
const fileattrcompressed = 128''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''
' 用来打开文件的常数
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
const openfileforreading = 1
const openfileforwriting = 2
const openfileforappending = 8 '''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''
' showdrivetype
' 目的:
' 生成一个字符串,来描述给定 drive 对象的驱动器类型。
' 示范下面的内容
' - drive.drivetype
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
function showdrivetype(drive) dim s
select case drive.drivetype
case drivetyperemovable
s = "removable"
case drivetypefixed
s = "fixed"
case drivetypenetwork
s = "network"
case drivetypecdrom
s = "cd-rom"
case drivetyperamdisk
s = "ram disk"
case else
s = "unknown"
end select showdrivetype = send function''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''
' showfileattr
' 目的:
' 生成一个字符串,来描述文件或文件夹的属性。
' 示范下面的内容
' - file.attributes
' - folder.attributes
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''function showfileattr(file) ' file 可以是文件或文件夹 dim s
dim attr
attr = file.attributes if attr = 0 then
showfileattr = "normal"
exit function
end if if attr and fileattrdirectory then s = s & "directory "
if attr and fileattrreadonly then s = s & "read-only "
if attr and fileattrhidden then s = s & "hidden "
if attr and fileattrsystem then s = s & "system "
if attr and fileattrvolume then s = s & "volume "
if attr and fileattrarchive then s = s & "archive "
if attr and fileattralias then s = s & "alias "
if attr and fileattrcompressed then s = s & "compressed " showfileattr = send function''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''
' generatedriveinformation
' 目的:
' 生成一个字符串,来描述可用驱动器的当前状态。
' 示范下面的内容
' - filesystemobject.drives
' - iterating the drives collection
' - drives.count
' - drive.availablespace
' - drive.driveletter
' - drive.drivetype
' - drive.filesystem
' - drive.freespace
' - drive.isready
' - drive.path
' - drive.serialnumber
' - drive.sharename
' - drive.totalsize
' - drive.volumename
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''function generatedriveinformation(fso) dim drives
dim drive
dim s set drives = fso.drives
s = "number of drives:" & tabstop & drives.count & newline & newline ' 构造报告的第一行。
s = s & string(2, tabstop) & "drive"
s = s & string(3, tabstop) & "file"
s = s & tabstop & "total"
s = s & tabstop & "free"
s = s & tabstop & "available"
s = s & tabstop & "serial" & newline ' 构造报告的第二行。
s = s & "letter"
s = s & tabstop & "path"
s = s & tabstop & "type"
s = s & tabstop & "ready?"
s = s & tabstop & "name"
s = s & tabstop & "system"
s = s & tabstop & "space"
s = s & tabstop & "space"
s = s & tabstop & "space"
s = s & tabstop & "number" & newline ' 分隔行。
s = s & string(105, "-") & newline for each drive in drives
s = s & drive.driveletter
s = s & tabstop & drive.path
s = s & tabstop & showdrivetype(drive)
s = s & tabstop & drive.isready if drive.isready then
if drivetypenetwork = drive.drivetype then
s = s & tabstop & drive.sharename
else
s = s & tabstop & drive.volumename
end if
s = s & tabstop & drive.filesystem
s = s & tabstop & drive.totalsize
s = s & tabstop & drive.freespace
s = s & tabstop & drive.availablespace
s = s & tabstop & hex(drive.serialnumber)
end if s = s & newline next generatedriveinformation = send function''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''
' generatefileinformation
' 目的:
' 生成一个字符串,来描述文件的当前状态。
' 示范下面的内容
' - file.path
' - file.name
' - file.type
' - file.datecreated
' - file.datelastaccessed
' - file.datelastmodified
' - file.size
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''function generatefileinformation(file) dim s s = newline & "path:" & tabstop & file.path
s = s & newline & "name:" & tabstop & file.name
s = s & newline & "type:" & tabstop & file.type
s = s & newline & "attribs:" & tabstop & showfileattr(file)
s = s & newline & "created:" & tabstop & file.datecreated
s = s & newline & "accessed:" & tabstop & file.datelastaccessed
s = s & newline & "modified:" & tabstop & file.datelastmodified
s = s & newline & "size" & tabstop & file.size & newline generatefileinformation = send function''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' generatefolderinformation
' 目的:
' 生成一个字符串,来描述文件夹的当前状态。
' 示范下面的内容
' - folder.path
' - folder.name
' - folder.datecreated
' - folder.datelastaccessed
' - folder.datelastmodified
' - folder.size
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''function generatefolderinformation(folder) dim s s = "path:" & tabstop & folder.path
s = s & newline & "name:" & tabstop & folder.name
s = s & newline & "attribs:" & tabstop & showfileattr(folder)
s = s & newline & "created:" & tabstop & folder.datecreated
s = s & newline & "accessed:" & tabstop & folder.datelastaccessed
s = s & newline & "modified:" & tabstop & folder.datelastmodified
s = s & newline & "size:" & tabstop & folder.size & newline generatefolderinformation = send function''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' generateallfolderinformation
' 目的:
' 生成一个字符串,来描述一个文件夹和所有文件及子文件夹的当前状态。
' 示范下面的内容
' - folder.path
' - folder.subfolders
' - folders.count
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''function generateallfolderinformation(folder) dim s
dim subfolders
dim subfolder
dim files
dim file s = "folder:" & tabstop & folder.path & newline & newline
set files = folder.files if 1 = files.count then
s = s & "there is 1 file" & newline
else
s = s & "there are " & files.count & " files" & newline
end if if files.count <> 0 then
for each file in files
s = s & generatefileinformation(file)
next
end if set subfolders = folder.subfolders if 1 = subfolders.count then
s = s & newline & "there is 1 sub folder" & newline & newline
else
s = s & newline & "there are " & subfolders.count & " sub folders" & newline & newline
end if if subfolders.count <> 0 then
for each subfolder in subfolders
s = s & generatefolderinformation(subfolder)
next
s = s & newline
for each subfolder in subfolders
s = s & generateallfolderinformation(subfolder)
next
end if generateallfolderinformation = send function'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''
' generatetestinformation
' 目的:
' 生成一个字符串,来描述 c:\test 文件夹和所有文件及子文件夹的当前状态。
' 示范下面的内容
' - filesystemobject.driveexists
' - filesystemobject.folderexists
' - filesystemobject.getfolder
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''function generatetestinformation(fso) dim testfolder
dim s if not fso.driveexists(testdrive) then exit function
if not fso.folderexists(testfilepath) then exit function set testfolder = fso.getfolder(testfilepath) generatetestinformation = generateallfolderinformation(testfolder) end function'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''
' deletetestdirectory
' 目的:
' 清理 test 目录。
' 示范下面的内容
' - filesystemobject.getfolder
' - filesystemobject.deletefile
' - filesystemobject.deletefolder
' - folder.delete
' - file.delete
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''sub deletetestdirectory(fso) dim testfolder
dim subfolder
dim file
' 有两种方法可用来删除文件: fso.deletefile(testfilepath & "\beatles\octopusgarden.txt") set file = fso.getfile(testfilepath & "\beatles\bathroomwindow.txt")
file.delete ' 有两种方法可用来删除文件夹:
fso.deletefolder(testfilepath & "\beatles")
fso.deletefile(testfilepath & "\readme.txt")
set testfolder = fso.getfolder(testfilepath)
testfolder.deleteend sub'''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''
' createlyrics
' 目的:
' 在文件夹中创建两个文本文件。
' 示范下面的内容
' - filesystemobject.createtextfile
' - textstream.writeline
' - textstream.write
' - textstream.writeblanklines
' - textstream.close
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''sub createlyrics(folder) dim textstream
set textstream = folder.createtextfile("octopusgarden.txt")
textstream.write("octopus' garden ") ' 请注意,该语句不添加换行到文件中。
textstream.writeline("(by ringo starr)")
textstream.writeblanklines(1)
textstream.writeline("i'd like to be under the sea in an octopus' garden in the shade,")
textstream.writeline("he'd let us in, knows where we've been -- in his octopus' garden in the shade.")
textstream.writeblanklines(2)
textstream.close set textstream = folder.createtextfile("bathroomwindow.txt")
textstream.writeline("she came in through the bathroom window (by lennon/mccartney)")
textstream.writeline("")
textstream.writeline("she came in through the bathroom window protected by a silver spoon")
textstream.writeline("but now she sucks her thumb and wanders by the banks of her own lagoon")
textstream.writeblanklines(2)
textstream.closeend sub''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' getlyrics
' 目的:
' 显示 lyrics 文件的内容。
' 示范下面的内容
' - filesystemobject.opentextfile
' - filesystemobject.getfile
' - textstream.readall
' - textstream.close
' - file.openastextstream
' - textstream.atendofstream
' - textstream.readline
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''function getlyrics(fso) dim textstream
dim s
dim file ' 有多种方法可用来打开一个文本文件,和多种方法来从文件读取数据。
' 这儿用了两种方法来打开文件和读取文件: set textstream = fso.opentextfile(testfilepath & "\beatles\octopusgarden.txt", openfileforreading)
s = textstream.readall & newline & newline
textstream.close set file = fso.getfile(testfilepath & "\beatles\bathroomwindow.txt")
set textstream = file.openastextstream(openfileforreading)
do while not textstream.atendofstream
s = s & textstream.readline & newline
loop
textstream.close getlyrics = s
end function'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''
' buildtestdirectory
' 目的:
' 创建一个目录分层结构来示范 filesystemobject。
' 以这样的次序来创建分层结构:
' c:\test
' c:\test\readme.txt
' c:\test\beatles
' c:\test\beatles\octopusgarden.txt
' c:\test\beatles\bathroomwindow.txt
' 示范下面的内容
' - filesystemobject.driveexists
' - filesystemobject.folderexists
' - filesystemobject.createfolder
' - filesystemobject.createtextfile
' - folders.add
' - folder.createtextfile
' - textstream.writeline
' - textstream.close
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''function buildtestdirectory(fso) dim testfolder
dim subfolders
dim subfolder
dim textstream
' 排除(a)驱动器不存在,或(b)要创建的目录已经存在的情况。 if not fso.driveexists(testdrive) then
buildtestdirectory = false
exit function
end if if fso.folderexists(testfilepath) then
buildtestdirectory = false
exit function
end if set testfolder = fso.createfolder(testfilepath) set textstream = fso.createtextfile(testfilepath & "\readme.txt")
textstream.writeline("my song lyrics collection")
textstream.close set subfolders = testfolder.subfolders
set subfolder = subfolders.add("beatles")
createlyrics subfolder
buildtestdirectory = trueend function''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''
' 主程序
' 首先,它创建一个 test 目录,以及一些子文件夹和文件。
' 然后,它转储有关可用磁盘驱动器和 test 目录的某些信息,
' 最后,清除 test 目录及其所有内容。
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''sub main dim fso ' 设立全局变量。
tabstop = chr(9)
newline = chr(10)
set fso = createobject("scripting.filesystemobject") if not buildtestdirectory(fso) then
print "test directory already exists or cannot be created. cannot continue."
exit sub
end if print generatedriveinformation(fso) & newline & newline
print generatetestinformation(fso) & newline & newline
print getlyrics(fso) & newline & newline
deletetestdirectory(fso)end sub