Results 1 to 3 of 3

Thread: Filename with filenumbers

  1. #1

    Thread Starter
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Filename with filenumbers

    vb Code:
    1. Private Sub SaveToText()
    2.  
    3.         Dim num As Integer
    4.         Dim filenumber As Integer
    5.  
    6.         Dim s As Short
    7.         Dim newstr(6) As String
    8.  
    9.         Dim I As Integer
    10.  
    11.         Dim Name As String
    12.         Dim Label As String
    13.         Dim AvailSpace As String
    14.         Dim TSize As String
    15.         Dim DriveFormat As String
    16.         Dim DriveReady As String
    17.  
    18.         Dim ItemCount As Integer
    19.         ItemCount = Me.ListView1.Items.Count
    20.  
    21.         If (Not System.IO.Directory.Exists("Drive Data")) Then
    22.             System.IO.Directory.CreateDirectory("Drive Data")
    23.         End If
    24.  
    25.         If File.Exists("DriveData\driveinfo" & num & ".txt") Then
    26.             While File.Exists("DriveData\driveinfo" & num & ".txt")
    27.                 num = num + 1
    28.             End While
    29.             filenumber = num
    30.         Else
    31.             filenumber = 1
    32.         End If
    33.  
    34.         Dim outputstream As New IO.StreamWriter("Drive Data\driveinfo" & filenumber & ".txt")
    35.  
    36.         For I = 0 To ItemCount - 1
    37.  
    38.             For s = 0 To 5
    39.                 newstr(s) = ListView1.Items.Item(I).SubItems(s).Text
    40.             Next
    41.  
    42.             Name = newstr(0)
    43.             Label = newstr(1)
    44.             AvailSpace = newstr(2)
    45.             TSize = newstr(3)
    46.             DriveFormat = newstr(4)
    47.             DriveReady = newstr(5)
    48.  
    49.             outputstream.WriteLine("Drive: " & Name.Trim() & " - " & Label.Trim() & " (Ready: " & DriveReady.Trim() & ")")
    50.             outputstream.WriteLine("Available Space: " & AvailSpace.Trim())
    51.             outputstream.WriteLine("Total Capacity: " & TSize.Trim())
    52.             outputstream.WriteLine("Format: " & DriveFormat.Trim())
    53.             outputstream.WriteLine("")
    54.  
    55.         Next I
    56.  
    57.         outputstream.Close()
    58.  
    59.         MsgBox(filenumber)
    60.         MsgBox("Export has been successfully created!", MsgBoxStyle.Information, "Information")
    61.  
    62.     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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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'?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    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:
    1. Private Sub SaveToText()
    2.  
    3.         Dim filenumber As Integer
    4.  
    5.         Dim s As Short
    6.         Dim newstr(6) As String
    7.  
    8.         Dim I As Integer
    9.  
    10.         Dim Name As String
    11.         Dim Label As String
    12.         Dim AvailSpace As String
    13.         Dim TSize As String
    14.         Dim DriveFormat As String
    15.         Dim DriveReady As String
    16.  
    17.         Dim ItemCount As Integer
    18.         ItemCount = Me.ListView1.Items.Count
    19.  
    20.         If (Not System.IO.Directory.Exists("Drive Data")) Then
    21.             System.IO.Directory.CreateDirectory("Drive Data")
    22.         End If
    23.  
    24.         filenumber = 1
    25.  
    26.         While File.Exists("Drive Data\driveinfo" & filenumber & ".txt")
    27.             filenumber = filenumber + 1
    28.         End While
    29.  
    30.         Dim outputstream As New IO.StreamWriter("Drive Data\driveinfo" & filenumber & ".txt")
    31.  
    32.         For I = 0 To ItemCount - 1
    33.  
    34.             For s = 0 To 5
    35.                 newstr(s) = ListView1.Items.Item(I).SubItems(s).Text
    36.             Next
    37.  
    38.             Name = newstr(0)
    39.             Label = newstr(1)
    40.             AvailSpace = newstr(2)
    41.             TSize = newstr(3)
    42.             DriveFormat = newstr(4)
    43.             DriveReady = newstr(5)
    44.  
    45.             outputstream.WriteLine("Drive: " & Name.Trim() & " - " & Label.Trim() & " (Ready: " & DriveReady.Trim() & ")")
    46.             outputstream.WriteLine("Available Space: " & AvailSpace.Trim())
    47.             outputstream.WriteLine("Total Capacity: " & TSize.Trim())
    48.             outputstream.WriteLine("Format: " & DriveFormat.Trim())
    49.             outputstream.WriteLine("")
    50.  
    51.         Next I
    52.  
    53.         outputstream.Close()
    54.  
    55.         MsgBox("Export has been successfully created!", MsgBoxStyle.Information, "Information")
    56.  
    57.     End Sub

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width