views:

59

answers:

5

I'm running a script made in Groovy from Soap UI and the script needs to generate lots of files. Those files have also in the name two numbers from a list (all the combinations in that list are different), and there are 1303 combinations available and the script generates just 1235 files.

A part of the code is:

filename = groovyUtils.projectPath + "\\" + "$file"+"_OK.txt";
targetFile = new File(filename);
targetFile.createNewFile();

where $file is actually that part of the file name which include those 2 combinations from that list:

file = "abc" + "-$firstNumer"+"_$secondNumber"

For those file which are not created is a message returned:"The filename, directory name or volume label syntax is incorrect".

I've tried puting another path:

filename = "D:\\rez\\" + "\\" + "$file"+"_OK.txt";
targetFile = new File(filename);
targetFile.createNewFile(); 

and also:

File parentFolder = new File("D:\\rez\\");
File targetFile = new File(parentFolder, "$file"+"_OK.txt");
targetFile.createNewFile();

(which I've found here: http://stackoverflow.com/questions/131901/what-are-possible-reasons-for-java-io-ioexception-the-filename-directory-name) but nothing worked.

I have no ideea where the problem is. Is strange that 1235 files are created ok, and the rest of them, 68 aren't created at all.

Thanks,

+1  A: 

My guess is that some of the files have illegal characters in their paths. Exactly which characters are illegal is platform specific, e.g. on Windows they are

\ / : * ? " < > |

Why don't you log the full path of the file before targetFile.createNewFile(); is called and also log whether this method succeeded or not, e.g.

filename = groovyUtils.projectPath + "\\" + "$file"+"_OK.txt";
targetFile = new File(filename);
println "attempting to create file: $targetFile"

if (targetFile.createNewFile()) {
    println "Successfully created file $targetFile"
} else {
    println "Failed to create file $targetFile"
}

When the process is finished, check the logs and I suspect you'll see a common pattern in the ""Failed to create file...." messages

Don
A: 

File.createNewFile() returns false when a file or directory with that name already exists. In all other failure cases (security, I/O) it throws an exception.

Evaluate createNewFile()'s return value or, additionally, use the File.exists() method:

File file = new File("foo")
// works the first time
createNewFile(file)
// prints an error message
createNewFile(file)

void createNewFile(File file) {
    if (!file.createNewFile()) {
        assert file.exists()
        println file.getPath() + " already exists."
    }
}
robbbert