Results 1 to 11 of 11
  1. #1
    mich is offline Member
    Join Date
    Apr 2017
    Posts
    6
    Rep Power
    0

    Default Problems building file scanner in java

    Hi there...
    I want to scan a folder and hash(MD5) every file inside. Then I compare the hashes with my sql db which contain malicious file hashes.
    If file(s) is malicious (hash(es) from my folder matches hashes in db) I want to append it to text area and delete it.

    If file(s) is not malicious (files in my folder doesn't match any hashes in db) want to print in text area 'no malicious files found'.

    I have few issues:

    1. If more than one file are malicious in my folder, the 1st file(hash) is printed twice. Any clue why?
    2. If no malicious file are in folder the message 'no maliciuos files found' is not printed to text area. Any idea?
    3. I can delete the malicious file from folder but can't delete it from text area. Any suggestions?

    Any help would be much appreciated. Thank you.

    Java Code:
    //here is my code
    public void actionPerformed(ActionEvent e){
    
        if(e.getSource() == button){        
            int returnVal = fc.showDialog(AntiVirus.this, "MD5 Virus Check");
            if(returnVal == JFileChooser.APPROVE_OPTION){
    
                path  = fc.getSelectedFile().getAbsolutePath();
    
                File folder = new File(path);
                File [] filesList = folder.listFiles(); 
    
                // loop inside the folder and print the hash value      
                for (File selectFile : filesList){                  
                    if (selectFile.isFile()){
    
                        areaMD5.append(checkSum(selectFile) + "\n");
    
                        String hash [] = areaMD5.getText().split("\n");
    
                        for (String hashFile : hash){
    
                            try{
    
                                final String queryCheck = "SELECT hashFileValue FROM hashfile WHERE hashFileValue = ?";             
                                final PreparedStatement ps = (PreparedStatement) conn.prepareStatement(queryCheck);                 
                                ps.setString(1, hashFile);                  
                                final ResultSet resultSet = ps.executeQuery();
    
                                while (resultSet.next()){
    
                                    String hfv = resultSet.getString("hashFileValue");
    
                                    if(hashFile.equals(hfv)){                                
    
                                        areaAnswer.append("     " + hashFile + "     " + selectFile.getName() + "\n");
    
    
                                       //delete file if malicious
                                        selectFile.delete();                                    
                                        areaDelete.setText("     File Name:     " + selectFile.getName() + "     was deleted ! \n");
    
                                    }
    
                                    //----------this statement doesn't work---------                          
                                    else{
                                        areaAnswer.setText("   No malicious files found \n ");              
                                    }
    
    
                                 }// finish while
    
                               } catch (Exception ex){
    
                                   areaAnswer.setText("Exception is: " + ex);
                               }    
    
                            }//end for loop
    
                        }//if(selectFile)
    
                    }//for(File selectFile)
    
            }//if(JFileChooser)
    
            else{
                areaMD5.setText("   Select a folder. ");
            }
    
        }//if(e.getSource)
    
    }//end actionPerformed
    Last edited by mich; 04-10-2017 at 09:38 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Problems building file scanner in java

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    Any clue why?
    How are you trying to debug the code to see what it is doing?
    Try adding some print statements to show the values of variables as they are changed and used. The printout will help you find the problems in the code.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    mich is offline Member
    Join Date
    Apr 2017
    Posts
    6
    Rep Power
    0

    Default Re: Problems building file scanner in java

    Thank you for reply.
    I did use print statements, it is ok until before else{areaAnswer.setText(" No malicious files found \n ");} statement.
    It doesn't go inside else statement.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Problems building file scanner in java

    It doesn't go inside else statement.
    Does that mean the if is always true? If you expect it to be false some time, what are the values tested in the if that always give a true?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    mich is offline Member
    Join Date
    Apr 2017
    Posts
    6
    Rep Power
    0

    Default Re: Problems building file scanner in java

    Thanks Norm.
    I compare file hashes in folder with hashes in db.

    If statement should be false when file hashes in folder don't match any hashes in my db (all hashes in db represent malicious files).

    Scenario,I have 2 folders:
    ...1 has say 5files, of which 2 are malicious.
    Run the program works ok (hashes are printed to text area) with one exception, 1st hash is printed twice.
    ...2nd has say 3files, all clean (no match in db).
    Now, when program runs it should print 'no malicious files founded' message to text area. As it is, it doesn't.

    I have tried an if statement instead of else,
    smth. like *if(!hashFile.equals(hfv)){areaAnswer.setText("No malicious files found \n ");} with no success.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Problems building file scanner in java

    If statement should be false when
    Is this the statement:
    Java Code:
      if(hashFile.equals(hfv)){
    What values does that equals method compare? Are they ever different?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Problems building file scanner in java

    You seem to be appending the hash to whatever areaMd5 is (I assume the text area) and then read out the hashes (or whatever is in that text area) and then check whether those values (which may or may not be hashes, I don't know) are in the db.
    Every time.

    What I would have expected to see is:
    Java Code:
    For each file in directory
        calculate hash of file
        check if hash is in db
        append result to text area
    but that doesn't look like what you are doing, and is probably the root cause of your issue.

    That aside, one small improvement either way is to create the PreparedStatement before the for(Files) loop.
    PreparedStatements allow the db (and driver) to pre-calculate queries, and do other set up things. Re-using them if you can (without causing other confusion) is a good idea. I say "if you can" because there's not often a good way of doing that. In this case there is.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    mich is offline Member
    Join Date
    Apr 2017
    Posts
    6
    Rep Power
    0

    Default Re: Problems building file scanner in java

    Thanks Norm.
    Yes that's the statement...
    values are md5 hashes, they are different...
    I have 2 hashes-md5 in one of my folders that are in db as well... that's when I compare and find malicious files...
    the other folder has different hashes than db...that's when I want to print to text area 'no malicious files found'

  9. #9
    mich is offline Member
    Join Date
    Apr 2017
    Posts
    6
    Rep Power
    0

    Default Re: Problems building file scanner in java

    Hi Tolls

    Thank you for suggestion.
    It doesn't work, it doesn't print hash for malicious file in md5 text area...
    I'll try more..

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Problems building file scanner in java

    What doesn't work?
    If you've changed the code to match the logic I gave then we would need to see the new code to help.

    One thing, if you are having issues with the values then you need to stick lots (and lots) of debugging println's in there, printing out values of things, and where you are in the code.
    At the moment (from the code above) you have no logging like that, so you can't easily tell what's going on.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    mich is offline Member
    Join Date
    Apr 2017
    Posts
    6
    Rep Power
    0

    Default Re: Problems building file scanner in java

    Thanks Tolls,

    I followed your steps...and WORKS...

    Regards,
    mich

Similar Threads

  1. Scanner Problems
    By kev670 in forum New To Java
    Replies: 3
    Last Post: 03-14-2011, 01:06 AM
  2. Scanner.next() problems!
    By ngc0202 in forum New To Java
    Replies: 3
    Last Post: 08-11-2010, 01:50 AM
  3. Help in building .ear file with eclipse
    By honzell in forum Eclipse
    Replies: 0
    Last Post: 03-24-2010, 09:29 AM
  4. building a house but having problems connecting it
    By youngflames in forum New To Java
    Replies: 9
    Last Post: 01-26-2010, 06:00 PM
  5. Scanner class problems
    By sypherz in forum New To Java
    Replies: 6
    Last Post: 06-09-2008, 09:46 AM

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
  •