1

我有一个旧的 VBScript 函数,可以从 URL 将文件副本保存在服务器上。如果文件已经存在,该函数会删除以前的版本并重写一个新的文件版本。问题是我需要插入区分大小写的文件名。例如,文件名“Test.html”和“test.html”应该保存为不同的副本,我的函数只是替换它们。有什么建议吗?

功能:

Public Function SaveToChache(Url, SaveToFolder, FileName)
    Dim ChacheFolder: ChacheFolder = SaveToFolder 'Folder where will the cache files be   stored (include trailing slash)
    Dim FilePath: FilePath = Server.MapPath(ChacheFolder & FileName)
     Dim objXMLHTTP: Set objXMLHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
     objXMLHTTP.open "GET", Url, false
     objXMLHTTP.send()
     If objXMLHTTP.Status = 200 Then 
         Dim objADOStream: Set objADOStream = CreateObject("ADODB.Stream")
         objADOStream.Open
         objADOStream.Type = 1 'adTypeBinary
         objADOStream.Write objXMLHTTP.ResponseBody
         objADOStream.Position = 0 'Set the stream position to the start
         Dim objFSO: Set objFSO = Createobject("Scripting.FileSystemObject")
        If objFSO.FileExists(FilePath) Then objFSO.DeleteFile FilePath
        Set objFSO = Nothing
        objADOStream.SaveToFile FilePath
        objADOStream.Close
        Set objADOStream = Nothing
        SaveToChache = objXMLHTTP.getResponseHeader("Content-Type")
    Else
        SaveToChache = ""
    End if
    Set objXMLHTTP = Nothing
End Function

调用函数:

savefile = SaveToChache("http://www.example.com", "/cache/", "Test.html")

谢谢!

4

1 回答 1

0

我会使用直接比较而不是objFSO.FileExists.

例如:

Dim objFSO: Set objFSO = Createobject("Scripting.FileSystemObject")
FilePath = "C:\Test\test.txt"

'Get path to file
strParentPath = objFSO.GetFile(FilePath).ParentFolder
'Get each file in the folder
Set objCheck = objFSO.GetFolder(strParentPath).Files

For Each x In objCheck
    If x = FilePath Then objFSO.DeleteFile(FilePath)
Next 

基本上,如果大小写也相同,x 只会等于 FilePath。

于 2013-10-07T18:48:08.280 回答