How to pause, stop, or abort an If Else expression when the result excepted has been achieved ?
I want check if somes folders exists, exemple 1, 2, and 3. But I want stop and create only one missing folder. If folder 1 and 3 exists, create the 2 only.
For this exemple I tell about 3 folders for explain quick, really I need more directories.
There is my code, i don't know why this don't work ... It create folders but, crash my program
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Dim dirNumber As Integer = 1
Dim stopFunction As String
stopFunction = "null"
Dim DIR_PATH_test As New IO.DirectoryInfo("C:\testdir\" & dirNumber)
If DIR_PATH_test.Exists Then
'1 exists
dirNumber = dirNumber 1 '2
ElseIf DIR_PATH_test.Exists Then
'2 exists
dirNumber = dirNumber 1 '3
ElseIf DIR_PATH_test.Exists Then
'3 exists
' dirNumber already 3 do nothing
dirNumber = dirNumber 1
Else
stopFunction = "stop"
End If
If stopFunction = "null" Then
Dim DIR_NAME_test As String = "C:\testdir\" & dirNumber
MsgBox("dir name = " & DIR_NAME_test)
MsgBox("dir number = " & dirNumber)
MkDir(DIR_NAME_test)
End If
End Sub
If anyone can tell me why it don't appropriate. Ty
EDIT :
This don't work too.
Dim dirNumber As Integer = 1
Dim DIR_PATH_test As New IO.DirectoryInfo("C:\testdir\" & dirNumber)
Dim DIR_NAME_test As String = "C:\testdir\" & dirNumber
If DIR_PATH_test.Exists Then
'1 exists
dirNumber = dirNumber 1
' 2
If DIR_PATH_test.Exists Then
'2 exists
dirNumber = dirNumber 1
' 3
If DIR_PATH_test.Exists Then
'3 exists
' dirNumber = 3 don't increment
Else
MkDir(DIR_NAME_test)
End If
Else
MkDir(DIR_NAME_test)
End If
Else
MkDir(DIR_NAME_test)
End If
CodePudding user response:
If you are repeating the same thing over and over, use a loop and create a new DirectoryInfo
at each loop iteration to reflect the new directory name
Dim baseDir As String = "C:\testdir\"
For dirNumber As Integer = 1 To 3
Dim dirInfo As New IO.DirectoryInfo(Path.Combine(baseDir, dirNumber.ToString())
If Not dirInfo.Exists Then
dirInfo.Create()
Exit For ' Create only one directory
End If
Next
Note, this assumes that the directory name is "1", "2" or "3".