
How-To... - Learn By Example - Cab Copier
Here
is the first Learn By Example which will show you how to combine the FileCopy
statement, Dir Function, and how to find the locations of the Windows and
Windows System folder locations into a fully working program.
The program is a utility that will copy all the CAB files from the Windows95/Windows 98 CD to either the Windows or Windows\System directory. The point of the program ? - As well as showing how you can use the above statements, it also saves you time in the long run. Whenever you install a new piece of Hardware or use the Windows Setup part of the Add/Remove Programs in the Control Panel, you will be prompted for the Windows CD. This means you have to get the CD out of the drawer, out of the case, then into the CD drive. BUT with this program, because the CAB files are already stored on the hard drive, you just need to browse to the directory they are located(either the Windows or Windows\System folder) and it will install the drivers, or install a piece of software(such as Microsoft Paint).
To follow this example, create the form above. The controls are named from top ~ Text Box - txtDriveLetter ; Radio Buttons - optLocation - option buttons are a control array ; Command Button - cmdCopy ; Command Button - cmdQuit.
The form is called frmCabCopier First of all you need to insert a standard module. Insert the following code into the standard module :-
'Function used to find location
of the Windows Directory...
Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA"
(ByVal lpBuffer As String, ByVal nSize As Long) As Long 'All one line...
'Function used to find location
of System Directory...
Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA"
(ByVal lpBuffer As String, ByVal nSize As Long) As Long
'Function used to find location of Temporary Path
Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength
As Long, ByVal lpBuffer As String) As Long
'Varaibles used for the above functions...
Global Location As String
Public Length As Long
Public TempString As Long
Public Cd_Letter As String 'Stores the CD Letter entered
by the user....
Sub Copying()
'Holds the Directory that contains the CAB files.
'Win95 users is '[cdrive]:\Win95, and Win98 users 'is [cdrive]:\Win98...
Dim CDDirectory As String
Dim FileCount As Integer 'Hold the amount
of cab files...
Dim CabFilePath As String 'Holds the full path of the
CAB files directory - Drive 'and Directory
'Find the Winx directory...
CDDirectory = Dir(Cd_Letter & "win*", 16)
'If the directory is win95 or WIN95(incase it returns
'in capitals) then....
If CDDirectory = "win95" Or CDDirectory = "WIN95" Then
FileCount = 27 'Store 27 for the amount of cab files...
End If
If CDDirectory = "win98" Or CDDirectory = "WIN98" Then
FileCount
= 48 'Store 48 for the amount of cab files...
End If
'Holds the full path of the CAB files directory,
'and add the trailing slash to the end...
CabFilePath = Cd_Letter & CDDirectory & "\"
'Set up a temporary string, and store the first
'CAB filename in it...
temp$ = Dir(CabFilePath & "win*.cab")
'Start a loop....
For i = 1 To FileCount
'Update the form with the file being copied.
'A label, guage, etc, could have also been used...
frmCabCopier.Caption = "Current File : " & temp$
'Change the Directory to the Windows, or Windows/System
'directory...
ChDir Location
'Copy the current CAB file from the CD to the
'current directory(either Windows or Windows\System...
FileCopy CabFilePath & temp$, temp$
temp$ = Dir 'Store the next cab file...
'Loop until all cab files have been copied 'either
27(Win95) or 48(Win98)
Next
'Change the caption of the Form...
frmCabCopier.Caption = "Current File : All Copied"
End Sub
Sub GetSystemDir()
Length = 99 'Max size of string - can be bigger, but
99 should be enough....
Location = String$(100, 0)
'Call the function with a Temp Variable, and store
'the location in the variable Location.
TempString = GetSystemDirectory(Location, Length)
End Sub
Sub GetTempDir()
Length = 99
Location = String$(100, 0)
TempString = GetTempPath(Length, Location)
End Sub
Sub GetWinDir()
Length = 99
Location = String$(100, 0)
TempString = GetWindowsDirectory(Location, Length)
End Sub
'<----END OF MODULE CODE----->
Now, open the form at the General Declerations part and insert the following code into it :-
Private Sub cmdCopy_Click()
cmdQuit.Enabled = False 'Disable the exit button...
'If the first radio button is selected then...
If optLocation(0).Value = True Then
GetWinDir 'Find the location of the Windows directory...
End If
'If the second radio button is selected then...
If optLocation(1).Value = True Then
GetSystemDir 'Find the location of the System Dir...
End If
'Store the CD Drive Letter with a :\
Cd_Letter = txtDriveLetter & ":\"
Copying 'Run the Copying procedure...
cmdQuit.Enabled = True 'Re-enable the exit button...
End Sub
Private Sub cmdQuit_Click()
End
End Sub
Private Sub Form_Load()
Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
End Sub
You can download the source-code here