I have 2 folders, each containing dozens of batch files (*.bat
).
The batch files containing text similar to either
del /f/q F:\MEDIA\IMAGE99\2010\270\z\4034\123.tif > nul
del /f/q F:\MEDIA\IMAGE99\2010\266\z\3025\456.tif > nul
del /f/q F:\MEDIA\IMAGE99\2010\267\z\3025\789.tif > nul
del /f/q F:\MEDIA\IMAGE99\2010\286\z\9025\101.tif > nul
del /f/q F:\MEDIA\IMAGE99\2010\272\z\6029\112.tif > nul
del /f/q F:\MEDIA\IMAGE99\2010\258\z\4034\134.tif > nul
or
rmdir /q F:\MEDIA\IMAGE99\2010\270\z\4034
rmdir /q F:\MEDIA\IMAGE99\2010\266\z\3025
rmdir /q F:\MEDIA\IMAGE99\2010\267\z\3025
rmdir /q F:\MEDIA\IMAGE99\2010\286\z\9025
rmdir /q F:\MEDIA\IMAGE99\2010\272\z\6029
rmdir /q F:\MEDIA\IMAGE99\2010\258\z\4034
In Java, I list each batch File
in each folder, and cycle through the list, executing each batch file as follows:
public static boolean batch(File file) {
boolean handled = false;
Process process = null;
try {
process = Runtime.getRuntime().exec("cmd /c start " + file);
handled = process.waitFor() == 0;
} catch (Exception ex) {
// handling removed for example purposes
}
return handled;
}
After the method returns, I delete the batch file.
The problem is, none of the commands within my batch files run (the files and folders I request deleted or removed are not), and the Java process simply continues and deletes batch file itself.
The batch files are located in folder d:\working\spaced folder\purge\batch_files\
Having written this out, I suspect my problem is that I'm passing a file path with a space in it to the exec()
method.
Is my suspicion correct? If so, how do I resolve it? If not, what might the problem be?
I'm going to look into Java: Execute /cmd /c start path-with-spaces\program.exe now that I've considered it.
UPDATE
Per the comments below, I've changed my code, but now the output hangs at waitFor()
and the batch file is not being processed (the files I request deleted are still there).
Code:
String commandString = "cmd /c \"" + file +"\"";
logger.info("COMMAND " + commandString);
process = Runtime.getRuntime().exec(commandString);
logger.info("WAITING FOR " + commandString);
handled = process.waitFor() == 0;
logger.info("HANDLED " + commandString + " = " + handled);
Output:
COMMAND : cmd /c "d:\working\spaced folder\purge\deleteBatch\F_140.bat"
WAITING FOR : cmd /c "d:\working\spaced folder\purge\deleteBatch\F_140.bat"