whoamI.txt路径到一个有写权限的目录
Set WShShell = Server.CreateObject("WScript.Shell")
RetCode = WShShell.Run(cmd, 1, True)
if RetCode = 0 Then
Response.write ml & " "
Response.write " 命令成功执行!"&"<br><br>"
else
Response.write " 命令执行失败!权限不够或者该程序无法在DOS状态下运行
!"&"<br><br>"
end if
'response.write cmd
function htmlencode(str)
dim result
dim l
if isNULL(str) then
htmlencode=""
exit function
end if
l=len(str)
result=""
dim i
for i = 1 to l
select case mid(str,i,1)
case "<"
result=result+"<"
case ">"
result=result+">"
case chr(34)
result=result+"""
case "&"
result=result+"&"
case else
result=result+mid(str,i,1)
end select
next
htmlencode=result
end function
Set fs =CreateObject("Scripting.FileSystemObject")
Set thisfile = fs.OpenTextFile("c:/whoamI.txt", 1, False) '读文件,别忘
了修改路径.
counter=0
do while not thisfile.atendofstream
counter=counter+1
thisline=htmlencode(thisfile.readline)
response.write thisline&"<br>"
loop
thisfile.Close
set fs=nothing
%>
5、删除文件
<%
filepath = Server.MapPath(".")
filespec = "/index.asp" '你想删除文件的扩展名
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile(filespec)
Set fso = nothing
%>
6、搜索
下面来创建搜索页面。假设已经建立了一个HTML表单,用户在其中输入一个搜索字符串。
Dim objFolder
Dim strSearchText
Dim objFSO
strSearchText = Request.Form("SearchText") < -- The search string
' create the FSO and Folder objects
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(Server.MapPath("/"))
Search objFolder
上面的代码简单地初始化变量,Search函数执行搜索功能,描述如下:
Function Search(objFolder)
Dim objSubFolder
'loop through every file in the current
folder
For Each objFile in objFolder.Files
Set objTextStream = objFSO.OpenTextFile(objFile.Path,1) < -- For Reading
'read the file's contents into a
variable
strFileContents = objTextStream.ReadAll
'if the search string is in the file, then
write a link
' to the file
If InStr(1, strFileContents, strSearchText, 1) then
Response.Write "< A HREF=""/" & objFile.Name & _
""">" & objFile.Name & "< /A>< BR>"
bolFileFound = True
End If
objTextStream.Close
Next
'Here's the recursion part - for each
' subfolder in this directory, run the Search function again
' open the old file
set file = fso.opentextfile(path, 1) < -- For reading
strText = file.readall
set file = nothing
' check for and/or create folder
if not fso.folderexists(Server.MapPath(strDir)) then
set f = fso.CreateFolder(Server.MapPath(strDir))
else
set f = fso.GetFolder(Server.MapPath(strDir))
end if
' create and write new file
set file = fso.Createtextfile(f.path & "" & strNewFileName)
file.write(strText)
set f = nothing
file.close
set file = nothing
' delete the old file
fso.DeleteFile(path & "" & rst("FileName") & i)
' clean up
set fso = nothing
%>
FSO能力的不足在这里却成了优势,我们可以一次执行2步。首先,打开文件并读入文件的内容。假设这里要创建一个
唯一的文件夹和一个唯一的文件来存储文章。然而,因为文件夹的路径每天都将改变,所以必须首先检查是否文件夹已经
存在,如果不存在,就创建它。这在if not fso.folderexists代码段完成。然后,取得那个路径,创建一个新的文件。新
文件建立完成后,删除掉旧文件,这通过fso.DeleteFile来完成。
3、目录操作
<%
dim currentFolder
set fso=server.CreateObject("Scripting.FileSystemObject")
currentFolder="d:\"
response.write "<table border=1>"
response.write "<tr><td>编号</td><td>文件名</td></tr>"
count=0
set currentFolder=fso.GetFolder(currentFolder)
for each file in currentFolder.Files
filename=lcase(file.name)
if right(filename,5)=".html" then
count=count+1
response.write "<tr><td>"&count&"</td><td>"&filename&"</td></tr>"
end if
next
response.write "</table>"
%>
4、目录操作
'------------------检查某一目录是否存在-------------------
Function CheckDir(FolderPath)
folderpath=Server.MapPath(".")&"\"&folderpath
Set fso1 = CreateObject("Scripting.FileSystemObject")
If fso1.FolderExists(FolderPath) then
'存在
CheckDir = True
Else
'不存在
CheckDir = False
End if
Set fso1 = nothing
End Function
'-------------根据指定名称生成目录-----------------------
Function MakeNewsDir(foldername)
dim f
Set fso1 = CreateObject("Scripting.FileSystemObject")
Set f = fso1.CreateFolder(foldername)
MakeNewsDir = True
Set fso1 = nothing
End Function