Java代碼(需要安裝java環境)
import java.io.File;
import java.io.IOException;
/**
* @author 王大仙兒
* @date 2019/12/16 16:47
*/
public class CleanNullFile {
public static void main(String[] args) {
if (args[0].isEmpty()) {
System.out.println("請輸入路徑");
} else {
for (String path : args) {
File file = null;
try {
file = new File(path + File.separator + "正在刪除空文件夾");
if(file.createNewFile()){
clean(path);
}else{
System.err.println("操作文件夾失敗,請檢查目錄權限");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (file != null) {
file.delete();
}
}
}
}
}
private static void clean(String path) {
File[] files = new File(path).listFiles();
if (files.length == 0) {
File file = new File(path);
if (file.delete()) {
System.out.println("刪除空文件夾:" + file.getPath());
} else {
System.err.println("刪除文件夾失敗:" + file.getPath());
}
clean(file.getParent());
} else {
for (File file : files) {
if (file.isDirectory()) {
clean(file.getPath());
}
}
}
}
}