Results 1 to 2 of 2

Thread: .Dat / CR32 file sorter

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Posts
    1

    .Dat / CR32 file sorter

    Hi all,

    Im making a small app for dealing with some of my files, the idea is a can scan a folder and output a .dat inside that folder which has the file name and CRC32 of every file, like a hash file, which is working fine.

    Then i want to be able to scan a folder, and organize it based on an existing .Dat, any unknown files get moved to a 'UnKnown' folder, and likewise any duplicates get moved to 'Dupes' folder, and also 3 integers to be done (Total have, missing)

    Im having issues with the 2nd part, ill post everything Ive done so far;



    The code to create .dat;
    Code:
            Dim W As StreamWriter
            W = New StreamWriter(_foldertohash & "\Hash.dat32")
    
              For Each strFile As String In Directory.GetFiles(_foldertohash, "*.*", SearchOption.AllDirectories)
    
                    If Path.GetFileName(strFile) = "Hash.dat" = False Then 'Make sure not to include the hash file in the creation
    
                       Dim fileStream As New FileStream(strFile, FileMode.Open, FileAccess.Read, FileShare.Read, 8192I)
                        Dim pathbefore As String = Path.GetFullPath(strFile)
    
                        hashfilename = "*" & pathbefore.Replace(_foldertohash, "")
                       W.WriteLine(GetCrc32String(fileStream) & hashfilename) ''Write the hash to the file
    
                    fileStream.Close() ''CLOSE THE FILE STREAM
    
                End If
            Next
            W.Close() 'Save and close the hash file
    This outputs a .dat file which is layed out like so;

    Code:
    4553BDFF*\FolderMaker.exe
    C6CA2E65*\FolderMaker.exe.config
    F068030D*\FolderMaker.exe.manifest
    459DD286*\SubFolder test\New Text Document.txt
    42C63ADF*\SubFolder test\Untitled-1.ico
    Now that's the part which works fine, heres the code which is were im struggling to get right;


    Code:
            Directory.CreateDirectory(_foldertohash & "\UnKnown Files")
            Directory.CreateDirectory(_foldertohash & "\Duplicates")
    
            For Each strFile As String In Directory.GetFiles(_foldertohash, "*.*", SearchOption.AllDirectories)
    
                Dim fileStream As New FileStream(strFile, FileMode.Open, FileAccess.Read, FileShare.Read, 8192I)
                Dim thecrc As String = GetCrc32String(fileStream)
                fileStream.Close()
    
                For Each Line As String In File.ReadLines(_foldertohash & "\Hash.DIM32")
    
                    If Line.Contains(thecrc) = True Then
                        If Path.GetFileName(strFile) = _foldertohash & "\Hash.DIM32" = False Then
                            Dim arr As String() = Line.Split("*"c)
                            Dim getfpath As String = Path.GetDirectoryName(arr(1))
                            Directory.CreateDirectory(_foldertohash & "\" & getfpath)
    
                            File.Move(strFile, _foldertohash & "\" & arr(1))
    
                        elseIf Line.Contains(thecrc) = false Then
                            File.Move(strFile, _foldertohash & "\UnKnown Files\" & Path.GetFileName(strFile))
                        end if
                    End If
                Next
             next
    Now that part in it self is working fine, but if theres any dupes then i get errors, its detecting if a file is a dupe and if it is only keeping 1 copy of it out side of the dupes folder, and moving the rest to the dupes folder that im having issues with, all dupes need to be moved to the dupes folder with an integer added at the end of the file name (eg - somefile (1).jpg, somefile(2).jpg)

    And also how to keep a count of 'total missing' and 'total matched', if anyone can help

    Thankyou

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: .Dat / CR32 file sorter

    Since you can't have two files with the same name in the same directory, what are you considering a duplicate, two files with different names, but the same CRC32 value?
    Also, you have code that will never be executed. You are checking if Line.Contains(thecrc) = True to get into a block of code, so it can never be False in that block so no reason to check for a case that can't exist.
    Code:
                    If Line.Contains(thecrc) = True Then
                        If Path.GetFileName(strFile) = _foldertohash & "\Hash.DIM32" = False Then
    '...
                        elseIf Line.Contains(thecrc) = false Then
                            File.Move(strFile, _foldertohash & "\UnKnown Files\" & Path.GetFileName(strFile))
                        end if
                    End If
    The logic doesn't make much since to be inside that loop anyway. Since you are checking the files against the file list one line at a time, you wouldn't know if the file was an UnKnown file at that point anyway, since you are only looking at one file entry from the list. You would only have an unknown file if you went through the whole list and didn't have a match.

    It would seem like if you loaded the information into a dictionary first, using the CRC32 value as a key, and the file path/name as the value, you could look up the CRC32 value in the dictionary without having to inner search loop in your code.
    Last edited by passel; Jun 18th, 2018 at 10:29 AM.

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