Program to output a directory and all its files?

Status
Not open for further replies.

iFargle

Linux / HPC / K8s SME
Staff member
Messages
4,403
Location
N/A
Here's what I want it to do

Say I mount a directory /home/user/etc/stuff/

I want a .txt file with all the contents of every directory inside that folder output like this:

addons/hello/data/bin/examples/test.txt
in rows... so for each file, it would output on the next line.

addons/hello/data/bin/examples/asdf.txt
addons/hello/data/bin/examples/test.txt
addons/hello/data/bin/examples/hi.txt

And so on

(the directory really looks like this

/home/user/etc/stuff/addons/hello/data/bin/examples/test.txt
But I don't want the /home/user/etc/stuff/ path. All I want is everything after "stuff"

I want something similar to what the "tree" command does, except output differently.

Is there a way Java can do this? If not, is there a program out there that can?

Also, if it is possible in Java, is there a way we can add stuff to each line? Instead of outputting just the contents like this:

addons/hello/data/bin/examples/asdf.txt
addons/hello/data/bin/examples/test.txt
addons/hello/data/bin/examples/hi.txt

Can it output like this? (I need it like this):

resource.AddFile("addons/hello/data/bin/examples/asdf.txt")
resource.AddFile("addons/hello/data/bin/examples/test.txt")
resource.AddFile("addons/hello/data/bin/examples/hi.txt")
....
etc etc

Thank you for any help :thumbsup:
 
UPDATE: I got one part of this...

Code:
import java.io.*;  
   
public class FileGeneration {  
    public static void main(String[] args) throws IOException {  
   
        File inputFile = new File("/users/ifargle/Desktop/WireModDownload.txt");  
        File outputFile = new File("/users/ifargle/Desktop/OUT.txt");  
   
        FileReader in = new FileReader(inputFile);  
        FileWriter out = new FileWriter(outputFile);  
          
        BufferedReader buffin = new BufferedReader(in);  
        BufferedWriter buffout = new BufferedWriter(out);  
          
        String line;  
        while ( (line = buffin.readLine()) != null ) {  
            buffout.write("resource.AddFile(\"");  
            buffout.write(line);  
            buffout.write("\")");  
            buffout.newLine();  
        }  
          
        // Closing a wrapping Reader/Stream closes wrapped one too  
        buffin.close();  
        buffout.close();  
    }  
}

Now to find a way to get the contents of the directories...
 
edit: if you wanna still go java for funzies. I *think* this gets everything :p
Code:
package FileGeneration;

import java.io.*; 
import java.util.*;
import static java.lang.System.out;
   
public class FileGeneration {  
    //ArrayList stores list of files for dirDrive method
    static ArrayList<String> fullList = new ArrayList<String>();
    public static void main(String[] args) throws IOException {
        //dir is location of directory you want list of files from
        File dir = new File("/test");
        //call dirDrive method
        String[] listFiles = FileGeneration.dirDive(dir);
        for(int z=0;z<listFiles.length;z++) out.println(listFiles[z]);
        
    }
    public static String[] dirDive(File dir){
        //Get list of files/folders in directory
        File[] checkList = dir.listFiles();
        for(int i=0;i<checkList.length;i++){
            //if directory, call dirDrive again with new subfolder
            if(checkList[i].isDirectory())dirDive(checkList[i]);
            //otherwise must be file, so convert to string and add to array
            else{
                String temp = checkList[i].toString();
                fullList.add(temp);
            }
        }
        //convert ArrayList to String Array and return list of files
        String[] result = fullList.toArray(new String[fullList.size()]);
        return result;
    }
}
 
edit: if you wanna still go java for funzies. I *think* this gets everything :p
Code:
package FileGeneration;

import java.io.*; 
import java.util.*;
import static java.lang.System.out;
   
public class FileGeneration {  
    //ArrayList stores list of files for dirDrive method
    static ArrayList<String> fullList = new ArrayList<String>();
    public static void main(String[] args) throws IOException {
        //dir is location of directory you want list of files from
        File dir = new File("/test");
        //call dirDrive method
        String[] listFiles = FileGeneration.dirDive(dir);
        for(int z=0;z<listFiles.length;z++) out.println(listFiles[z]);
        
    }
    public static String[] dirDive(File dir){
        //Get list of files/folders in directory
        File[] checkList = dir.listFiles();
        for(int i=0;i<checkList.length;i++){
            //if directory, call dirDrive again with new subfolder
            if(checkList[i].isDirectory())dirDive(checkList[i]);
            //otherwise must be file, so convert to string and add to array
            else{
                String temp = checkList[i].toString();
                fullList.add(temp);
            }
        }
        //convert ArrayList to String Array and return list of files
        String[] result = fullList.toArray(new String[fullList.size()]);
        return result;
    }
}

Slightly less complicated.
Code:
package com.example.filelister;

import java.io.File;

public class FileGeneration {  

	public static void main(String[] args) {
		File dir = new File(".");
		FileGeneration fg = new FileGeneration();
		fg.dirDive(dir);
	}

	public void dirDive(File dir){
		if (dir.isDirectory()) {
			File[] files = dir.listFiles();
			for(File file : files) {
				dirDive(file);
			}
		} else {
			System.out.println(dir);
		}
	}

}
 
Ah the old file : files :p haven't learned the newer syntax yet, should probably get around to that at some point.
 
Status
Not open for further replies.
Back
Top Bottom