简体   繁体   English

复制文件Vbscript

[英]CopyFile Vbscript

I need to join any files (2GB) without read their content. 我需要加入任何文件(2GB)而不读取其内容。 I have tried to use CopyFile method but it doesn't work. 我试图使用CopyFile方法,但是它不起作用。

My code is that: 我的代码是:

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

Are there some way to join two files using Vbscript? 有什么方法可以使用Vbscript连接两个文件吗?

Thanks 谢谢

To join two files, 'someone' has to to read (and write) the contents of both files. 要连接两个文件,“某人”必须读取(和写入)两个文件的内容。 This 'someone' could be copy [/B] f1 + f2 f3 . 这个“某人”可以是copy [/B] f1 + f2 f3 So use your loop to build the correct file specs and WshShell.Run/.Exec the suitabe commands. 因此,请使用循环构建正确的文件规范,并使用suitabe命令WshShell.Run/.Exec

Depends which COM objects you have access to and where the code is running. 取决于您有权访问哪些COM对象以及代码在何处运行。

1) If you have access to the Shell, then use the copy command of the DOS prompt. 1)如果您有权访问命令行管理程序,请使用DOS提示符下的copy命令。 This example shows the excecution of the Dir command but it's the same technique. 此示例显示了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

And maybe also take a look here http://ss64.com/vb/shellexecute.html . 也许还要在这​​里看看http://ss64.com/vb/shellexecute.html Don't know if that method will help. 不知道该方法是否有帮助。

2) If you have no shell then if you can make COM objects then make one with C++ or Delphi or VB6 etc. Then use that COM object to execute the DOS command to do the merging. 2)如果没有外壳,则可以制作COM对象,然后使用C ++或Delphi或VB6等制作一个COM对象。然后使用该COM对象执行DOS命令进行合并。

3) Otherwise you will have to read the data from the file. 3)否则,您将不得不从文件中读取数据。 If it is text files then that is easy because there are simple commands to do that with in the Vbs. 如果它是文本文件,那么这很容易,因为在Vb中有简单的命令可以执行此操作。 If it is binary data then that requires more work to get at the binary data and use the "LenB" and "AscB" style functions to access the raw bytes of the Unicode. 如果它是二进制数据,则需要更多的工作来获取二进制数据并使用“ LenB”和“ AscB”样式函数来访问Unicode的原始字节。 But you really do not want to do that. 但是您真的不想这样做。 Any upload handling script for ASP should show you the technique for working with raw bytes from the strings. 任何适用于ASP的上传处理脚本都应向您展示使用字符串中的原始字节的技术。

You can combine VBScript with Windows Copy command to join files. 您可以将VBScript与Windows Copy命令结合使用以加入文件。 You can see the document on appending binary files using Copy here https://support.microsoft.com/en-us/kb/71161 您可以使用此处的复制https://support.microsoft.com/zh-cn/kb/71161看到有关附加二进制文件的文档

Here's the example of the technique: 这是该技术的示例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM