home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / largef1a / frmmain.frm (.txt) < prev   
Encoding:
Visual Basic Form  |  1999-09-19  |  14.4 KB  |  411 lines

  1. VERSION 5.00
  2. Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
  3. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "Comdlg32.ocx"
  4. Begin VB.Form frmMain 
  5.    BorderStyle     =   1  'Fixed Single
  6.    Caption         =   "File Splitter"
  7.    ClientHeight    =   3315
  8.    ClientLeft      =   45
  9.    ClientTop       =   330
  10.    ClientWidth     =   3765
  11.    LinkTopic       =   "Form1"
  12.    MaxButton       =   0   'False
  13.    MinButton       =   0   'False
  14.    ScaleHeight     =   3315
  15.    ScaleWidth      =   3765
  16.    StartUpPosition =   2  'CenterScreen
  17.    Begin VB.ComboBox cmbSplitSize 
  18.       Height          =   315
  19.       Left            =   120
  20.       Style           =   2  'Dropdown List
  21.       TabIndex        =   10
  22.       Top             =   960
  23.       Width           =   1335
  24.    End
  25.    Begin VB.Frame fraStats 
  26.       Caption         =   "Statistics"
  27.       Height          =   1335
  28.       Left            =   120
  29.       TabIndex        =   6
  30.       Top             =   1800
  31.       Width           =   3495
  32.       Begin VB.Label lblFileCount 
  33.          AutoSize        =   -1  'True
  34.          Caption         =   "File Count:"
  35.          Height          =   195
  36.          Left            =   120
  37.          TabIndex        =   12
  38.          Top             =   240
  39.          Width           =   750
  40.       End
  41.       Begin VB.Label lblElapsed 
  42.          AutoSize        =   -1  'True
  43.          Caption         =   "Elapsed Time:"
  44.          Height          =   195
  45.          Left            =   120
  46.          TabIndex        =   9
  47.          Top             =   960
  48.          Width           =   1005
  49.       End
  50.       Begin VB.Label lblFinish 
  51.          AutoSize        =   -1  'True
  52.          Caption         =   "Finish Time:"
  53.          Height          =   195
  54.          Left            =   120
  55.          TabIndex        =   8
  56.          Top             =   720
  57.          Width           =   840
  58.       End
  59.       Begin VB.Label lblStart 
  60.          AutoSize        =   -1  'True
  61.          Caption         =   "Start Time:"
  62.          Height          =   195
  63.          Left            =   120
  64.          TabIndex        =   7
  65.          Top             =   480
  66.          Width           =   765
  67.       End
  68.    End
  69.    Begin VB.CommandButton cmdBrowse 
  70.       Caption         =   "..."
  71.       Height          =   255
  72.       Left            =   3360
  73.       TabIndex        =   4
  74.       Top             =   360
  75.       Width           =   255
  76.    End
  77.    Begin VB.TextBox txtFileName 
  78.       Height          =   285
  79.       Left            =   120
  80.       TabIndex        =   3
  81.       Top             =   360
  82.       Width           =   3135
  83.    End
  84.    Begin VB.CommandButton cmdJoin 
  85.       Caption         =   "Join File"
  86.       Enabled         =   0   'False
  87.       Height          =   375
  88.       Left            =   2640
  89.       TabIndex        =   2
  90.       Top             =   960
  91.       Width           =   975
  92.    End
  93.    Begin MSComctlLib.ProgressBar ProgressBar1 
  94.       Height          =   255
  95.       Left            =   120
  96.       TabIndex        =   1
  97.       Top             =   1440
  98.       Width           =   3495
  99.       _ExtentX        =   6165
  100.       _ExtentY        =   450
  101.       _Version        =   393216
  102.       Appearance      =   1
  103.       Scrolling       =   1
  104.    End
  105.    Begin VB.CommandButton cmdSplit 
  106.       Caption         =   "Split File"
  107.       Default         =   -1  'True
  108.       Enabled         =   0   'False
  109.       Height          =   375
  110.       Left            =   1560
  111.       TabIndex        =   0
  112.       Top             =   960
  113.       Width           =   975
  114.    End
  115.    Begin MSComDlg.CommonDialog CommonDialog1 
  116.       Left            =   3120
  117.       Top             =   120
  118.       _ExtentX        =   847
  119.       _ExtentY        =   847
  120.       _Version        =   393216
  121.    End
  122.    Begin VB.Label lblSplitSize 
  123.       AutoSize        =   -1  'True
  124.       Caption         =   "Split Size (KB)"
  125.       Height          =   195
  126.       Left            =   120
  127.       TabIndex        =   11
  128.       Top             =   720
  129.       Width           =   990
  130.    End
  131.    Begin VB.Label lblFileName 
  132.       AutoSize        =   -1  'True
  133.       Caption         =   "Filename:"
  134.       Height          =   195
  135.       Left            =   120
  136.       TabIndex        =   5
  137.       Top             =   120
  138.       Width           =   675
  139.    End
  140. Attribute VB_Name = "frmMain"
  141. Attribute VB_GlobalNameSpace = False
  142. Attribute VB_Creatable = False
  143. Attribute VB_PredeclaredId = True
  144. Attribute VB_Exposed = False
  145. Option Explicit
  146. Public Function SplitFiles(ByVal inputFilename As String, newFileSizeBytes As Long) As Boolean
  147. Dim fReadHandle As Long
  148. Dim fWriteHandle As Long
  149. Dim fSuccess As Long
  150. Dim lBytesWritten As Long
  151. Dim lBytesRead As Long
  152. Dim ReadBuffer() As Byte
  153. Dim TotalCount As Long
  154. Dim StartTime As Date
  155. Dim FinishTime As Date
  156. Dim StartTimeDouble As Double
  157. Dim FinishTimeDouble As Double
  158. Dim Count As Integer
  159.     ' User Interface Stuff
  160.     StartTime = Now
  161.     StartTimeDouble = getTime
  162.     Me.MousePointer = vbHourglass
  163.     cmdJoin.Enabled = False
  164.     cmdSplit.Enabled = False
  165.     lblStart = "Start Time: " & StartTime
  166.     ProgressBar1.Max = FileLen(inputFilename)
  167.     ProgressBar1.Value = 0
  168.     Count = 1
  169.     ' Resize Byte Array for Read
  170.     ReDim ReadBuffer(0 To newFileSizeBytes)
  171.         
  172.     ' Determine Total Number of Output Files for User Interface Only
  173.     TotalCount = (FileLen(inputFilename) \ UBound(ReadBuffer)) + 1
  174.     ' Open Read File Handle
  175.     fReadHandle = CreateFile(inputFilename, GENERIC_WRITE Or GENERIC_READ, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)
  176.     ' If Successful read, continue
  177.     If fReadHandle <> INVALID_HANDLE_VALUE Then
  178.         ' Read First File Block
  179.         fSuccess = ReadFile(fReadHandle, ReadBuffer(0), UBound(ReadBuffer), lBytesRead, 0)
  180.         
  181.         ' Increment ProgressBar
  182.         ProgressBar1.Value = ProgressBar1.Value + lBytesRead
  183.         ProgressBar1.Refresh
  184.         
  185.         ' Loop while not EOF
  186.         Do While lBytesRead > 0
  187.             ' Update File Count Statistic on User Interface
  188.             lblFileCount.Caption = "Split Count: " & Count & " of " & TotalCount
  189.             lblFileCount.Refresh
  190.             
  191.             ' Open Write File Handle
  192.             If Dir(inputFilename & "." & Count) <> "" Then
  193.                 Kill inputFilename & "." & Count
  194.             End If
  195.             fWriteHandle = CreateFile(inputFilename & "." & Count, GENERIC_WRITE Or GENERIC_READ, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)
  196.             ' If Successful Write, Continue
  197.             If fWriteHandle <> INVALID_HANDLE_VALUE Then
  198.                 ' Write Data Block to File
  199.                 fSuccess = WriteFile(fWriteHandle, ReadBuffer(0), lBytesRead, lBytesWritten, 0)
  200.                 If fSuccess <> 0 Then
  201.                     ' Required to Write to File
  202.                     fSuccess = FlushFileBuffers(fWriteHandle)
  203.                     ' Close Write File
  204.                     fSuccess = CloseHandle(fWriteHandle)
  205.                 Else
  206.                     ' On Failure Quit
  207.                     lblFileCount.Caption = "Split Count: Write Error"
  208.                     SplitFiles = False
  209.                     Exit Function
  210.                 End If
  211.             Else
  212.                 ' On Failure Quit
  213.                 lblFileCount.Caption = "Split Count: Write Error"
  214.                 SplitFiles = False
  215.                 Exit Function
  216.             End If
  217.             ' Get the next Read Block
  218.             fSuccess = ReadFile(fReadHandle, ReadBuffer(0), UBound(ReadBuffer), lBytesRead, 0)
  219.                        
  220.             ' Increment ProgressBar
  221.             ProgressBar1.Value = ProgressBar1.Value + lBytesRead
  222.             ProgressBar1.Refresh
  223.             
  224.             ' Increment Count
  225.             Count = Count + 1
  226.         Loop
  227.         ' Close Read File
  228.         fSuccess = CloseHandle(fReadHandle)
  229.     Else
  230.         ' On Failure Quit
  231.         lblFileCount.Caption = "Split Count: Read Error"
  232.         SplitFiles = False
  233.         Exit Function
  234.     End If
  235.     ' User Interface Stuff
  236.     ProgressBar1.Value = ProgressBar1.Max
  237.     lblFileCount.Caption = "Split Count: Finished (" & Count - 1 & " Files)"
  238.     FinishTime = Now
  239.     FinishTimeDouble = getTime
  240.     lblFinish = "Finish Time: " & FinishTime
  241.     lblElapsed = "Elapsed Time: " & Format(FinishTimeDouble - StartTimeDouble, "#.00") & " seconds"
  242.     Call txtFileName_Change
  243.     Me.MousePointer = vbDefault
  244.     SplitFiles = True
  245. End Function
  246. Public Function JoinFiles(ByVal inputFilename As String) As Boolean
  247. Dim fReadHandle As Long
  248. Dim fWriteHandle As Long
  249. Dim fSuccess As Long
  250. Dim lBytesWritten As Long
  251. Dim lBytesRead As Long
  252. Dim ReadBuffer() As Byte
  253. Dim TotalCount As Long
  254. Dim StartTime As Date
  255. Dim FinishTime As Date
  256. Dim StartTimeDouble As Double
  257. Dim FinishTimeDouble As Double
  258. Dim Count As Integer
  259. Dim FileName As String
  260. Dim ret As Integer
  261.     ' Check for existing Output File
  262.     If Dir(inputFilename) <> "" Then
  263.         ret = MsgBox("Output file (" & inputFilename & ") already exists." & vbCr & "Are you sure you want to overwrite it?", vbYesNo + vbQuestion, "Overwrite Warning")
  264.         If ret = vbNo Then
  265.             lblFileCount.Caption = "Join Count: Output File Exists"
  266.             JoinFiles = False
  267.             Exit Function
  268.         Else
  269.             Kill inputFilename
  270.         End If
  271.     End If
  272.         
  273.     ' Determine how many split files are contained in the entire set
  274.     Count = 1
  275.     FileName = Dir(inputFilename & ".1")
  276.     If FileName = "" Then
  277.         lblFileCount.Caption = "Join Count: No Input Files"
  278.         JoinFiles = False
  279.         Exit Function
  280.     End If
  281.     ProgressBar1.Value = 0
  282.     ProgressBar1.Max = FileLen(inputFilename & ".1")
  283.     Do While FileName <> ""
  284.         Count = Count + 1
  285.         FileName = Dir(inputFilename & "." & Count)
  286.         If FileName <> "" Then
  287.             ProgressBar1.Max = ProgressBar1.Max + FileLen(inputFilename & "." & Count)
  288.         End If
  289.     Loop
  290.     TotalCount = Count - 1
  291.     ' User Interface Stuff
  292.     StartTime = Now
  293.     StartTimeDouble = getTime
  294.     Me.MousePointer = vbHourglass
  295.     cmdJoin.Enabled = False
  296.     cmdSplit.Enabled = False
  297.     lblStart = "Start Time: " & StartTime
  298.     ' Open Write File Handle
  299.     fWriteHandle = CreateFile(inputFilename, GENERIC_WRITE Or GENERIC_READ, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)
  300.     ' If Successful Write, Continue
  301.     If fWriteHandle <> INVALID_HANDLE_VALUE Then
  302.         For Count = 1 To TotalCount
  303.             ' Open Read File Handle
  304.             ReDim ReadBuffer(0 To FileLen(inputFilename & "." & Count))
  305.             fReadHandle = CreateFile(inputFilename & "." & Count, GENERIC_WRITE Or GENERIC_READ, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)
  306.             
  307.             ' If Successful read, continue
  308.             If fReadHandle <> INVALID_HANDLE_VALUE Then
  309.                 ' Read First File Block
  310.                 fSuccess = ReadFile(fReadHandle, ReadBuffer(0), UBound(ReadBuffer), lBytesRead, 0)
  311.                 
  312.                 ' Write Data Block to File
  313.                 fSuccess = WriteFile(fWriteHandle, ReadBuffer(0), UBound(ReadBuffer), lBytesWritten, 0)
  314.                 If fSuccess <> 0 Then
  315.                     ' Required to Write to File
  316.                     fSuccess = FlushFileBuffers(fWriteHandle)
  317.                 Else
  318.                     ' On Failure Quit
  319.                     lblFileCount.Caption = "Join Count: Write Error"
  320.                     JoinFiles = False
  321.                     Exit Function
  322.                 End If
  323.                                 
  324.                 fSuccess = CloseHandle(fReadHandle)
  325.             
  326.                  ' Increment ProgressBar
  327.                 ProgressBar1.Value = ProgressBar1.Value + lBytesWritten
  328.                 ProgressBar1.Refresh
  329.                 
  330.                 ' Update File Count Statistic on User Interface
  331.                 lblFileCount.Caption = "Join Count: " & Count & " of " & TotalCount
  332.                 lblFileCount.Refresh
  333.             Else
  334.                 ' On Failure Quit
  335.                 lblFileCount.Caption = "Join Count: Read Error"
  336.                 JoinFiles = False
  337.                 Exit Function
  338.             End If
  339.         
  340.         Next Count
  341.     Else
  342.         ' On Failure Quit
  343.         lblFileCount.Caption = "Join Count: Write Error"
  344.         JoinFiles = False
  345.         Exit Function
  346.     End If
  347.     ' Close Write File
  348.     fSuccess = CloseHandle(fWriteHandle)
  349.             
  350.     ' User Interface Stuff
  351.     ProgressBar1.Value = ProgressBar1.Max
  352.     lblFileCount.Caption = "File Count: Finished (" & Count - 1 & " Files)"
  353.     FinishTime = Now
  354.     FinishTimeDouble = getTime
  355.     lblFinish = "Finish Time: " & FinishTime
  356.     lblElapsed = "Elapsed Time: " & Format(FinishTimeDouble - StartTimeDouble, "#.00") & " seconds"
  357.     Call txtFileName_Change
  358.     Me.MousePointer = vbDefault
  359. End Function
  360. Private Sub cmdBrowse_Click()
  361.     On Error GoTo e_cmdBrowse
  362.     CommonDialog1.CancelError = True
  363.     Call CommonDialog1.ShowOpen
  364.     txtFileName = CommonDialog1.FileName
  365.     Exit Sub
  366. e_cmdBrowse:
  367.     Exit Sub
  368. End Sub
  369. Private Sub cmdJoin_Click()
  370.     If txtFileName <> "" And Dir(txtFileName & ".1") <> "" Then
  371.         Call JoinFiles(txtFileName)
  372.     Else
  373.         Call txtFileName_Change
  374.         Call MsgBox("File Not Found (" & txtFileName & ")", vbOKOnly + vbCritical, "File Read Error")
  375.     End If
  376. End Sub
  377. Private Sub cmdSplit_Click()
  378.     If txtFileName <> "" And Dir(txtFileName) <> "" Then
  379.         Call SplitFiles(txtFileName, CLng(cmbSplitSize.Text) * CLng(1024))
  380.     Else
  381.         Call txtFileName_Change
  382.         Call MsgBox("File Not Found (" & txtFileName & ")", vbOKOnly + vbCritical, "File Read Error")
  383.     End If
  384. End Sub
  385. Private Sub Form_Load()
  386.     cmbSplitSize.Clear
  387.     cmbSplitSize.AddItem ("1")
  388.     cmbSplitSize.AddItem ("2")
  389.     cmbSplitSize.AddItem ("5")
  390.     cmbSplitSize.AddItem ("10")
  391.     cmbSplitSize.AddItem ("50")
  392.     cmbSplitSize.AddItem ("100")
  393.     cmbSplitSize.AddItem ("500")
  394.     cmbSplitSize.AddItem ("1000")
  395.     cmbSplitSize.AddItem ("1440")
  396.     cmbSplitSize.AddItem ("2000")
  397.     cmbSplitSize.ListIndex = 7
  398. End Sub
  399. Private Sub txtFileName_Change()
  400.     If txtFileName <> "" And Dir(txtFileName) <> "" Then
  401.         cmdSplit.Enabled = True
  402.     Else
  403.         cmdSplit.Enabled = False
  404.     End If
  405.     If txtFileName <> "" And Dir(txtFileName & ".1") <> "" Then
  406.         cmdJoin.Enabled = True
  407.     Else
  408.         cmdJoin.Enabled = False
  409.     End If
  410. End Sub
  411.