Hello

My recursive file scan is not doing what I want it to do.

I want it to be able to go through all roots (drives)...

With no user intervention, so without including "C:\\" or "." as a string in the source, or without a TextIO.java class.

import java.io.*;
import java.util.*;
 
public class RecursiveFileListAttemp1 {
    public static void main(String[] args) {
        File f = new File("C:\\");  
        search(f);
    }

    public static void search(File f) {  
        if ( !f.exists() ) return; 
        String name = f.getName(); 
        if ( f.isDirectory() ) {
        File[] files = f.listFiles();
        for( int i = 0 ; i < files.length; i++ ) {
        search( files[i] );
            }
        }
    }
}

At the moment I just get a null pointer exception...

I dont understand where Ive gone wrong in the code:
if its a file --> return the file name --> if its a directory --> list the files in the directory

Help please!

Hello,

Code is working perfectly! No NPException. Using Eclipse SDK Version: 3.4.0.

Best of luck!

really thats interesting punnetkay i wonder why

but it needs to work for me!

post above: thanks for that, but I dont want to list the roots I want to list the files and files in directories that are in all directories on a computer.

I think Ive got some stable code now. But I still want to know how to recursive scan the whole computer and not just a drive. At the moment I'm able to scan "C:\\" or "D:\\" and not both, does any one know how I can make that work?

post above: thanks for that, but I dont want to list the roots I want to list the files and files in directories that are in all directories on a computer.

Then what was this?

I want it to be able to go through all roots (drives)...

With no user intervention, so without including "C:\\" or "." as a string in the source, or without a TextIO.java class.

As that is the part I was responding to. You will need to use that.

I think Ive got some stable code now. But I still want to know how to recursive scan the whole computer and not just a drive. At the moment I'm able to scan "C:\\" or "D:\\" and not both, does any one know how I can make that work?

See above. You can only "recursively" scan each drive/root one at a time. But you can loop over what the above mentioned method returns.

Why does no one ever listen to what I say?

Hello again,

And again its working fine for me.

public static void main(String[] args) {
        for(File file : File.listRoots()){ // listRoots() method return list of drives. array of File type.
        	search(file);
        }
    }

    public static void search(File f) {  
        if ( !f.exists() ) return; 
        String name = f.getName();
        System.out.println(name);
        if ( f.isDirectory() ) {
        File[] files = f.listFiles();
        for( int i = 0 ; i < files.length; i++ ) {
        search( files[i] );
            }
        }
    }

Regards,

The NPE (as the OP already knows from his post on www.java-forums.com (or is it org?)) is because listFiles will return null on an empty directory so he needs to add an if statement to check for that.

can I add a equals() statement in the code above and from that print to the terminal window files that occur on more than one instance on the recursive scan?

lol i started that thread but ignore that one for now lol

anyways

thanks masijade for your reply

i tried something new

modified code below

public static void search(File f) {
        if ( !f.exists() ) return; 
        String name = f.getName();
        System.out.println(name);
        if ( f.isDirectory() ) {
        File[] files = f.listFiles();
        if (files != null) {
        for( int i = 0 ; i < files.length; i++ ) {
        search( files[i] );
        } if (f.length() <= 1) return;
		}
		

            }
        }

well to the code ive added the following line: if (f.length() <= 1) return; but i dont think it has had any change in the list of results brought to the terminal window

can some one with more java knowledge tell me why please? any help or correction welcomed!

> but i dont think it has had any change in the list of results brought
> to the terminal window

And what were you expecting by checking if the file size is less than or equal to one byte?

Maybe posting/uploading a virtual directory structure along with the expected output might help others in collaborating for a better solution.

I'm fairly sure you meant "files.length" and not "f.length()".

Edit: Not that that matters though. Once the loop is done, the method will return anyway.

hello again i tried your advice erm no difference I think

I dont quite understand how not to make "the method not return anyway"

Please may someone help me to get files of the same size listed on screen or put into an array (both if possible).

Heres what ive tried so far, (also take a look at the commented out code)

import java.io.*;
import java.util.*;

public class scanner {

public static void main(String[] args) {
        for(File file : File.listRoots()){ 
            search(file);
           // duplicate(file);
        }
    }

public static void search(File f)
{
    //if (!f.exists()) return;
   // String name = f.getName();
   // System.out.println(name);
  //  if (duplicate(f)) System.out.println(f); 
     if ( f.isDirectory() ) {
        File[] files = f.listFiles();
         //if (files.length == files.length) return; {
        for( int i = 0 ; i < files.length; i++ ) {
            if (files[i].length() == files[i].length()) {
        search( files[i] );
            //if (files[i].length() == files[i].length()) return;    
              System.out.println(files[i]);  
            } } } } 
            
        

//public static void duplicate(File file) 
//{

  //if (file.length() == file.length()) return;  
  //    }
}

please please help..

so just to add more detail because theyre may be confusion

Im basically trying to get files with equal file size first, then im trying to put those in an array, then the files in the array need to be compared possibily by length (bytes) (not file names but files contents), the comparison need to be made maybe by binary contents or by generating an MD checksum which ever ones easier.

so just to add more detail because theyre may be confusion

Im basically trying to get files with equal file size first, then im trying to put those in an array, then the files in the array need to be compared possibily by length (bytes) (not file names but files contents), the comparison need to be made maybe by binary contents or by generating an MD checksum which ever ones easier.

Like it has already been mentioned by masijade, you have to make a stab at this and post again if you have any problems. Just putting in random conditional statements here and there won't cut in. Since you already seem to know the logic, you can at least try implementing it.

i just thought you guys would of liked a challenge lol

thanks anyways

> i just thought you guys would of liked a challenge lol

I have got enough challenges in my daily life, I guess I'll just pass. :-)

But don't give up hope, who knows, masijade might just end up dropping in a jar file here with a "meh" look on his face. ;-)

Comments
thankyou for the hope!

why when i compile this code now i only get a list of folders and .sqm files and .log file and a .txt file and an .exe file. Ive got this list of files from d and c drive

import java.io.*; 
import java.util.*;
import java.*;

import java.io.*;
import java.util.*;
 
public class DeleteSameFilesAttempt9x10 {
public static void main(String[] args) {
        for(File file : File.listRoots()){ 
            search(file);
        }
    }

   
    
    public static void search(File f) {
        if ( !f.exists() ) return; 
        String name = f.getName();
        System.out.println(name);
        if ( f.isDirectory() ) {
        File[] files = f.listFiles();
        if (files != null) {
        for( int i = 0 ; i < files.length; i++ ) {
        samesize( files[i], files[i] ); {
            
            System.out.println(files[i]);
     
        }   

     
            
        }
    }
 
}
}

public static void samesize (File filey, File filex) {
  if (filey != null) if (filex !=null)
  {
      if (filey.length() == filex.length()) return;
  String name1 = filey.getName();
  String name2 = filex.getName();
  System.out.println(name1 + name2);
            }
        }
    }

I don't even understand what you mean by that question. Your code is only processing the top level of each root entry and only then if it's a directory.

hmm i odnt think it use to do that lol, any idea how I can reshuffle the code or something to make it search through everyfile and return only those files that are equal in size (lenght()? bytes?)

This question has already been answered. Start a new discussion instead.