Discuz教程网

ASP FSO文件操作函数代码(复制文件、重命名文件、删除文件、替换字符串)

[复制链接]
authicon dly 发表于 2011-9-8 20:16:38 | 显示全部楼层 |阅读模式
FSO文件(File)对象属性
DateCreated 返回该文件夹的创建日期和时间
DateLastAccessed 返回最后一次访问该文件的日期和时间
DateLastModified 返回最后一次修改该文件的日期和时间
Drive 返回该文件所在的驱动器的Drive对象
Name 设定或返回文件的名字
ParentFolder 返回该文件的父文件夹的Folder对象
Path 返回文件的绝对路径,可使用长文件名
ShortName 返回DOS风格的8.3形式的文件名
ShortPath 返回DOS风格的8.3形式的文件绝对路径
Size 返回该文件的大小(字节)
Type 如果可能,返回一个文件类型的说明字符串
FSO文件(File)对象方法
FSO文件对象方法 用途
CopyFile 拷贝一个或者多个文件到新路径
CreateTextFile 创建文件并且返回一个TextStream对象
DeleteFile 删除一个文件
OpenTextFile 打开文件并且返回TextStream对象,以便读取或者追加

重命名文件:
  1. Function reName(sourceName,destName)
  2. dim oFso,oFile
  3. set oFso=server.createobject("Scripting.FileSystemObject")
  4. set oFile=oFso.getFile(Server.mappath(sourceName))
  5. oFile.Name=destName
  6. Set oFso=Nothing
  7. Set oFile=Nothing
  8. End Function
复制代码

删除文件:
  1. Function FSOdel(fileName)
  2. dim fso,f
  3. set fso = server.CreateObject("scripting.filesystemobject")
  4. f=server.MapPath(fileName)
  5. if fso.FileExists(f) then
  6. fso.DeleteFile f,true
  7. end if
  8. set f = nothing
  9. set fso = nothing
  10. End Function
复制代码

