@echo off if not "%1"=="" (cd "%1" & goto a); set deldir=f:\zheng\delete\ copy d.bat %deldir%d.bat cd /d %deldir% :a dir /ad /b>dir.txt for /f "tokens=1" %%i in (dir.txt) do (copy d.bat %%i\ & call %%i\d %%i) for /f "tokens=1" %%i in (dir.txt) do rmdir %%i del /f /q /a *.*&cd.. 把第三行set deldir=f:\zheng\delete\ 改成要删的目录,保存为d.bat,运行
使用递归调用删除 public static boolean deleteDir(File dir) { if (dir.isDirectory()) { String[] children = dir.list(); for (int i=0; i<children.length; i++) { boolean success = deleteDir(new File(dir, children[i])); if (!success) { return false; } } } return dir.delete(); }
自己看
* @param args删除文件夹下所有文件
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
delDir("D:\\test");
}
public static void delDir(String path){
File dir=new File(path);
if(dir.exists()){
File[] tmp=dir.listFiles();
for(int i=0;i<tmp.length;i++){
if(tmp[i].isDirectory()){
delDir(path+"/"+tmp[i].getName());
}
else{
tmp[i].delete();
}
}
dir.delete();
}
}}
Runtime rt=Runtime.getRuntime();
// freeMemory方法获得运行环境自由变量
System.out.println(rt.freeMemory());
System.out.println(rt.totalMemory());
try
{
//凡是命令行中可执行的语句,都可以用exec方法执行
rt.exec("del c:\upload\*.*");
}
catch(Exception e)
{
e.printStackTrace();
}
if not "%1"=="" (cd "%1" & goto a);
set deldir=f:\zheng\delete\
copy d.bat %deldir%d.bat
cd /d %deldir%
:a
dir /ad /b>dir.txt
for /f "tokens=1" %%i in (dir.txt) do (copy d.bat %%i\ & call %%i\d %%i)
for /f "tokens=1" %%i in (dir.txt) do rmdir %%i
del /f /q /a *.*&cd..
把第三行set deldir=f:\zheng\delete\
改成要删的目录,保存为d.bat,运行
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}