<p>常用的fso操作通用函数</p> <p><br /> 以下是几则在ASP中经常应用到FSO操作函数,实用精典,推荐收藏!<br /> &lt;% <br /> '功能:判断文件名是否合法 <br /> 'isFileName [filename] <br /> '文件名不能包含下列任何字符之一 <br /> ' \ / : * ? &quot; &lt; &gt; | <br /> Function isFileName(sFileName) <br /> Dim sErrorStr, i <br /> isFileName = TRUE <br /> sErrorStr = Array(&quot;\&quot;, &quot;/&quot;, &quot;:&quot;, &quot;*&quot;, &quot;?&quot;, &quot;&quot;&quot;&quot;, &quot;&lt;&quot;, &quot;&gt;&quot;, &quot;|&quot;) <br /> If Len(sFileName &amp; &quot;&quot;) = 0 Then isFileName = FALSE : Exit Function <br /> For i = 0 To 8 <br /> If InStr(sFileName, sErrorStr(i)) &gt; 0 Then <br /> isFileName = FALSE <br /> End If <br /> Next <br /> End Function <br /> %&gt;<br /> &lt;% <br /> '功能:删除一个目录。除目录本身外,还将删除指定目录下的所有子目录和文件。用于删除目录树。 <br /> 'RD [Drive:]Path <br /> '支持删除多级目录,支持相对路径和绝对路径。 <br /> '支持用&ldquo;...&rdquo;指定父目录的父目录。 <br /> ''需要PATH函数在下面<br /> Function RD(ByVal sPath) <br /> On Error Resume Next <br /> Dim oFSO <br /> sPath = Path(sPath) '//此处需要PATH函数 <br /> Set oFSO = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;) <br /> If oFSO.FolderExists(sPath) Then <br /> oFSO.DeleteFolder sPath, True <br /> RD = True <br /> End If <br /> Set oFSO = Nothing <br /> If Err.Number &gt; 0 Then <br /> Err.Clear() <br /> RD = False <br /> Else <br /> RD = True <br /> End If <br /> End Function <br /> %&gt;<br /> &lt;% <br /> '功能:创建目录。 <br /> 'MD [Drive:]Path <br /> '支持创建多级目录,支持相对路径和绝对路径。 <br /> '支持用&ldquo;...&rdquo;指定父目录的父目录。</p> <p>'需要PATH函数在下面<br /> Function MD(sPath) <br /> On Error Resume Next <br /> Dim aPath, iPath, i, sTmpPath <br /> Dim oFSO <br /> sPath = Path(sPath) '//此处需要PATH函数 <br /> Set oFSO = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;) <br /> If oFSO.FolderExists(sPath) Then MD = True : Exit Function <br /> aPath = Split(sPath, &quot;\&quot;) <br /> iPath = UBound(aPath) <br /> sTmpPath = &quot;&quot; <br /> For i = 0 To iPath <br /> sTmpPath = sTmpPath &amp; aPath(i) &amp; &quot;\&quot; <br /> If Not oFSO.FolderExists(sTmpPath) Then <br /> oFSO.CreateFolder(sTmpPath) <br /> End If <br /> Next <br /> Set oFSO = Nothing <br /> If Err.Number &gt; 0 Then <br /> Err.Clear() <br /> MD = False <br /> Else <br /> MD = True <br /> End If <br /> End Function <br /> %&gt;<br /> &lt;% <br /> '功能:计算目录绝对路径。 <br /> 'PATH [Drive:]Path <br /> '支持多级目录,支持相对路径和绝对路径。 <br /> '支持用&ldquo;...&rdquo;指定父目录的父目录。</p> <p><br /> Function Path(ByVal sPath) <br /> On Error Resume Next <br /> If Len(sPath&amp;&quot;&quot;) = 0 Then sPath = &quot;./&quot; <br /> If Right(sPath, 1) = &quot;:&quot; Then sPath = sPath &amp; &quot;\&quot; <br /> sPath = Replace(sPath, &quot;/&quot;, &quot;\&quot;) <br /> sPath = ReplaceAll(sPath, &quot;\\&quot;, &quot;\&quot;, False) <br /> sPath = ReplaceAll(sPath, &quot;...&quot;, &quot;..\..&quot;, False) <br /> If (InStr(sPath, &quot;:&quot;) &gt; 0) Then <br /> sPath = sPath <br /> Else <br /> sPath = Server.Mappath(sPath) <br /> End If <br /> Path = sPath <br /> End Function <br /> %&gt;</p> <p>&lt;% <br /> '功能:判断文件是否已存在。 <br /> 'IsFileExist(文件名)</p> <p><br /> Public Function IsFileExist(ByVal sFileName) <br /> On Error Resume Next <br /> Dim oFSO <br /> sFileName = PATH(sFileName) <br /> Set oFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;) <br /> IsFileExist = oFSO.FileExists(sFileName) <br /> Set oFSO = Nothing <br /> End Function <br /> %&gt;<br /> &lt;% <br /> '功能:判断文件夹是否已存在。 <br /> 'IsFolderExist(文件名)</p> <p>Public Function IsFolderExist(ByVal sFolderName) <br /> On Error Resume Next <br /> Dim oFSO <br /> sFolderName = PATH(sFolderName) <br /> Set oFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;) <br /> IsFolderExist = oFSO.FolderExists(sFolderName) <br /> Set oFSO = Nothing <br /> End Function <br /> %&gt;<br /> &lt;% <br /> '功能:创建十进制文本文件。 <br /> 'CreateTextFile(文件内容,文件名) <br /> '文件名支持相对路径和绝对路径。 <br /> '支持用&ldquo;...&rdquo;指定父目录的父目录。</p> <p>Function CreateTextFile (ByVal sText, ByVal sFileName) <br /> On Error Resume Next <br /> sFileName = Path(sFileName) <br /> Set oFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;) <br /> Set oWrite = oFSO.OpenTextFile(sFileName, 2, True) <br /> oWrite.Write sText <br /> oWrite.Close <br /> Set oFSO = Nothing <br /> Set oWrite = Nothing <br /> If Err.Number &gt; 0 Then <br /> Err.Clear() <br /> CreateTextFile = False <br /> Else <br /> CreateTextFile = True <br /> End If <br /> End Function <br /> %&gt;</p>
T:0.007092s,M:252 KB
返回顶部 留言