How to create object for each file from a directory using groovy?

Hi,
i need to check the presence of .txt file in a directory.
if the directory is empty the test should fail.
if files exist i need to create an object for each file.

What i tried:

def dir = “c:\temp”
try {
File file = new File(dir);
// check if directory is empty
if (file.isDirectory()) {
String list = file.list();
if (list == null || list.length == 0) {
System.out.println(“Dirctory is empty!”);
}else {
System.out.println(“Dirctory is not empty!”);
System.out.println(list.length)
file.eachFileMatch(~/.*.txt/) { f →
System.out.println(f.getName())
}
}
} else {
System.out.println(“Not a directory!”);
}

} catch (IOException ex) {
ex.printStackTrace();
}

1- im not able to fail the test if directory contains 0 files. i tried with FileNotFound Exception, it doent fail the test.

The code returns the list of filename if the directory contains files.
like for example:

file1.txt
file2.txt

2- How can i create a variable for each filename?
like:
def f1 = (“c:\temp\file1.txt”)
def f2 = (“c:\temp\file2.txt”)

hello,

read your files to the list like

File folder = new File("/Users/you/folder/");
File[] listOfFiles = folder.listFiles();

for (File file : listOfFiles) {
    if (file.isFile()) {
        System.out.println(file.getName());
    }
}

Hi,
i`m able to read files.
i want to create an object for each file name returned by the code.
im working on it…
Your code is correct too.

hi,

object or String variable?

String variable

hi,

not sure is this any help

File folder = new File("C:/Users/fitim/Desktop/files");
File[] listOfFiles = folder.listFiles();

Map<String, String> map = new HashMap<String, String>();


for (File file : listOfFiles) {
	if (file.isFile()) {
		//System.out.println(file.getName());
		//String path = file.getAbsolutePath();
		//System.out.println(file.getAbsolutePath());
		map.put(file.getName(),file.getAbsolutePath())
	}
}

Iterator<String> iterator = map.keySet().iterator();

while(iterator.hasNext()){
	String key   = iterator.next();
	String value = map.get(key);
	println("key: "+key+" value: "+value)
}

key: file1.txt value: C:\Users\fitim\Desktop\files\file1.txt
key: file3.txt value: C:\Users\fitim\Desktop\files\file3.txt
key: file2.txt value: C:\Users\fitim\Desktop\files\file2.txt

hello you,

sorted map example

File folder = new File("C:/Users/fitim/Desktop/files");
File[] listOfFiles = folder.listFiles();

Map<String, String> map = new HashMap<String, String>();


for (File file : listOfFiles) {
	if (file.isFile()) {
		//System.out.println(file.getName());
		//String path = file.getAbsolutePath();
		//System.out.println(file.getAbsolutePath());
		map.put(file.getName(),file.getAbsolutePath())
	}
}

Map<String, String> treeMap = new TreeMap<String, String>(map);
printMap(treeMap);


public static void printMap(Map<String,String> map) {
	Set s = map.entrySet();
	Iterator it = s.iterator();
	while ( it.hasNext() ) {
	   Map.Entry entry = (Map.Entry) it.next();
	   String key = (String) entry.getKey();
	   String value = (String) entry.getValue();
	   System.out.println(key + " => " + value);
	}//while
	System.out.println("========================");
}//printMap

file1.txt => C:\Users\fitim\Desktop\files\file1.txt
file2.txt => C:\Users\fitim\Desktop\files\file2.txt
file3.txt => C:\Users\fitim\Desktop\files\file3.txt

Hi,
it works when i call String key inside the while loop.
if i call it outside it get only the first filename.

If i can get all filename as string:
suppose i receive this:
f1 => file1.txt
f2 => file2.txt

Then i would like use them like
def file1 =‘new File(f1)’
file1.renameTo ‘newfile.groovy’

Maybe i should get filename in array outside the while loop

hi,

ok then we will need to do another approach, will info after my work day

Hi,
i tried this approach and i can get the result.

File file = new File(“C:/temp”);
String fileList = file.list();
List arrayToList = Arrays.asList(fileList);
System.out.println(arrayToList);
System.out.println (fileList[0]);
System.out.println(fileList[1]);

The result is:
file1.txt
file2.txt

We can close this thread, Timo_Kuisma1 was helpful for me.