Filename with filenumbers
vb Code:
Private Sub SaveToText()
Dim num As Integer
Dim filenumber As Integer
Dim s As Short
Dim newstr(6) As String
Dim I As Integer
Dim Name As String
Dim Label As String
Dim AvailSpace As String
Dim TSize As String
Dim DriveFormat As String
Dim DriveReady As String
Dim ItemCount As Integer
ItemCount = Me.ListView1.Items.Count
If (Not System.IO.Directory.Exists("Drive Data")) Then
System.IO.Directory.CreateDirectory("Drive Data")
End If
If File.Exists("DriveData\driveinfo" & num & ".txt") Then
While File.Exists("DriveData\driveinfo" & num & ".txt")
num = num + 1
End While
filenumber = num
Else
filenumber = 1
End If
Dim outputstream As New IO.StreamWriter("Drive Data\driveinfo" & filenumber & ".txt")
For I = 0 To ItemCount - 1
For s = 0 To 5
newstr(s) = ListView1.Items.Item(I).SubItems(s).Text
Next
Name = newstr(0)
Label = newstr(1)
AvailSpace = newstr(2)
TSize = newstr(3)
DriveFormat = newstr(4)
DriveReady = newstr(5)
outputstream.WriteLine("Drive: " & Name.Trim() & " - " & Label.Trim() & " (Ready: " & DriveReady.Trim() & ")")
outputstream.WriteLine("Available Space: " & AvailSpace.Trim())
outputstream.WriteLine("Total Capacity: " & TSize.Trim())
outputstream.WriteLine("Format: " & DriveFormat.Trim())
outputstream.WriteLine("")
Next I
outputstream.Close()
MsgBox(filenumber)
MsgBox("Export has been successfully created!", MsgBoxStyle.Information, "Information")
End Sub
Here's my code, and I can get it to create the directory "Drive Data" if it doesn't exist, and a file gets created as "driveinfo1.txt" but if that file exists it won't save the new file as "driveinfo2.txt" which is what i'm trying to get this code to do.
If anyone can help me understand where the logic in this is wrong and put me on the right path i'd appreciate that. Don't need someone to write the code for me, but i'm sure i'm just doing some silly mistake in this code not allowing it to work the way I want it to.
Re: Filename with filenumbers
Could it be that you're using two different folders? "Drive Data" and "DriveData" are not the same thing.
By the way, your If...Else is pointless. Just use a While loop alone. Also, what's the point of having 'num' and 'filenum'?
Re: Filename with filenumbers
lol I was just testing random things. But I fixed it up now, and it works, I knew it was something stupid to begin with... Anyways thanks for the help :) I really appreciate that.
Here's the result:
vb Code:
Private Sub SaveToText()
Dim filenumber As Integer
Dim s As Short
Dim newstr(6) As String
Dim I As Integer
Dim Name As String
Dim Label As String
Dim AvailSpace As String
Dim TSize As String
Dim DriveFormat As String
Dim DriveReady As String
Dim ItemCount As Integer
ItemCount = Me.ListView1.Items.Count
If (Not System.IO.Directory.Exists("Drive Data")) Then
System.IO.Directory.CreateDirectory("Drive Data")
End If
filenumber = 1
While File.Exists("Drive Data\driveinfo" & filenumber & ".txt")
filenumber = filenumber + 1
End While
Dim outputstream As New IO.StreamWriter("Drive Data\driveinfo" & filenumber & ".txt")
For I = 0 To ItemCount - 1
For s = 0 To 5
newstr(s) = ListView1.Items.Item(I).SubItems(s).Text
Next
Name = newstr(0)
Label = newstr(1)
AvailSpace = newstr(2)
TSize = newstr(3)
DriveFormat = newstr(4)
DriveReady = newstr(5)
outputstream.WriteLine("Drive: " & Name.Trim() & " - " & Label.Trim() & " (Ready: " & DriveReady.Trim() & ")")
outputstream.WriteLine("Available Space: " & AvailSpace.Trim())
outputstream.WriteLine("Total Capacity: " & TSize.Trim())
outputstream.WriteLine("Format: " & DriveFormat.Trim())
outputstream.WriteLine("")
Next I
outputstream.Close()
MsgBox("Export has been successfully created!", MsgBoxStyle.Information, "Information")
End Sub