Results 1 to 16 of 16

Thread: [RESOLVED] Folder Looping Problem

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Posts
    27

    Resolved [RESOLVED] Folder Looping Problem

    Hello, I'm working on a new version of my old SecureMD5 program (which you may have seen on download.com or softpedia). One of the new features is Recursive Generation.

    The program loops through each file in a folder and generates a checksum for it. Then it places the checksums in a file.

    The only problem is... nothing happens. I click Generate, and the programs just sits there and does nothing. Here's my code for the Generate button:

    VB Code:
    1. Try
    2.                     Dim dir As New System.IO.DirectoryInfo(TextBox7.Text)
    3.                     Dim md5 As New System.Security.Cryptography.MD5CryptoServiceProvider
    4.                     Dim hash As Array
    5.                     Dim i As Integer
    6.                     Dim files As Array
    7.                     Dim sb As New System.Text.StringBuilder
    8.                     files = dir.GetFiles
    9.                     For i = files.Length To files.GetUpperBound(0)
    10.                         Dim f As New System.IO.FileStream(i.ToString, IO.FileMode.Open)
    11.                         md5.ComputeHash(f)
    12.                         hash = md5.Hash
    13.                         For Each hashByte As Byte In hash
    14.                             sb.Append(String.Format("{0:X1}", hashByte))
    15.                         Next
    16.                         System.IO.File.AppendAllText(TextBox8.Text, sb.ToString)
    17.                         f.Close()
    18.                     Next
    19.                 Catch ex As Exception
    20.                     MessageBox.Show("An error occured while trying to generate a checksum!" & Environment.NewLine & ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
    21.                 End Try

    TextBox7 is the textbox for the folder path and TextBox8 is the textbox for the output file (with the checksums). Just thought you might need to know that.

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

    Re: Folder Looping Problem

    VB Code:
    1. For i = files.Length To files.GetUpperBound(0)
    presumably should be
    VB Code:
    1. For i = 0 To files.GetUpperBound(0)
    If you'd used a breakpoint and stepped through your code I'm sure you'd have seen this pretty quickly. Also, whenever you are using the length of an array or collection to control a For loop I STRONGLY suggest including the "Step 1" on the end. If you don't and the array or collection is empty then the loop will try to access the first element/item and throw an exception, because it will implicitly try to iterate from zero to -1 with a step of -1.
    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
    Junior Member
    Join Date
    Aug 2005
    Posts
    27

    Re: Folder Looping Problem

    Thank you for replying, but when I change it to 0, I get
    Can not find file "C:\Documents and Settings\My Documents\0
    Also, I dunno how to use breakpoints. I never really took the time to learn how to use the built in debug functions.

  4. #4
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Folder Looping Problem

    Dim f As New System.IO.FileStream(i.ToString, IO.FileMode.Open)

    This is the problem. You are casting the counter variable to a string.

    Should be files(i).Fullname

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

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

    Re: Folder Looping Problem

    Quote Originally Posted by conipto
    Dim f As New System.IO.FileStream(i.ToString, IO.FileMode.Open)

    This is the problem. You are casting the counter variable to a string.

    Should be files(i).Fullname

    Bill
    Actually just files(i) as it is a String array rather than a FileInfo array.

    DataHunter2009, you ABSOLUTELY MUST learn how to use the debugger. It's one of the main reasons IDEs exist. Do you really think it's fair to ask us to spend the time to fix your problems when you already have the tools at your disposal to do it yourself? That's a gentle reprimand but a reprimand nonetheless. Using a breakpoint and the Watch window would have fixed your problem in about 1 minute.

    Also, given that you don't really need the loop index I'd tend to use a For Each loop rather than a For loop. That would get rid of both the issues pointed out in one go:
    VB Code:
    1. For Each fileName As String In files
    2.     Dim f As New System.IO.FileStream(fileName, IO.FileMode.Open)
    Last edited by jmcilhinney; Nov 13th, 2005 at 10:04 PM.
    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

  6. #6
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Folder Looping Problem

    Doesn't DirectoryInfo.GetFiles return a FileInfo Array?

    Oh and dude, those untyped Array's are ugly :P

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

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

    Re: Folder Looping Problem

    Quote Originally Posted by conipto
    Doesn't DirectoryInfo.GetFiles return a FileInfo Array?

    Oh and dude, those untyped Array's are ugly :P

    Bill
    Oops, quite right you are. I guess I must have just assumed without actually having looked. *self-flagellates* It actually should just be a String array though as no other member of the FileInfo is used:
    VB Code:
    1. For Each filePath As String In IO.Directory.GetFiles(folderPath)
    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

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Posts
    27

    Re: Folder Looping Problem

    Thanks for the answers! I changed the for statment and it worked great!

    Now the only problem is that when it does the next file, it sticks on the hash of the file before it. For example, I hashed a file and it gave me: "915083DB5073C6FCC9A6834D5D286B", but the next file gave me: "915083DB5073C6FCC9A6834D5D286B438C5BE9B4E66824201B873AC7DDB". Any ideas?

    VB Code:
    1. System.IO.File.AppendAllText(TextBox8.Text, "MD5 Hashes Generated by IntegriCheck" & Environment.NewLine & "Folder: " & TextBox7.Text & Environment.NewLine)
    2.                     Dim hash As Array
    3.                     Dim sb As New System.Text.StringBuilder
    4.                     For Each filePath As String In IO.Directory.GetFiles(TextBox7.Text)
    5.                         Dim f As New System.IO.FileStream(filePath, IO.FileMode.Open)
    6.                         Dim md5 As New System.Security.Cryptography.MD5CryptoServiceProvider
    7.                         md5.ComputeHash(f)
    8.                         hash = md5.Hash
    9.                         For Each hashByte As Byte In hash
    10.                             sb.Append(String.Format("{0:X1}", hashByte))
    11.                         Next
    12.                         System.IO.File.AppendAllText(TextBox8.Text, filePath & " - " & sb.ToString & Environment.NewLine)
    13.                         f.Close()

    Also, I didn't mean to waste your time. I'll check out VB's debug functions.

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

    Re: Folder Looping Problem

    I'm not sure I see the real flaw in your code but try this anyway. It is more efficient if nothing else, and might fix something we're not seeing:
    VB Code:
    1. Dim hash As Byte()
    2. Dim sb As New StringBuilder
    3. Dim f As FileStream
    4. Dim md5 As New MD5CryptoServiceProvider
    5.  
    6. For Each filePath As String In Directory.GetFiles(TextBox7.Text)
    7.     f = New FileStream(filePath, FileMode.Open)
    8.     hash = md5.ComputeHash(f)
    9.     'etc.
    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

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Posts
    27

    Re: Folder Looping Problem

    Thanks
    I changed all of that, but I still have the same problem. I cant find a way to clear the hash. Even "md5.clear()" doesn't work. Here's my current code:
    VB Code:
    1. System.IO.File.AppendAllText(TextBox8.Text, "MD5 Hashes Generate by IntegriCheck" & Environment.NewLine & "Folder: " & TextBox7.Text & Environment.NewLine)
    2.                     Dim hash As Byte()
    3.                     Dim sb As New System.Text.StringBuilder
    4.                     Dim f As IO.FileStream
    5.                     Dim md5 As New MD5CryptoServiceProvider
    6.  
    7.                     For Each filePath As String In IO.Directory.GetFiles(TextBox7.Text)
    8.                         f = New IO.FileStream(filePath, IO.FileMode.Open)
    9.                         hash = md5.ComputeHash(f)
    10.                         For Each hashByte As Byte In hash
    11.                             sb.Append(String.Format("{0:X1}", hashByte))
    12.                         Next
    13.                         System.IO.File.AppendAllText(TextBox8.Text, filePath & " - " & sb.ToString & Environment.NewLine)
    14.                         f.Close()
    15.                     Next

  11. #11
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Folder Looping Problem

    How about adding this on the first line of the for each loop (and removing the new from the original declaration)

    md5 = New MD5CryptoServiceProvider

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

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

    Re: Folder Looping Problem

    I think you should definitely not call Clear, as it Disposes the object. I'm not sure why you can't transform multiple streams with the same MD5 object though.

    One more note on efficiency. I'd suggest only using AppendAllText if you intend to make a single write to a file. Otherwise, open the file once at the start of the process and close it again at the end rather than opening and closing it repeatedly.
    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

  13. #13
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Folder Looping Problem

    This is a little OT, but a week ago I threw together a snippet that will find all the sub folders in a path and populate them to a string array.

    http://forums.aspfree.com/t87006/s.html

    A little added functionality for you instead of doing each directory individually?

    I don't know, just thought I'd throw it in

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

    Re: Folder Looping Problem

    I just realised that you're using a single StringBuilder object and Appending the new hash value each time, so each value you write to the file will (should, by your code) contain all the previous values as well. Is that what you mean, as opposed to each new hash value not being created? If that's the case, I'm guessing what you actually want to do is create a new StringBuilder for each new file. Also note that the StringBuilder class has an AppendFormat method, so using String.Format is redundant and less efficient.
    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

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Aug 2005
    Posts
    27

    Re: Folder Looping Problem

    Thank you very much! I moved the New statement for the stringbuilder down into the loop. It works great now!

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

    Re: Folder Looping Problem

    Again, using the debugger would have allowed you to solve this fairly quickly. Set a breakpoint at the top of the code, step throguh line by line and use the Autos, Locals and Watch windows to track the values of variables and expressions. Not to belabour the point, but using the debugger in the first place would have saved you a day. Glad you got it fixed though. Don't forget to resolve your thread from the Thread Tools menu.
    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

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