home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD138341162001.psc / JoinFile.cls < prev    next >
Encoding:
Visual Basic class definition  |  2001-01-16  |  15.0 KB  |  474 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "JoinFile"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = True
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = False
  10. Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
  11. Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
  12. Attribute VB_Ext_KEY = "SavedWithClassBuilder" ,"Yes"
  13. '--------------------------------------------------------------------
  14. 'Autor         : Rohullah Habibi
  15. 'Date          : Jan 12, 2001
  16. '--------------------------------------------------------------------
  17. 'Class         : JoinFiles
  18. 'Description   : Joins splitted files splitted by the SplitFile object
  19. 'Dependencies  : SplitFile Class
  20. 'UserInterface : No, just MsgBox is used in few places.
  21. 'Requirements  : Make sure the FLAG1 and FLAG2 properties match with
  22. '                SplitFile class properties.
  23. '                Set the following properties:
  24. '                1-JoinThisFile (the first splitted file)
  25. '                2-DestinationPath (where you want to save the joined file)
  26. '
  27. 'Note          : if the ErrNumber property <> 0, it means an error occured
  28. '                the ErrMessage property contain the error message.
  29. '
  30. '                After you set up all properties,
  31. '                call the LocateSplittedFiles() method to find out if
  32. '                the user selected the right splitted file.
  33. '
  34. '                If you want to show the process in a ProgressBar control
  35. '                then set the ProcessBar property to a ProgressBar control.
  36. '
  37. '                If you want to show the process in a Label control
  38. '                then set the StatusLabel property to a Label control.
  39. '
  40. '                If you want to show splitted files in ListView control
  41. '                then set the FilesListView property to a ListView control.
  42. '
  43. '                if you want to delete all splitted files after join,
  44. '                call the DeleteSplittedfiles method.
  45. '
  46. '                if you want to get a confirmation message before
  47. '                deleting all splitted files,
  48. '                set the ConfirmDelete property to true.
  49. '
  50. '>>>>>>>>>>>>>>> After you set all required properties then
  51. '                call the JoinFiles() method to split the file
  52. '
  53. '--------------------------------------------------------------------
  54.  
  55. Option Explicit
  56.  
  57. Public JoinFileCount As Integer
  58. Public JoinedFileName As String
  59. Public ConfirmDelete As Boolean
  60.  
  61.  
  62. Private splitObject As New SplitFile 'Just to call some friend procedures
  63. Private intWriteHandle As Integer
  64. Private intReadHandle As Integer
  65. Private mvarErrMessage As String 'local copy
  66. Private mvarErrNumber As Integer 'local copy
  67. Private mvarProcessBar As Object 'local copy
  68. Private mvarStatusLabel As Object 'local copy
  69. Private mvarJoinThisFile As String 'local copy
  70. Private mvarFilesListview As Object 'local copy
  71. Private mvarDestinationPath As String 'local copy
  72. Private mvarFileName As String 'local copy
  73. Private SplittedFilesCount As Integer
  74. Private JoinedFileCount As Integer
  75.  
  76. Private Const FLAG1 = "."
  77. Private Const FLAG2 = "«"
  78.  
  79.  
  80.  
  81. Public Property Let FileName(ByVal vData As String)
  82.     mvarFileName = GetJoinedName(vData, FLAG1)
  83. End Property
  84.  
  85.  
  86. Public Property Get FileName() As String
  87.       FileName = mvarFileName
  88. End Property
  89.  
  90.  
  91. Public Property Let DestinationPath(ByVal vData As String)
  92.     mvarDestinationPath = vData
  93. End Property
  94.  
  95.  
  96. Public Property Get DestinationPath() As String
  97.     DestinationPath = mvarDestinationPath
  98. End Property
  99.  
  100.  
  101. Public Property Set FilesListview(ByVal vData As Object)
  102.     Set mvarFilesListview = vData
  103. End Property
  104.  
  105.  
  106. Public Property Get FilesListview() As Object
  107.     Set FilesListview = mvarFilesListview
  108. End Property
  109.  
  110.  
  111. Public Property Let JoinThisFile(ByVal vData As String)
  112. Attribute JoinThisFile.VB_Description = "The path and file name to be splitted"
  113.     mvarJoinThisFile = vData
  114.     FileName = splitObject.GetFileName(vData)
  115. End Property
  116.  
  117.  
  118. Public Property Get JoinThisFile() As String
  119.     JoinThisFile = mvarJoinThisFile
  120. End Property
  121.  
  122.  
  123. Public Property Set StatusLabel(ByVal vData As Object)
  124.     Set mvarStatusLabel = vData
  125. End Property
  126.  
  127.  
  128. Public Property Get StatusLabel() As Object
  129.     Set StatusLabel = mvarStatusLabel
  130. End Property
  131.  
  132. Public Property Set ProcessBar(ByVal vData As Object)
  133.     Set mvarProcessBar = vData
  134. End Property
  135.  
  136.  
  137. Public Property Get ProcessBar() As Object
  138.     Set ProcessBar = mvarProcessBar
  139. End Property
  140.  
  141.  
  142. Public Property Let ErrNumber(ByVal vData As Integer)
  143. Attribute ErrNumber.VB_Description = "if an error occured this property will contain the error number"
  144.     mvarErrNumber = vData
  145. End Property
  146.  
  147.  
  148. Public Property Get ErrNumber() As Integer
  149.     ErrNumber = mvarErrNumber
  150. End Property
  151.  
  152.  
  153.  
  154. Public Property Let ErrMessage(ByVal vData As String)
  155. Attribute ErrMessage.VB_Description = "if an error occured this property will contain the error message"
  156.     mvarErrMessage = vData
  157. End Property
  158.  
  159.  
  160. Public Property Get ErrMessage() As String
  161.     ErrMessage = mvarErrMessage
  162. End Property
  163.  
  164.  
  165. Public Function LocateSplitedFiles()
  166.  
  167.     On Error GoTo ErrorHandle
  168.     Dim i As Integer
  169.     Dim SplitIsValid As Boolean
  170.         
  171.     If Not IsFileAValidSplit(JoinThisFile, FLAG1, FLAG2) Then
  172.         ErrNumber = -1
  173.         ErrMessage = "The file you selected is not a splitted file!"
  174.         Exit Function
  175.     Else
  176.         If Val(Mid$(JoinThisFile, InStr(1, JoinThisFile, FLAG2) + 1)) <> 1 Then
  177.             ErrNumber = -1
  178.             ErrMessage = "The file you selected is not the first splitted file!"
  179.             Exit Function
  180.         End If
  181.     End If
  182.     
  183.     SplitIsValid = True
  184.     
  185.     If LCase(Left(JoinThisFile, 2)) <> "a:" Then
  186.         For i = 1 To SplittedFilesCount
  187.             If Dir(GetSplitFile(JoinThisFile) & i) = "" Then
  188.                SplitIsValid = False
  189.                Exit For
  190.             End If
  191.         Next
  192.     
  193.         If Not SplitIsValid Then
  194.            ErrNumber = -1
  195.            ErrMessage = "Missing Split File: " & GetSplitFile(JoinThisFile) & i
  196.            Exit Function
  197.         End If
  198.         If TypeName(FilesListview) = "ListView" Then
  199.             FilesListview.ListItems.Clear
  200.             
  201.             For i = 1 To SplittedFilesCount
  202.                 splitObject.AddListViewItem FilesListview, GetSplitFile(JoinThisFile) & i, _
  203.                         FileLen(GetSplitFile(JoinThisFile) & i)
  204.             Next
  205.             
  206.         End If
  207.     End If
  208.     Exit Function
  209. ErrorHandle:
  210.     ErrNumber = Err.Number
  211.     ErrMessage = Err.Description
  212. End Function
  213.  
  214. Public Function JoinFiles()
  215.     On Error GoTo GetErr
  216.     Dim intjoinfilecount As Integer
  217.     
  218.     If SplittedFilesCount = 0 Then
  219.         LocateSplitedFiles
  220.     End If
  221.     If ErrNumber <> 0 Then
  222.         Exit Function
  223.     End If
  224.     
  225.     Dim i As Integer
  226.     Dim ByteArray() As Byte
  227.      
  228.        
  229.     intWriteHandle = FreeFile
  230.     Open DestinationPath & FileName For Binary Access Write As #intWriteHandle
  231.     
  232.     If TypeName(ProcessBar) = "ProgressBar" Then
  233.        ProcessBar.Visible = True
  234.        ProcessBar.Max = SplittedFilesCount
  235.     End If
  236.     
  237.     Screen.MousePointer = vbHourglass
  238.     For i = 1 To SplittedFilesCount
  239.         DoEvents
  240.         
  241.         If OpenNextSplittedFile() <> 0 Then Exit For
  242.         
  243.         ReDim ByteArray(1 To LOF(intReadHandle))
  244.         
  245.         Get #intReadHandle, , ByteArray
  246.         Put #intWriteHandle, , ByteArray
  247.     Next
  248.     Screen.MousePointer = vbDefault
  249.     Exit Function
  250. GetErr:
  251.     Screen.MousePointer = vbDefault
  252.     ErrNumber = Err.Number
  253.     ErrMessage = Err.Description
  254. End Function
  255.  
  256.  
  257. Private Function OpenNextSplittedFile() As Integer
  258.         On Error GoTo Errorhand
  259.         
  260.         Dim strNextSplitedFile As String
  261.      
  262.         Close (intReadHandle)
  263.                 
  264.         JoinedFileCount = JoinedFileCount + 1
  265.         
  266.         strNextSplitedFile = GetSplitFile(JoinThisFile) & JoinedFileCount
  267.         
  268.         If LCase(Left(strNextSplitedFile, 2)) = "a:" Then
  269.             If MsgBox("Please insert disk #" & JoinedFileCount & "  of  " & SplittedFilesCount & "  in drive A:.", vbInformation + vbOKCancel) = vbCancel Then
  270.                ErrNumber = -1
  271.                ErrMessage = "Process canceled by the user."
  272.                OpenNextSplittedFile = ErrNumber
  273.                Exit Function
  274.             Else
  275.                 If Dir(strNextSplitedFile) = "" Then Err.Raise 52
  276.             End If
  277.         End If
  278.         
  279.         
  280.         intReadHandle = FreeFile
  281.         Open strNextSplitedFile For Binary Access Read As #intReadHandle
  282.         
  283.         If TypeName(ProcessBar) = "ProgressBar" Then
  284.            ProcessBar.Value = JoinedFileCount
  285.         End If
  286.         If TypeName(StatusLabel) = "Label" Then
  287.             If Len("Joining File: " & strNextSplitedFile) > 80 Then
  288.                 StatusLabel.Caption = "Joining File: " & "..." & Right$(strNextSplitedFile, 51)
  289.             Else
  290.                 StatusLabel.Caption = "Joining File: " & strNextSplitedFile
  291.             End If
  292.         End If
  293.         
  294.         If LCase(Left(strNextSplitedFile, 2)) = "a:" Then
  295.             If TypeName(FilesListview) = "ListView" Then
  296.                splitObject.AddListViewItem FilesListview, strNextSplitedFile, LOF(intReadHandle)
  297.             End If
  298.         End If
  299.         DoEvents
  300.         OpenNextSplittedFile = 0
  301.         Exit Function
  302. Errorhand:
  303.       If Err.Number = 52 Then 'bad file name or number
  304.          If MsgBox("Please insert disk #" & JoinedFileCount & "  of  " & SplittedFilesCount & "  in drive A:.", vbInformation + vbOKCancel) = vbCancel Then
  305.             ErrNumber = -1
  306.             ErrMessage = "Process canceled by the user."
  307.             OpenNextSplittedFile = ErrNumber
  308.             Exit Function
  309.          Else
  310.             Resume
  311.          End If
  312.     Else
  313.         ErrNumber = Err.Number
  314.         ErrMessage = Err.Description
  315.         OpenNextSplittedFile = Err.Number
  316.     End If
  317. End Function
  318.  
  319.  
  320.  
  321. Private Sub Class_Initialize()
  322.     JoinedFileCount = 0
  323.     intReadHandle = 0
  324.     intWriteHandle = 0
  325.     ErrMessage = ""
  326.     ErrNumber = 0
  327.     SplittedFilesCount = 0
  328.     DestinationPath = ""
  329.     FileName = ""
  330.     ConfirmDelete = True
  331. End Sub
  332.  
  333. Private Sub Class_Terminate()
  334.     If TypeName(ProcessBar) = "ProgressBar" Then
  335.        ProcessBar.Value = 0
  336.        ProcessBar.Visible = False
  337.        Set ProcessBar = Nothing
  338.     End If
  339.     If TypeName(StatusLabel) = "Label" Then
  340.         StatusLabel.Caption = ""
  341.         Set StatusLabel = Nothing
  342.     End If
  343.     If TypeName(FilesListview) = "ListView" Then
  344.         Set FilesListview = Nothing
  345.     End If
  346.     
  347.     Close #intWriteHandle
  348.     Close #intReadHandle
  349.     Set splitObject = Nothing
  350.     If ErrNumber <> 0 Then
  351.         DeletePartialJoin
  352.     End If
  353. End Sub
  354.  
  355. Private Function DeletePartialJoin()
  356. If DestinationPath & FileName <> "" Then
  357.     If Len(Trim(Dir(DestinationPath & FileName))) > 0 Then
  358.         Kill (DestinationPath & FileName)
  359.     End If
  360. End If
  361. End Function
  362.  
  363.  
  364. Public Function DeleteSplittedFiles()
  365.     If ErrNumber = 0 And LCase(Left(JoinThisFile, 2)) <> "a:" Then
  366.        If ConfirmDelete Then
  367.           If MsgBox("All splitted files successfully joined, do you wish to delete them?", vbQuestion + vbOKCancel) = vbCancel Then
  368.              Exit Function
  369.           End If
  370.        End If
  371.        Close (intReadHandle)
  372.        Close (intWriteHandle)
  373.        Dim i As Integer
  374.        For i = 1 To SplittedFilesCount
  375.            If Dir(GetSplitFile(JoinThisFile) & i) <> "" Then
  376.               Kill (GetSplitFile(JoinThisFile) & i)
  377.            End If
  378.        Next
  379.        If TypeName(FilesListview) = "ListView" Then
  380.            FilesListview.ListItems.Clear
  381.        End If
  382.     End If
  383. End Function
  384.  
  385.  
  386. Private Function IsFileAValidSplit(strFileName, strFlag1 As String, strFlag2 As String) As Boolean
  387.     Dim i As Integer
  388.     Dim blnValid As Boolean
  389.     Dim strsplitcount As String
  390.     Dim FLAG2 As String
  391.     blnValid = True
  392.     
  393.     For i = Len(Trim(strFileName)) To 1 Step -1
  394.         'check #1
  395.         'we expect digit to be the last char
  396.         If i = Len(Trim(strFileName)) And Not IsNumeric(Mid$(strFileName, i, 1)) Then
  397.            blnValid = False
  398.            Exit For
  399.         End If
  400.         If Not IsNumeric(Mid$(strFileName, i, 1)) Then
  401.            'so, if we are here and it is the first non numeric char then
  402.            'it must be the second flag.
  403.            'we need to check this char against the flag passed to this func
  404.            'if it dosen't match then the user picked up the wrong file.
  405.            If FLAG2 = "" And strFlag2 = Mid$(strFileName, i, 1) Then
  406.                 FLAG2 = Mid$(strFileName, i, 1)
  407.                 'check #2
  408.                 'we expect digit after falg2 has the flag
  409.                 If Not IsNumeric(Mid$(strFileName, i - 1, 1)) Then
  410.                    blnValid = False
  411.                    Exit For
  412.                 End If
  413.            Else
  414.                 'ok, the char dosen't match with the flag, we got a wrong file
  415.                 If FLAG2 = "" Then
  416.                     blnValid = False
  417.                     Exit For
  418.                 Else
  419.                     'if we are here it means that we got the second flag, now
  420.                     'its time to check the first flag, if it desn't match again
  421.                     'we got the wrong file
  422.                    If strFlag1 <> Mid$(strFileName, i, 1) Then
  423.                       blnValid = False
  424.                       Exit For
  425.                    Else
  426.                         'if the flag matched then we got the right file and lets
  427.                         'get out of here
  428.                         Exit For
  429.                    End If
  430.                 End If
  431.            End If
  432.         Else
  433.             'any number of digits in between flag1 and flag2 to means total # of
  434.             'splitted files, so lets get the count of splitted files.
  435.             If FLAG2 <> "" And IsNumeric(Mid$(strFileName, i, 1)) Then
  436.                 strsplitcount = Mid$(strFileName, i, 1) & strsplitcount
  437.             End If
  438.         End If
  439.     Next
  440.     If FLAG2 = "" Or strsplitcount = "" Then
  441.        blnValid = False
  442.     Else
  443.         SplittedFilesCount = strsplitcount
  444.     End If
  445.     IsFileAValidSplit = blnValid
  446. End Function
  447.  
  448. Private Function GetSplitFile(strFile As String) As String
  449.     'This function returns the file name upto the second flag
  450.     'we asume we have the right file, because it is alerady checked.
  451.     
  452.     Dim i As Integer
  453.     For i = Len(Trim(strFile)) To 1 Step -1
  454.         
  455.         If Not IsNumeric(Mid$(strFile, i, 1)) Then 'we are at the second flag
  456.             strFile = Mid$(strFile, 1, i)
  457.             Exit For
  458.         End If
  459.     Next
  460.     GetSplitFile = strFile
  461. End Function
  462.  
  463. Function GetJoinedName(strSplitFile As String, strFlag1 As String) As String
  464.     Dim i As Integer
  465.     For i = Len(Trim(strSplitFile)) To 1 Step -1
  466.         
  467.         If Mid$(strSplitFile, i, 1) = strFlag1 Then  'we are at the second flag
  468.             strSplitFile = Mid$(strSplitFile, 1, i - 1)
  469.             Exit For
  470.         End If
  471.     Next
  472.     GetJoinedName = strSplitFile
  473. End Function
  474.