替换文件中的字符串:
  1. Function FSOreplace(fileName,Target,repString)
  2. Dim objFSO,objCountFile,FiletempData
  3. Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
  4. Set objCountFile = objFSO.OpenTextFile(Server.MapPath(fileName),1,True)
  5. FiletempData = objCountFile.ReadAll
  6. objCountFile.Close
  7. FiletempData=Replace(FiletempData,Target,repString)
  8. Set objCountFile=objFSO.CreateTextFile(Server.MapPath(fileName),True)
  9. objCountFile.Write FiletempData
  10. objCountFile.Close
  11. Set objCountFile=Nothing
  12. Set objFSO = Nothing
  13. End Function

  14. <%

  15. '*******************************************************
  16. '函数名:CreateFolder(sPath)
  17. '作 用: 创建目录
  18. '参 数:sPath : 创建的相对目录路径
  19. '返回值:成功 true , 失败 false
  20. '*******************************************************
  21. 'response.Write createfolder("/dgsunshine/UploadFile/demo1/")
  22. Function CreateFolder(sPath)
  23. On Error Resume Next

  24. Dim Fso,Arrfolder,Folder,i,j

  25. If sPath="" then
  26. CreateFolder = False
  27. Exit Function
  28. End If

  29. If Left(sPath,1) = "/" Then
  30. Folder = "/"
  31. sPath = Mid(sPath,2,Len(sPath))
  32. Else
  33. Folder = "./"
  34. End If

  35. if Right(sPath,1) = "/" then sPath = Left(sPath,Len(sPath)-1)

  36. ArrFolder = Split(sPath,"/")

  37. Set Fso = Server.CreateObject("Scripting.FileSystemObject")

  38. For i = 0 To Ubound(ArrFolder)
  39. If i = 0 then
  40. Folder = Folder & ArrFolder(i) & "/"
  41. Else
  42. Folder = Folder & ArrFolder(i) & "/"
  43. End If

  44. If Fso.folderExists(Server.MapPath(Folder)) = False then
  45. response.Write server.MapPath(folder)
  46. Fso.createFolder(Server.MapPath(Folder))
  47. End If

  48. Next
  49. Set Fso = nothing

  50. If Err.Number <> 0 then
  51. Err.clear()
  52. CreateFolder = False
  53. Else
  54. CreateFolder = True
  55. End If
  56. End function


  57. Function getFile(paramFilePath)
  58. Set Fso = Server.CreateObject("Scripting.FileSystemObject")
  59. Set Fso_Read = fso.OpenTextFile(Server.MapPath(paramFilePath),1,false,-2)
  60. getFile = Fso_Read.readall
  61. Set Fso_Read = Nothing
  62. Set Fso = Nothing
  63. End Function

  64. '*******************************************************
  65. '函数名:CreateFile(paramFileContent,paramFilePath)
  66. '作 用: 创建文件
  67. '参 数:paramFileContent '文件的内容
  68. ' paramFilePath '文件名(不包括路径)
  69. '返回值:成功 true , 失败 false
  70. '*******************************************************
  71. Function CreateFile(paramFileContent,paramFilePath)
  72. On Error Resume Next
  73. Dim Fso,fWrite

  74. Set Fso = Server.CreateObject("Scripting.FileSystemObject")
  75. Set fWrite = Fso.CreateTextFile(Server.Mappath(paramFilePath),true)

  76. fWrite.write paramFileContent
  77. fWrite.close()
  78. Set fWrite = nothing
  79. Set Fso = nothing

  80. If Err.number <> 0 Then
  81. Err.clear()
  82. CreateFile = False
  83. Else
  84. CreateFile = True
  85. End If
  86. End Function

  87. '*******************************************************
  88. '函数名:DelFile(FilePath)
  89. '作 用: 删除文件
  90. '参 数:FilePath '文件路径 多个文件用"|"隔开
  91. '返回值:成功 true , 失败 false
  92. '*******************************************************
  93. Function DelFile(FilePath)
  94. On Error Resume Next
  95. Dim fso,arrFile,i

  96. If GetSafeStr(FilePath,"")="" then
  97. CreateFolder = false
  98. Exit Function
  99. End If

  100. arrFile = Split(FilePath,"|")
  101. Set Fso = Server.CreateObject("Scripting.FileSystemObject")

  102. for i=0 to UBound(arrFile)
  103. FilePath = arrFile(i)
  104. If Fso.FileExists(Server.MapPath(FilePath)) then
  105. Fso.DeleteFile Server.MapPath(FilePath)
  106. End If
  107. Next
  108. Set fso = nothing

  109. If Err then
  110. Err.clear()
  111. DelFile = false
  112. Else
  113. DelFile = true
  114. End If
  115. End Function

  116. '*******************************************************
  117. '函数名:DelFolder(FolderPath)
  118. '作 用: 删除目录
  119. '参 数:FolderPath '目录路径 '多个目录用"|"分隔
  120. '返回值:成功 true , 失败 false
  121. '*******************************************************
  122. Function DelFolder(FolderPath)
  123. On Error Resume Next
  124. Dim Fso,arrFolder,i

  125. If GetSafeStr(FolderPath,"")="" then
  126. DelFolder = false
  127. Exit Function
  128. End If

  129. arrFolder = Split(FolderPath,"|")
  130. Set Fso = Server.CreateObject("Scripting.FileSystemObject")

  131. For i=0 to UBound(arrFolder)
  132. FolderPath = arrFolder(i)
  133. If Fso.folderexists(Server.MapPath(FolderPath)) then
  134. Fso.deleteFolder Server.MapPath(FolderPath)
  135. End If
  136. Next

  137. If Err then
  138. Err.clear()
  139. DelFolder = false
  140. 'ShowError "删除目录失败",""
  141. else
  142. DelFolder = true
  143. End If
  144. End Function


  145. '*******************************************************
  146. '函数名:IsExistFile(FilePath)
  147. '作 用: 判断文件或目录是否存在
  148. '参 数:FilePath '文件路径 多个文件用"|"隔开
  149. '返回值:成功 true , 失败 false
  150. '*******************************************************
  151. Function IsExistFile(FilePath)
  152. On Error Resume Next
  153. Dim fso,arrFile,i

  154. If GetSafeStr(FilePath,"")="" then
  155. IsExistFile = false
  156. End If

  157. arrFile = Split(FilePath,"|")
  158. Set Fso = Server.CreateObject("Scripting.FileSystemObject")

  159. for i=0 to UBound(arrFile)
  160. FilePath = arrFile(i)
  161. If Fso.FileExists(Server.MapPath(FilePath)) then
  162. IsExistFile = True
  163. End If
  164. If Fso.folderexists(Server.MapPath(FilePath)) then
  165. IsExistFile = True
  166. End If
  167. Next
  168. Set fso = nothing

  169. If Err then
  170. Err.clear()
  171. IsExistFile = false
  172. 'ShowError "判断文件或目录是否存在失败",""
  173. else
  174. IsExistFile = true
  175. End If
  176. End Function


  177. '*******************************************************
  178. '函数名:DelFile(FilePath)
  179. '作 用: 删除文件或目录
  180. '参 数:FilePath '文件路径 多个文件用"|"隔开
  181. '返回值:成功 true , 失败 false
  182. '*******************************************************
  183. Function DelFile(FilePath)
  184. On Error Resume Next
  185. Dim fso,arrFile,i

  186. If GetSafeStr(FilePath,"")="" then
  187. CreateFolder = false
  188. End If

  189. arrFile = Split(FilePath,"|")
  190. Set Fso = Server.CreateObject("Scripting.FileSystemObject")

  191. for i=0 to UBound(arrFile)
  192. FilePath = arrFile(i)
  193. If Fso.FileExists(Server.MapPath(FilePath)) then
  194. Fso.DeleteFile Server.MapPath(FilePath)
  195. End If
  196. If Fso.folderexists(Server.MapPath(FilePath)) then
  197. Fso.deleteFolder Server.MapPath(FilePath)
  198. End If
  199. Next
  200. Set fso = nothing

  201. If Err then
  202. Err.clear()
  203. DelFile = false
  204. 'ShowError "删除文件或目录失败",""
  205. else
  206. DelFile = true
  207. End If
  208. End Function


  209. '*******************************************************
  210. '函数名:ReNameFile((oldName,newName)
  211. '作 用: 重命名文件或目录
  212. '参 数:strOldName '原文件名 多个用"|"隔开
  213. ' strNewName '新文件名 多个用"|"隔开
  214. ' 上面两个参数请保持一致
  215. '返回值:成功 true , 失败 false
  216. '*******************************************************
  217. Function ReNameFile(strOldName,strNewName)
  218. On Error Resume Next
  219. Dim fso,arrOld,arrNew,i,oldName,newName

  220. old = GetSafeStr(strOldName,"")
  221. Newfile = GetSafeStr(strNewName,"")

  222. If old ="" or Newfile = "" then
  223. ReNameFile = false
  224. Exit Function
  225. End If

  226. arrOld = Split(strOldName,"|")
  227. arrNew = Split(strNewName,"|")

  228. If UBound(arrOld)<> UBound(arrNew) then
  229. ReNameFile = false
  230. Exit Function
  231. End If

  232. Set Fso = Server.CreateObject("Scripting.FileSystemObject")
  233. for i=0 to UBound(arrOld)
  234. oldName = Server.MapPath(arrOld(i))
  235. newName = Server.MapPath(arrNew(i))
  236. If Fso.FileExists(oldName) and not Fso.FileExists(newName) then
  237. fso.MoveFile oldName,newName
  238. 'ReNameFile = True
  239. End If
  240. Next
  241. Set fso = nothing

  242. If Err.Number <> 0 Then
  243. Err.clear()
  244. ReNameFile = false
  245. Else
  246. ReNameFile = True
  247. End If
  248. End Function


  249. '*******************************************************
  250. '函数名:CopyFiles((TempSource,TempEnd)
  251. '作 用: 复制文件或者目录
  252. '参 数:TempSource '源文件名 多个用"|"隔开
  253. ' TempEnd '目的文件名 多个用"|"隔开
  254. ' 注意:上面两个参数请保持一致,并且都为完整路径,
  255. ' 已经经过Server.MapPath方法处理过
  256. '返回值:成功 true , 失败 false
  257. '*******************************************************
  258. Function CopyFiles(TempSource,TempEnd)
  259. On Error Resume Next
  260. Dim CopyFSO,arrSource,arrEnd

  261. CopyFiles = false
  262. Set CopyFSO = Server.CreateObject("Scripting.FileSystemObject")

  263. If TempSource ="" or TempEnd = "" then
  264. ErrRaise "复制文件或目录","条件为空"
  265. CopyFiles = false
  266. Exit Function
  267. End If

  268. arrSource = Split(TempSource,"|")
  269. arrEnd = Split(TempEnd,"|")
  270. If UBound(arrSource) <> UBound(arrEnd) then
  271. CopyFiles= false
  272. Exit Function
  273. End If

  274. for i=0 to UBound(arrSource)
  275. srcName = arrSource(i)
  276. tarName = arrEnd(i)
  277. IF CopyFSO.FileExists(srcName) and not CopyFSO.FileExists(tarName) then
  278. CopyFSO.CopyFile srcName,tarName
  279. CopyFiles = true
  280. End If

  281. IF CopyFSO.FolderExists(srcName) and not CopyFSO.FolderExists(tarName)then
  282. CopyFSO.CopyFolder srcName,tarName
  283. CopyFiles = true
  284. End If
  285. Next
  286. Set CopyFSO = Nothing

  287. If Err then
  288. 'Err.clear()
  289. CopyFiles = false
  290. End If
  291. End Function
  292. %>
复制代码



上一篇:asp文本框换行显示代码
下一篇:ASP页面静态化批量生成代码分享(多种方法)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-8-2 14:57

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表