0

我需要加入任何文件(2GB)而不阅读它们的内容。我曾尝试使用 CopyFile 方法,但它不起作用。

我的代码是:

Public Function UnificarCRIs(ByVal path, ByVal FICRIEC, ByVal sessio, ByVal CIBAA)
Dim objFile, objCurrentFolder, filesys, origenFitxers
Dim FileName, WshShell

On error resume next

Set filesys = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objCurrentFolder = filesys.getFolder(path)
origenFitxers = " "

For Each objFile In objCurrentFolder.Files
    FileName = objFile
    If (right(FileName, 4) = ".cri") Then
        origenFitxers = FileName
        'Wscript.Echo FileName
        If filesys.FileExists(path & FICRIEC & sessio) Then
            'Wscript.Echo "If"
            Wscript.Echo path & FICRIEC & sessio & "+" & FileName
            filesys.CopyFile path & FICRIEC & sessio & "+" & FileName, path & FICRIEC & sessio 
             'WshShell.Run ("copy " & path & FICRIEC & sessio & "+" & FileName & " " & path & FICRIEC & sessio & "_tmp")
            'filesys.DeleteFile path & FICRIEC & sessio
            'filesys.MoveFile path & FICRIEC & sessio & "_tmp", path & FICRIEC & sessio
        Else
            Wscript.Echo "Else"
            WshShell.Run ("copy " & FileName & " " & path & FICRIEC & sessio)
            'filesys.CopyFile FileName,path & FICRIEC & sessio
        End If  
    End If 
Next

End Function

有什么方法可以使用 Vbscript 连接两个文件吗?

谢谢

4

3 回答 3

2

要加入两个文件,“某人”必须读取(和写入)两个文件的内容。这个“某人”可能是copy [/B] f1 + f2 f3。因此,请使用您的循环来构建正确的文件规范和WshShell.Run/.Execsuitabe 命令。

于 2013-02-13T16:22:14.453 回答
1

取决于您可以访问哪些 COM 对象以及代码在何处运行。

1) 如果您有权访问 Shell,则使用 DOS 提示符的复制命令。此示例显示了 Dir 命令的执行,但它是相同的技术。

Dim oShell    
Set oShell = WScript.CreateObject ("WScript.Shell")

' Note the True value means wait to complete...
' And the 0 value means do not display any window...

oShell.run "cmd /K CD C:\ & Dir", 0, True  

Set oShell = Nothing

也许还可以看看这里http://ss64.com/vb/shellexecute.html。不知道这个方法有没有帮助。

2)如果你没有shell,那么如果你可以制作COM对象,那么用C++或Delphi或VB6等制作一个。然后使用该COM对象执行DOS命令进行合并。

3)否则,您将不得不从文件中读取数据。如果是文本文件,那么这很容易,因为在 Vbs 中有简单的命令可以做到这一点。如果是二进制数据,则需要更多的工作来获取二进制数据并使用“LenB”和“AscB”样式函数来访问 Unicode 的原始字节。但你真的不想那样做。任何 ASP 的上传处理脚本都应该向您展示使用字符串中的原始字节的技术。

于 2013-02-13T16:28:38.353 回答
1

您可以将 VBScript 与 Windows Copy 命令结合使用来连接文件。您可以使用 Copy here https://support.microsoft.com/en-us/kb/71161查看有关附加二进制文件的文档

这是该技术的示例:

JoinFiles "c:\test\", "c:\test\mergedfile.cri"

Function JoinFiles (inPath, outPath)
    Dim objShell, objFSO

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objShell = WScript.CreateObject("WScript.Shell")

    Dim strFilenames, objFile, intFilecount, intExitCode

    strFilenames = ""
    intFilecount = 0
    intExitCode = 0

    ' If the input folder exists, proceed to listing the files
    If objFSO.FolderExists (inPath) Then
        ' List the files in the folder and join the files which has cri extension
        For Each objFile In objFSO.GetFolder(inPath).Files
            If LCase (objFSO.GetExtensionName (objFile.Path)) = "cri" Then
                intFilecount = intFilecount+1
                strFilenames = strFilenames & """" & objFile.Path & """ + "
            End If
        Next

        ' If there're more than one file, proceed to join the file
        If (intFilecount > 1) Then
            ' join the files. Remove the last 3 characters from strFilenames (" + ").
            intExitCode = objShell.Run ("%COMSPEC% /C COPY /B " & Left (strFilenames, Len (strFilenames)-3) _
            & " """ & outPath & """ /Y", 0, True)
        Else
            ' Not enough file to join
        End If
    Else
        ' Can't find folder, exit.
    End If      
End Function
于 2016-06-27T03:28:06.900 回答