【问题标题】:CopyFile VbscriptCopyFile Vbscript
【发布时间】:2013-01-29 06:08:38
【问题描述】:

我需要加入任何文件 (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 连接两个文件?

谢谢

【问题讨论】:

    标签: join vbscript archive file-copying


    【解决方案1】:

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

    【讨论】:

    • 谢谢。我可以使用 Run 方法抛出复制命令,但是现在我无法在每个循环中同步运行方法
    【解决方案2】:

    取决于您可以访问哪些 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) 如果你没有外壳,那么如果你可以制作 COM 对象,那么用 C++ 或 Delphi 或 VB6 等制作一个。然后使用该 COM 对象执行 DOS 命令进行合并。

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

    【讨论】:

    • 谢谢。我可以使用 Run 方法抛出复制命令,但是现在我无法在每个循环中同步运行方法:
    • 命令有参数。将 waitOnReturn 设置为 TRUE,它只会在文件实际合并后返回。 ss64.com/vb/run.html
    • @bobshacks - 请更正oShell.run 语句中的错误(参数列表(),注释标记)。
    • @bobshacks - 感谢您改进代码 (+1)。我很抱歉成为害虫,但刚才我看到 (n undefined) vbsTrue 让你的 sn-p 成为一个陷阱 - 简单的 True 会好得多。
    【解决方案3】:

    您可以将 VBScript 与 Windows Copy 命令结合使用来连接文件。 您可以在此处查看有关使用 Copy 附加二进制文件的文档 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
    

    【讨论】:

      猜你喜欢
      • 2019-06-07
      • 1970-01-01
      • 2011-09-17
      • 2011-06-10
      • 1970-01-01
      • 2018-02-25
      • 2017-07-23
      • 2015-10-28
      • 1970-01-01
      相关资源
      最近更新 更多