User Tools

Site Tools


k6:k6.4:start

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!

  • sDirPath: Path of the directory to be removed,
  • bForce: Optional parameter.

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

  • user's home directory.
  • root directory and any directory under the root directory (/home, /usr, /etc, …).

6.4.3 Shell.Move

Static Sub Shell.Move( sSource As String, sDestination As String )
  • sSource: source path,
  • sDestination: destination path.

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.

  • First, a directory User.Home &/ “B1_First/B2_Second/B3_Last” is created if it does not already exist. Then 10 text files with random file names are created in the directory B3_Last, the contents of each file consisting of two lines of text.
  • Then the directory User.Home &/ “Destination” is created if it does not already exist.
  • Then the directory User.Home &/ “B1_First/B2_Second/B3_Last” with content (!) is moved into the directory User.Home &/ “Destination”.
  • Finally, you can delete all created directories (recursively) and their contents.

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:

  • Line 11 specifies the path for the directory tree to be created. The base directory is B1_First.
  • In line 14 the directory tree is created - which can be seen in figure 6.4.4.1. A query whether the directory tree already exists would not be necessary because it is done internally by the method. However, this was only realised later.
  • Subsequently, 10 text files are created in the target directory, which are displayed in a switched-on extended detail view in the DirChooser.
  • In lines 29 to 31, it is first checked whether the specified directory exists. If it does not exist, then the target directory “Destination” is created.
  • In line 32, the directory B3_Last with the 10 files is moved to the destination directory, as can be seen in figure 6.4.4.2.
  • In lines 50 to 70, all directory trees created at runtime are deleted.
  • The move generates an error if, for example, the files or directories involved do not exist:

B4
Figure 6.4.4.4: Error during moving

Download

Project

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k6/k6.4/start.txt · Last modified: 13.01.2022 (external edit)

Page Tools