Results 1 to 6 of 6

Thread: File Splitter * resolved

Threaded View

  1. #6
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    I made this in the last hour or so... it can join and split multiple files, and it works in DOS (edit: command prompt I mean) too....

    for example
    FileSplit -split -2files "testfile1.dat" "testfile2.dat"

    it will output:
    testfile1.dat.split001
    testfile1.dat.split002

    testfile2.dat.split001
    testfile2.dat.split002

    VB Code:
    1. Option Explicit
    2. Option Compare Text ' for the Like operator
    3.  
    4. Private Enum IfStringNotFound
    5.     ReturnOriginalStr = 0
    6.     ReturnEmptyStr = 1
    7. End Enum
    8.  
    9. Private Sub Form_Load()
    10.     Dim CMD As String, Command1 As String, Command2 As String
    11.     Dim FileNames As Collection, K As Variant, SplitBytes As Long
    12.     Dim Buffer As String, FileCount As Integer
    13.    
    14.     Me.Hide
    15.    
    16.     On Error GoTo ErrExit
    17.     CMD = Trim(Command)
    18.     If CMD = "" Then
    19.         MsgBox "Format examples:" & vbNewLine & _
    20.             "FileSplit -split -2files ""TestFile.dat""" & vbNewLine & _
    21.             "FileSplit -split -700kbytes ""TestFile.dat""" & vbNewLine & _
    22.             "FileSplit -split -3mbytes ""TestFile.dat""" & vbNewLine & _
    23.             "FileSplit -join ""TestFile.dat.split001"" ""TestFile.dat.split002"""
    24.         Unload Me
    25.         Exit Sub
    26.     End If
    27.    
    28.     Command1 = LeftRight(CMD, "-", , ReturnEmptyStr)
    29.     Command2 = LeftLeft(LeftRight(Command1, "-", , ReturnEmptyStr), " ")
    30.     Command1 = LeftLeft(Command1, " ")
    31.    
    32.     ' Command1 should contain either "split" or "join" without qwotes
    33.    
    34.     Set FileNames = GetFileNames(LeftRight(RightRight(CMD, "-", , ReturnEmptyStr), " "))
    35.    
    36.     If FileNames Is Nothing Then
    37.         MsgBox "No files selected", vbExclamation
    38.         Unload Me
    39.         Exit Sub
    40.     End If
    41.    
    42.     If Command1 Like "*split*" Then ' split into files
    43.         For Each K In FileNames
    44.             If Command2 Like "#*files*" Then ' splits file in # files
    45.                 SplitBytes = FileLen(CStr(K)) / Val(Command2) ' calculate how many bytes in one file
    46.             ElseIf Command2 Like "#*kbytes" Then
    47.                 SplitBytes = Val(Command2) * 1024& ' size is given in Kilo Bytes
    48.             ElseIf Command2 Like "#*mbytes" Then
    49.                 SplitBytes = Val(Command2) * 1048576 ' size is given in Mega Bytes
    50.             ElseIf Command2 Like "#*bytes" Then
    51.                 SplitBytes = Val(Command2) ' size is given in Bytes
    52.             End If
    53.            
    54.             Open CStr(K) For Binary Access Read As #1
    55.                 Do
    56.                     If Loc(1) + SplitBytes > LOF(1) Then
    57.                         Buffer = String(LOF(1) - Loc(1), 0) ' if last file, do only remaining bytes
    58.                     Else
    59.                         Buffer = String(SplitBytes, 0)
    60.                     End If
    61.                    
    62.                     Get #1, , Buffer
    63.                    
    64.                     FileCount = FileCount + 1
    65.                     Open CStr(K) & ".split" & Right("000" & FileCount, 3) For Binary Access Write As #2
    66.                         Put #2, , Buffer
    67.                     Close #2
    68.                 Loop Until Loc(1) >= LOF(1)
    69.             Close #1
    70.         Next
    71.     ElseIf Command1 Like "*join*" Then ' join files
    72.         Dim Q As Integer, P As Integer, TmpStr As String
    73.         Dim ArrFileNames() As String, BaseFile As String
    74.        
    75.         ' can't sort the items in the Collection, so have to transfer into String array
    76.         ReDim ArrFileNames(FileNames.Count - 1)
    77.        
    78.         For Q = 0 To UBound(ArrFileNames)
    79.             ArrFileNames(Q) = FileNames(Q + 1)
    80.         Next Q
    81.        
    82.         ' sort the files, to make sure the data will be joined back in the right order
    83.         For Q = 0 To UBound(ArrFileNames)
    84.             For P = Q To UBound(ArrFileNames)
    85.                 If ArrFileNames(Q) > ArrFileNames(P) Then
    86.                     TmpStr = ArrFileNames(Q)
    87.                     ArrFileNames(Q) = ArrFileNames(P)
    88.                     ArrFileNames(P) = TmpStr
    89.                 End If
    90.             Next P
    91.         Next Q
    92.        
    93.         Do Until FileNames.Count = 0
    94.             FileNames.Remove 1
    95.         Loop
    96.        
    97.         ' put the filenames back into the collection in the right order
    98.         For Q = 0 To UBound(ArrFileNames)
    99.             FileNames.Add ArrFileNames(Q)
    100.         Next Q
    101.        
    102.         ' now they are in the right order, and ready to put them together
    103.        
    104.         Do Until FileNames.Count = 0
    105.             BaseFile = RightLeft(FileNames(1), ".split", , ReturnEmptyStr)
    106.            
    107.             Open BaseFile For Binary Access Write As #1
    108.                 Do While (CStr(FileNames(1)) Like BaseFile & "*")
    109.                     Open CStr(FileNames(1)) For Binary Access Read As #2
    110.                         Buffer = String(LOF(2), 0)
    111.                         Get #2, , Buffer
    112.                     Close #2
    113.                    
    114.                     Put #1, , Buffer
    115.                     FileNames.Remove 1
    116.                     If FileNames.Count = 0 Then Exit Do
    117.                 Loop
    118.             Close #1
    119.         Loop
    120.     Else
    121.         MsgBox "Unknown command.", vbExclamation
    122.     End If
    123.    
    124.     Unload Me
    125.     Exit Sub
    126. ErrExit:
    127.     MsgBox "Unexpected error !" & vbNewLine & _
    128.         "Error: " & Err.Number & vbNewLine & _
    129.         "Description: " & Err.Description, vbCritical, "Error"
    130.    
    131.     Err.Clear
    132.     Unload Me
    133.     Exit Sub
    134. End Sub
    135.  
    136. Private Function GetFileNames(ByVal CMD As String) As Collection
    137.     Dim FileName As String
    138.     If Trim(CMD) = "" Then Exit Function
    139.    
    140.     On Error GoTo ErrResize
    141.     Do Until Trim(CMD) = ""
    142.         CMD = LeftRight(CMD, """", , ReturnEmptyStr)
    143.         FileName = LeftLeft(CMD, """", , ReturnEmptyStr)
    144.         CMD = LeftRight(CMD, """", , ReturnEmptyStr)
    145.        
    146.         If Dir(FileName, vbHidden Or vbArchive) <> "" Then
    147.             If GetFileNames Is Nothing Then Set GetFileNames = New Collection
    148.             GetFileNames.Add FileName
    149.         End If
    150.     Loop
    151.    
    152.     Exit Function
    153. ErrResize:
    154.     MsgBox "Unexpected error !" & vbNewLine & _
    155.         "Error: " & Err.Number & vbNewLine & _
    156.         "Description: " & Err.Description, vbCritical, "Error"
    157.     Err.Clear
    158.     Exit Function
    159. End Function
    160.  
    161. Private Function LeftLeft(ByRef Str As String, LFind As String, Optional Compare As VbCompareMethod = vbBinaryCompare, Optional RetError As IfStringNotFound = ReturnOriginalStr) As String
    162.     Dim K As Long
    163.    
    164.     K = InStr(1, Str, LFind, Compare)
    165.     If K = 0 Then
    166.         LeftLeft = IIf(RetError = ReturnOriginalStr, Str, "")
    167.     Else
    168.         LeftLeft = Left(Str, K - 1)
    169.     End If
    170. End Function
    171.  
    172. Private Function LeftRight(ByRef Str As String, LFind As String, Optional Compare As VbCompareMethod = vbBinaryCompare, Optional RetError As IfStringNotFound = ReturnOriginalStr) As String
    173.     Dim K As Long
    174.    
    175.     K = InStr(1, Str, LFind, Compare)
    176.     If K = 0 Then
    177.         LeftRight = IIf(RetError = ReturnOriginalStr, Str, "")
    178.     Else
    179.         LeftRight = Right(Str, (Len(Str) - Len(LFind)) - K + 1)
    180.     End If
    181. End Function
    182.  
    183. Private Function RightLeft(ByRef Str As String, RFind As String, Optional Compare As VbCompareMethod = vbBinaryCompare, Optional RetError As IfStringNotFound = ReturnOriginalStr) As String
    184.     Dim K As Long
    185.    
    186.     K = InStrRev(Str, RFind, , Compare)
    187.    
    188.     If K = 0 Then
    189.         RightLeft = IIf(RetError = ReturnOriginalStr, Str, "")
    190.     Else
    191.         RightLeft = Left(Str, K - 1)
    192.     End If
    193. End Function
    194.  
    195. Private Function RightRight(ByRef Str As String, RFind As String, Optional Compare As VbCompareMethod = vbBinaryCompare, Optional RetError As IfStringNotFound = ReturnOriginalStr) As String
    196.     Dim K As Long
    197.    
    198.     K = InStrRev(Str, RFind, , Compare)
    199.    
    200.     If K = 0 Then
    201.         RightRight = IIf(RetError = ReturnOriginalStr, Str, "")
    202.     Else
    203.         RightRight = Mid(Str, K + 1, Len(Str))
    204.     End If
    205. End Function
    Attached Files Attached Files
    Last edited by CVMichael; Feb 12th, 2003 at 04:09 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width