简体   繁体   中英

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

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 . So use your loop to build the correct file specs and WshShell.Run/.Exec the suitabe commands.

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.

You can combine VBScript with Windows Copy command to join files. You can see the document on appending binary files using Copy here https://support.microsoft.com/en-us/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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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