Table of Contents

6.4 Class Shell (gb.util)

The Shell class of the gb.util component provides you with three static methods to create, delete or move directories (recursively!) with content.

6.4.1 Shell.MkDir

Static Sub Shell.MkDir (sDirPath As String )

This method creates a directory - with sDirPath as the directory path - and all its parent (parent) directories. All paths must always be specified as absolute directory paths.

6.4.2 Shell.RmDir

Static Sub Shell.RmDir ( sDirPath As String [ , bForce As Boolean ] )

Removes the specified directory by recursively removing its entire contents and then the directory itself!

If Force is not set (default), then attempting to remove the following directories will result in an error:

6.4.3 Shell.Move

Static Sub Shell.Move( sSource As String, sDestination As String )

Moves a file or directory - with content - even if the source and destination are on different devices.

6.4.4 Project

In the project presented, all three methods are used in a variety of ways.

B1
Figure 6.4.4.1: Directory B3_Last with 10 text files has been created.

B2
Figure 6.4.4.2: The directory B3_Last was moved with content

B3
Figure 6.4.4.3: All created directories have been successfully deleted

The source code is given in full and then commented:

[1] ' Gambas class file
[2]
[3] Public Sub Form_Open()
[4]   FMain.Resizable = False
[5] End
[6]
[7] Public Sub btnShellMkDir_Click()
[8]
[9]   Dim sPath As String
[10]
[11]   sPath = User.Home &/ "B1_First/B2_Second/B3_Last"
[12]
[13]   If Not Exist(sPath) Then ' Query NOT required
[14]      Shell.MkDir(sPath)
[15]   Endif
[16]
[17]   CreateTextFiles(sPath, 10)
[18]   DirChooser1.ShowFile = True
[19]   DirChooser1.ShowDetailed = True
[20]   DirChooser1.Reload()
[21]   DirChooser1.SelectedPath = sPath
[22]
[23] End
[24]
[25] Public Sub btnShellMove_Click()
[26]
[27]   Dim sMessage As String
[28]
[29]   If Not Exist(User.Home &/ "Destination") Then
[30]      Shell.MkDir(User.Home &/ "Destination")
[31]   Endif
[32]   Try Shell.Move(User.Home &/ "B1_First/B2_Second/B3_Last", User.Home &/ "Destination")
[33] ' Try Shell.Move(User.Home &/ "B1_First/B2_Second", User.Home &/ "Destination")
[34]   If Error Then
[35]      sMessage = ("Error moving the specified directory!")
[36]      sMessage &= gb.NewLine & Error.Text & "."
[37]      sMessage &= gb.NewLine & "Error-Code = " & Error.Code
[38]      Message.Error(sMessage)
[39]      Shell.RmDir(User.Home &/ "Destination")
[40]      Return
[41]   Endif
[42]   DirChooser1.ShowFile = True
[43]   DirChooser1.ShowDetailed = True
[44]   DirChooser1.Reload()
[45]   DirChooser1.SelectedPath = User.Home &/ "Destination/B3_Last"
[46] ' DirChooser1.SelectedPath = User.Home &/ "Destination/B2_Second/B3_Last"
[47]
[48] End
[49]
[50] Public Sub btnShellRmDir_Click()
[51]
[52]   If Exist(User.Home &/ "B1_First") Then
[53]      Try Shell.RmDir(User.Home &/ "B1_First")
[54]      If Error Then
[55]         Message.Error("Deleting the specified directory resulted in an error!")
[56]      Endif
[57]   Endif
[58]
[59]   If Exist(User.Home &/ "Destination") Then
[60]      Try Shell.RmDir(User.Home &/ "Destination")
[61]      If Error Then
[62]         Message.Error("Deleting the specified directory resulted in an error!")
[63]      Endif
[64]   Endif
[65]
[66]   DirChooser1.Reload()
[67]   DirChooser1.ShowFile = False
[68]   DirChooser1.ShowDetailed = False
[69]
[70] End
[71]
[72] Private Sub CreateTextFiles(sPathDir As String, iNumber As String)
[73]
[74]   Dim sFileName As String
[75]   Dim i As Integer
[76]
[77]    For i = 1 To iNumber
[78]     sFileName = Hex$(Rand(0, 2 ^ 32 - 1)) ' Random file name
[79]     If Not Exist(sPathDir &/ sFileName & ".txt") Then
[80]        File.Save(sPathDir &/ sFileName & ".txt", "Text line 1.\nText line 2.")
[81]     Endif
[82]   Next
[83]
[84] End

Comment:

B4
Figure 6.4.4.4: Error during moving

Download

Project

Download