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
Uses Apache Commons-IO
package com.stackoverflow.windows;
import java.io.File;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.NullOutputStream;
public class Command {
private Command() {}
public static boolean batch(File file) {
boolean handled = false;
Process process = null;
ProcessBuilder pb = new ProcessBuilder("cmd", "/c", file.getAbsolutePath());
pb.redirectErrorStream(true);
try {
process = pb.start();
IOUtils.copy(process.getInputStream(), new NullOutputStream());
handled = process.waitFor() == 0;
} catch (Exception ignore) {
// Only throws an IOException we're trying to avoid anyway,
// and an expected InterruptedException
// handled will be false
} finally {
if (process != null) {
IOUtils.closeQuietly(process.getInputStream());
}
}
return handled;
}
}