批量删除带有例外的空目录

时间:2018-10-15 17:35:06

标签: batch-file cmd

我最近开始创建一些小的批处理文件,因为我认为批处理本身很有趣,而且还因为它有助于使我定期执行的某些操作自动化。

我正在使用的脚本是一个程序,该程序删除文件夹中的所有目录(到目前为止,已作为%UserProfile%放入),并允许忽略Exception文件中的所有内容。我不想要确切路径,而是想要它,以便如果文件夹路径中的任何地方都存在异常,则需要将其排除。

这就是我所拥有的(记住我不是很好):

@ECHO OFF

CD %UserProfile%

SET /A Violation=0
SET "ExceptionFile=%*"

SETLOCAL ENABLEDELAYEDEXPANSION
SHIFT
ECHO Starting...

ECHO Scan started at %TIME% > "%UserProfile%\Log.txt"
FOR /f "usebackq delims=" %%D in (`"dir /ad/b/s | sort /R"`) DO CALL :DIREXPANDER "%%D"
ECHO Scan Finished at %TIME% >> "%UserProfile%\Log.txt"
GOTO:EOF

:DIREXPANDER
    SET /A Violation=0
    SET Directory=%1
    ECHO %Directory%
    FOR /F %%X IN (%ExceptionFile%) DO (
        SET "Exception=%%X"
        ECHO %Directory% Compared to %Directory:!Exception!=% >>  "%UserProfile%\Test.txt"
        If NOT %Directory%==%Directory:!Exception!=% (
            ECHO EXCEPTION: %Directory% Violates !Exception! >> "%UserProfile%\Log.txt"
            REM SET /A Violation=1
        )
    )

    IF %Violation%==0 (
        RMDIR %Directory% 2>NUL && ECHO DELETION: %Directory% >> "%UserProfile%\Log.txt"
    )
    GOTO:EOF

如果我替换!Exception!使用实际的String,它可以正常工作-但使用变量时,它似乎不起作用。

感谢您发现的任何建议或问题!

1 个答案:

答案 0 :(得分:1)

非常确定您可以在不使用CALL的情况下将其写入标签。

public class WeatherRootObject extends SugarRecord {

public WeatherRootObject()
{

}

public WeatherRootObject(Current current, Location location, Forecast forecast) {
    this.current = current;
    this.location = location;
    this.forecast = forecast;
}

@SerializedName("current")
private Current current;

@SerializedName("location")
private Location location;

@SerializedName("forecast")
private Forecast forecast;

public void setCurrent(Current current){
    this.current = current;
}

public Current getCurrent(){
    return current;
}

public void setLocation(Location location){
    this.location = location;
}

public Location getLocation(){
    return location;
}

public void setForecast(Forecast forecast){
    this.forecast = forecast;
}

public Forecast getForecast(){
    return forecast;
}

@Override
public String toString(){
    return 
        "WeatherRootObject{" + 
        "current = '" + current + '\'' + 
        ",location = '" + location + '\'' + 
        ",forecast = '" + forecast + '\'' + 
        "}";
    }
}    

此嵌套代码在我的计算机上快5秒钟。