Search code examples
joinvbscriptarchivefile-copying

CopyFile Vbscript


I need to join any files (2GB) without read their content. I have tried to use CopyFile method but it doesn't work.

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?

Thanks


Solution

  • Depends which COM objects you have access to and where the code is running.

    1) If you have access to the Shell, then use the copy command of the DOS prompt. This example shows the excecution of the Dir command but it's the same technique.

    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. 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.

    3) Otherwise you will have to read the data from the file. If it is text files then that is easy because there are simple commands to do that with in the Vbs. 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. 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.