分类: 系统运维
2008-11-20 11:12:22
FSO移动文件,重命名文件
方法:MoveFile
调用格式:
FileSystemObject名.MoveFile 源文件, 目的文件
注意事项:
源文件:必须是已存在的文件,否则会产生“找不到源文件”的错误(错误编码:53)
目的文件:(1)目的文件必须是不存在的文件,否则会产生“文件已存在”的错误(错误编号=58);(2)若目的文件的所在目录与源文件的的所在目录相同,则MoveFile的功能是变更文件名称;若目的文件的所在目录与源文件的所在目录不同,则MoveFile的功能是移动文件。
源码实例:
将与ASP文件同目录下的File1.txt更名为NewFile1.txt。
程序如下:(MoveFile.asp)
<%
'==================================================
'源码:FSO移动文件,重命名文件示例
'时间:2005年12月17日
'==================================================
Set fs = Server.CreateObject("Scripting.FileSystemObject")
SFile = Server.MapPath("File1.txt")
NFile = Server.MapPath("NewFile1.txt")
on Error Resume Next
fs.MoveFile SFile, NFile
If Err.Number = 53 Then
Response.Write File & "文件不存在!"
Response.End
Elseif Err.Number = 58 Then
Response.Write File & "文件已存在!"
Response.End
Elseif Err.Number <> 0 Then
Response.Write "未知错误,错误编码:" & Err.Number
Response.End
Else
Response.Write "成功重命名文件!" & SFile &"为" &NFile& ""
End If
%>
FSO如何重命名文件夹?
如题:FSO如何重命名文件夹?
以下是重命名文件的方法:
<%
Dim Fs,D_File
set Fs=createobject("scripting.filesystemobject")
D_File=server.MapPath("zsp.asp")
if Fs.FileExists(D_file) then
Fs.MoveFile D_File,Server.MapPath("head.asp")
else
Response.Write "产生错误,文件重命名失败!"
End If
set Fs=nothing
%>
把当前目录中的zsp.asp重命名为head.asp
请问那文件夹又如何操作呢,速求!谢谢
<%
Name1=Request("Name1")
Name2=Request("Name2")
if Name1<>"" and Name2<>"" then
Sub RenameFolder(ByVal strFolderPath, ByVal strNewName)
Dim objFileSystem
Set objFileSystem=Server.CreateObject("Scripting.FileSystemObject")
' 如果路径不包含冒号, 则认为 strFolderPath 是虚拟路径.
' 故调用 Server.MapPath() 方法将此路径转为绝对路径
If Instr(1,strFolderPath,":")=-1 Then
strFolderPath=Server.MapPath(strFolderPath)
End If
Dim objFolder
Set objFolder=objFileSystem.GetFolder(strFolderPath)
objFolder.Name=strNewName
Set objFolder=nothing
Set objFileSystem=nothing
End Sub
RenameFolder Server.MapPath(Name1),Name2
response.write ""
end if
%>
====
单一创建,不可创建多级目录
Sub CreateFolder(strFolder)
’首选判断要建立的是否已经存在
Dim strTestFolder, objFSO
strTestFolder = Server.Mappath(strFolder)
Set objFSO = CreateObject("Scripting.FileSystemObject")
’ 检查文件夹是否存在
If objFSO.FolderExists(strTestFolder) Then
Else
’ 建立文件夹
objFSO.CreateFolder(strTestFolder)
Set objFSO = Nothing
End If
End Sub
可创建多级目录,但是路径必需是完整路径,
如:创立C盘下的Tools\baidu 路径必需是C:\Tools\Baidu方可创建成功,比较不方便
代码如下:
Function AutoCreateFolder(strPath)
On Error Resume Next
Dim astrPath, ulngPath, i, strTmpPath
Dim objFSO
If InStr(strPath, "\") <=0 Or InStr(strPath, ":") <= 0 Then
AutoCreateFolder = False
Exit Function
End If
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strPath) Then
AutoCreateFolder = True
Exit Function
End If
astrPath = Split(strPath, "\")
ulngPath = UBound(astrPath)
strTmpPath = ""
For i = 0 To ulngPath
strTmpPath = strTmpPath & astrPath(i) & "\"
If Not objFSO.FolderExists(strTmpPath) Then
’ 创建
objFSO.CreateFolder(strTmpPath)
End If
Next
Set objFSO = Nothing
If Err = 0 Then
AutoCreateFolder = True
Else
AutoCreateFolder = False
End If
End Function
结合上面的我写的代码更优于他们的
以本文件判断,实际物理路径,无需加盘符,斜杠可\也可/统一按/执行
<%
’*************************************
’过 程 名: AutoMaticCreation(strPath)
’参 数:strPath ---上传路径可以为根目录/Dir/或上级目录../Dir/也可同级目录Dir/
’备注说明:
’ 此过程由WebAdmin结合上的程序修改而成转载请保留些信息
’ 我的:
’*************************************
Function AutoMaticCreation(strPath)
On Error Resume Next
Dim astrPath, ulngPath, i, strTmpPath
Dim objFSO
strPath = Server.Mappath(strPath)
strPath=Replace(strPath, "\", "/")
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strPath) Then
AutoCreateFolder = True
Exit Function
End If
astrPath = Split(strPath, "/")
ulngPath = UBound(astrPath)
strTmpPath = ""
For i = 0 To ulngPath
strTmpPath = strTmpPath & astrPath(i) & "/"
If Not objFSO.FolderExists(strTmpPath) Then
objFSO.CreateFolder(strTmpPath)
End If
Next
Set objFSO = Nothing
End Function
%>