2012-12-03 24 views
1

我想創建5000個目錄,其目錄的名稱是計數器。用VB.NET創建5000個目錄

下面是我想要使用的代碼,但是這隻爲我創建1個目錄,爲什麼?

Dim Counter As Integer 

Counter = 1 

Do Until Counter = 5000 

     FolderPath = "C:/pics/" + Counter.ToString() + "/" 
     Directory.CreateDirectory(FolderPath) 

Loop 

     Counter += 1 

VB.NET或C#會這樣做,我只想運行一次。

+4

你可能想向上移動計數器邏輯進入循環。 – JonH

回答

6

在do循環中移動counter+=1。它可能創建第一個目錄,但是因爲計數器永遠不會在循環內部增加,所以它可能只是覆蓋自身。

改成這樣:

Do Until Counter = 5000 

     FolderPath = "C:/pics/" + Counter.ToString() + "/" 
     Directory.CreateDirectory(FolderPath) 
     Counter += 1 
Loop 
+3

謝謝你的工作!艾但我很愚蠢! – Etienne

5

不要使用do while整數,整數和雙精度型更好地使用功能For

For Counter as Integer = 1 to 5000 
    FolderPath = "C:/pics/" + Counter.ToString() + "/" 
    Directory.CreateDirectory(FolderPath) 
Next 

附:在你的情況下,你需要在loop聲明前移動counter+=1

2

你真的應該使用For循環此:

For counter as Integer = 1 To 5000 
    FolderPath = "C:/pics/" + counter.ToString() + "/" 
    Directory.CreateDirectory(FolderPath) 
End For