Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim folderName As String = " WAP02"
If Not Directory.Exists(" C:\folder test" & "\" & folderName) Then
MessageBox.Show("Directory Does not Exist. Program will create a Directory")
Directory.CreateDirectory(" C:\folder test" & "\" & folderName)
MessageBox.Show("Directory created successfully")
Else
MessageBox.Show("Directory already existed")
End If
End Sub
我创建了一个按钮,将文件夹添加到我的文件夹中(文件夹测试)。如果我已经在我的文件夹(文件夹测试)中有WAP01,当我点击按钮时,如何编写例程来获取WAP02,如果我再次单击按钮,我将获得WAP03等等。
答案 0 :(得分:0)
尝试以下
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim folderName As String = "WAP0"
Dim folderUniqueNumber AS Integer = 1
While Directory.Exists(" C:\folder test" & "\" & folderName & folderUniqueNumber)
folderUniqueNumber +=1
End While
If folderUniqueNumber = 1 Then
MessageBox.Show("Directory Does not Exist. Program will create a Directory")
Directory.CreateDirectory(" C:\folder test" & "\" & folderName & folderUniqueNumber)
MessageBox.Show("Directory created successfully")
Else
MessageBox.Show("Directory already existed. Directory number incremented to: " & folderUniqueNumber)
Directory.CreateDirectory(" C:\folder test" & "\" & folderName & folderUniqueNumber)
MessageBox.Show("Directory created successfully")
End If
End Sub
While循环检查目录是否存在,如果是,则为文件夹编号添加1。然后,使用新创建的唯一文件夹名称创建目录,并添加一些小消息,指示用户是否必须首先增加文件夹。
编辑:
在阅读其他评论后,我认为这就是您所需要的。它成功地从“WAP01”一直通过“WZP20”创建编号目录。如果前面的目录在其所有子目录中的大小不超过4GB,则它不会创建下一个目录。与Enes的解决方案不同,即使目录不是在一个会话中创建的,它也能正常运行(如果您打开应用程序,按几次按钮,关闭它,重新打开它,按几次按钮),或者类似的目录在程序外部创建或删除。
如果用完了字母(在用完所有可能的“WZP”目录后),程序应该显示一条消息,然后退出。
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim folderPrevious As String = Nothing
Dim folderLetterAsNum As Integer = 65
Dim folderName As String = "W" & Chr(folderLetterAsNum) & "P0"
Dim folderUniqueNumber As Integer = 1
While Directory.Exists(" C:\folder test" & "\" & folderName & folderUniqueNumber)
folderPrevious = folderName & folderUniqueNumber
If folderUniqueNumber < 20 Then
folderUniqueNumber += 1
ElseIf folderLetterAsNum < 90
folderUniqueNumber = 1
folderLetterAsNum += 1
Else
MsgBox("You are out of characters!")
Me.Close()
Exit Sub
End If
If folderUniqueNumber < 10 Then
folderName = "W" & Chr(folderLetterAsNum) & "P0"
Else
folderName = "W" & Chr(folderLetterAsNum) & "P"
End If
End While
If folderPrevious Is Nothing OrElse FldrSize("C:\folder test\" & folderPrevious) / (1024 * 1024 * 1024) > 4 Then
If folderUniqueNumber = 1 And folderLetterAsNum = 65 Then
MessageBox.Show("Directory Does not Exist. Program will create a Directory")
Directory.CreateDirectory(" C:\folder test" & "\" & folderName & folderUniqueNumber)
MessageBox.Show("Directory created successfully")
Else
MessageBox.Show("Directory already existed. Directory directory created as: " & folderName & folderUniqueNumber)
Directory.CreateDirectory(" C:\folder test" & "\" & folderName & folderUniqueNumber)
MessageBox.Show("Directory created successfully")
End If
Else
MsgBox("Directory was not over 4GB. No directory created!")
End If
End Sub
Public Function FldrSize(ByVal dPath As String)
Dim size As Integer = 0
For Each foundFile As String In My.Computer.FileSystem.GetFiles(dPath, FileIO.SearchOption.SearchAllSubDirectories, "*.*")
Dim fInfo As New FileInfo(foundFile)
size += fInfo.Length
Next
Return size
End Function
希望有所帮助!
我不能直接评论Enes的答案(我没有足够的声誉),但遗憾的是他的解决方案存在缺陷。特别是当它创建文件夹“WAP20”时,它将尝试立即创建“WBP1”。这是因为它无法正确检查第二个IF结构中“WAP20”的大小。此外,如果您不在同一会话中完全创建目录,则程序将无法正常运行,或者在程序外部创建或删除文件夹。这很难描述,但我担心在这方面它不是一个合理的解决方案。
答案 1 :(得分:0)
首先,你需要这个。
Imports System.IO
然后这是你的代码。
Dim num As Integer = 1
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim folderName As String = "WAP" + num.ToString
If Not Directory.Exists(" C:\folder test" & "\" & folderName) Then
MessageBox.Show("Directory Does not Exist. Program will create a Directory")
Directory.CreateDirectory(" C:\folder test" & "\" & folderName)
MessageBox.Show("Directory created successfully")
Else
MessageBox.Show("Directory already existed")
End If
num += 1
End Sub
答案 2 :(得分:0)
Imports System
Imports System.IO
Public Class Form1
Dim size1 As Long = 4294967297
Dim num As Integer = 0
Dim folderName As String
Dim folderName2 As String
Dim dInfo As DirectoryInfo
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
If num < 21 And num >= 0 Then
folderName2 = "WAP" + (num - 1).ToString
If Directory.Exists("C:\folder test\" & folderName2) Then
dInfo = New DirectoryInfo("C:\folder test\" & folderName2)
size1 = dInfo.EnumerateFiles().Sum(Function(file) file.Length)
End If
folderName = "WAP" + num.ToString
If Not Directory.Exists("C:\folder test\" & folderName) And size1 / (1024 * 1024 * 1024) > 4 Then
MsgBox("Directory does not exist or directory size is bigger than 4GB. Program will create a new directory.")
Directory.CreateDirectory("C:\folder test\" & folderName)
MsgBox("Directory created successfully")
num += 1
Else
MsgBox("Directory already existed and smaller then 4GB.")
End If
End If
If num < 41 And num >= 21 Then
folderName2 = "WBP" + (num - 21).ToString
If Directory.Exists("C:\folder test\" & folderName2) Then
dInfo = New DirectoryInfo("C:\folder test\" & folderName2)
size1 = dInfo.EnumerateFiles().Sum(Function(file) file.Length)
End If
folderName = "WBP" + (num - 20).ToString
If Not Directory.Exists("C:\folder test\" & folderName) And size1 / (1024 * 1024 * 1024) > 4 Then
MsgBox("Directory does not exist or directory size is bigger than 4GB. Program will create a new directory.")
Directory.CreateDirectory("C:\folder test\" & folderName)
MsgBox("Directory created successfully")
num += 1
Else
MsgBox("Directory already existed and smaller then 4GB.")
End If
End If
If num < 61 And num >= 41 Then
folderName2 = "WCP" + (num - 41).ToString
If Directory.Exists("C:\folder test\" & folderName2) Then
dInfo = New DirectoryInfo("C:\folder test\" & folderName2)
size1 = dInfo.EnumerateFiles().Sum(Function(file) file.Length)
End If
folderName = "WCP" + (num - 40).ToString
If Not Directory.Exists("C:\folder test\" & folderName) And size1 / (1024 * 1024 * 1024) > 4 Then
MsgBox("Directory does not exist or directory size is bigger than 4GB. Program will create a new directory.")
Directory.CreateDirectory("C:\folder test\" & folderName)
MsgBox("Directory created successfully")
num += 1
Else
MsgBox("Directory already existed and smaller then 4GB.")
End If
End If
End Sub
End Class
当您按Button2
时,它会首先播放WAP0。如果WAP0大小超过4GB,则创建WAP1,然后创建WAP2,WAP3。当它到达WAP20时,它继续在WBP0上直到达到WBP20。然后启动WCP系列。你可以自己添加WDP。看看差异,每次都得到什么改变。祝你好运!