【问题标题】:How to Copy HTML file along with all the associated images and scripts folder?如何复制 HTML 文件以及所有相关的图像和脚本文件夹?
【发布时间】:2011-07-20 08:37:16
【问题描述】:

我想使用 VB.net 将 HTML 文件从一个位置复制到另一个位置。
当我使用三个 FileCopy、System.IO.File.Copy、My.Computer.FileSystem.CopyFile 中的任何一个时 它只复制文件,而不是包含相关图像和脚本的“filename_files”文件夹。

我想要以编程方式将 a.html 作为 b.html 复制到另一个位置

当我这样做并打开 b.html 时,它会在没有任何图像和脚本的情况下打开它。

请帮忙

【问题讨论】:

  • 这需要手动将每个文件从源位置复制到目标位置,并且在开始复制位于源位置文件夹内的文件之前,您需要在目标位置创建同名文件夹位置,然后开始复制文件。
  • 但这也意味着我必须更改 HTML 文件的内容,因为它具有 src 标记并且路径是“oldfilename_files”文件夹。该怎么做?
  • 只要您将在目标位置创建的文件夹名称与源位置相同,您就不必更改 html 文件的内容,请记住您已经给出了相对路径在src 而不是绝对的。

标签: vb.net file-io file-copying


【解决方案1】:

您可以使用以下两种方法共同复制包含脚本和图像的文件夹,从而使用内置方法FileCopy复制您的HTML文件,并使用以下方法复制您所需的文件夹。

我找到了第一个方法,它返回给定路径 here 中的文件数组

Public Function FileList(Mask As String) As String()

    Dim sWkg As String
    Dim sAns() As String
    Dim lCtr As Long

    ReDim sAns(0) As String
    sWkg = Dir(Mask, vbNormal)

    Do While Len(sWkg)

        If sAns(0) = "" Then
            sAns(0) = sWkg
        Else
            lCtr = UBound(sAns) + 1
            ReDim Preserve sAns(lCtr) As String
            sAns(lCtr) = sWkg
        End If
        sWkg = Dir
   Loop
   FileList = sAns
End Function

现在使用上述方法和以下方法,您可以通过指定源和目标路径来复制文件夹。该方法将返回布尔值,指定文件夹是否被复制。

Public Function FolderCopy(ByVal SourceFolder As String, ByVal TargetFolder As String) As Boolean
    Dim flist() As String
    Dim sURL As String = New String(SourceFolder)
    Dim tURL As String = New String(TargetFolder)
    Dim i As Integer
    Dim slashpos As Long
    If Not Directory.Exists(tURL) Then

        slashpos = InStrRev(sURL, "\") 'Get position of last occurrence if '\' in given path
        If slashpos <> sURL.Length Then 'Check if URL does not have slash at its end
            sURL = sURL & "\" 'Add slash at URL end
        End If

        flist = FileList(sURL)
        slashpos = InStrRev(tURL, "\") 'Get position of last occurrence if '\' in given path
        If slashpos = tURL.Length Then
            tURL = tURL.Substring(0, tURL.Length - 1)
        End If
        slashpos = InStrRev(tURL, "\")

        Try
            Directory.CreateDirectory(tURL)
            For i = 0 To flist.Length - 1
                FileCopy(sURL & flist(i), tURL & "\" & flist(i))
            Next
            FolderCopy = True
        Catch ex As Exception
            FolderCopy = False
        End Try

    Else
        FolderCopy = False
    End If
End Function

确保在使用FolderCopy 方法之前在类的开头包含Imports System.IO,并注意这两个方法都需要包含。

【讨论】:

    【解决方案2】:
    ' copy all files and subdirectories from the
    ' specified source to the specified destination.
    Private Sub RecursiveCopyFiles( ByVal sourceDir As String, ByVal destDir As String, _
    ByVal fRecursive As Boolean)
    
        Dim i As Integer
        Dim posSep As Integer
        Dim sDir As String
        Dim aDirs() As String
        Dim sFile As String
        Dim aFiles() As String
    
        ' Add trailing separators to the supplied paths if they don't exist.
        If Not sourceDir.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString()) Then 
             sourceDir &= System.IO.Path.DirectorySeparatorChar
        End If
    
        If Not destDir.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString()) Then
             destDir &= System.IO.Path.DirectorySeparatorChar
        End If
    
        ' Recursive switch to continue drilling down into dir structure.
        If fRecursive Then
            ' Get a list of directories from the current parent.
            aDirs = System.IO.Directory.GetDirectories(sourceDir)
    
            For i = 0 To aDirs.GetUpperBound(0)
                ' Get the position of the last separator in the current path.
                posSep = aDirs(i).LastIndexOf("\")
    
                ' Get the path of the source directory.
                sDir = aDirs(i).Substring((posSep + 1), aDirs(i).Length -(posSep + 1))
    
                ' Create the new directory in the destination directory.
                System.IO.Directory.CreateDirectory(destDir + sDir)
    
                ' Since we are in recursive mode, copy the children also
                RecursiveCopyFiles(aDirs(i), (destDir + sDir), fRecursive)
            Next
        End If
    
        ' Get the files from the current parent.
        aFiles = System.IO.Directory.GetFiles(sourceDir)
    
        ' Copy all files.
        For i = 0 To aFiles.GetUpperBound(0)
            ' Get the position of the trailing separator.
            posSep = aFiles(i).LastIndexOf("\")
    
            ' Get the full path of the source file.
            sFile = aFiles(i).Substring((posSep + 1), aFiles(i).Length - (posSep+ 1))
    
            ' Copy the file.
            System.IO.File.Copy(aFiles(i), destDir + sFile)
        Next i
    End Sub
    

    【讨论】:

    • 它是递归函数,所以先了解递归
    猜你喜欢
    • 2010-11-07
    • 1970-01-01
    • 1970-01-01
    • 2012-04-01
    • 1970-01-01
    • 2011-07-19
    • 2017-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多