繁体   English   中英

在Windows cmd Shell脚本方面需要您的帮助

[英]Need your assistance with Windows cmd shell script

我有以下脚本,该脚本从ROOT目录中删除所有子目录,并删除ROOT目录中除* .bat文件之外的所有文件。

pause
FOR /R C:\Temp\ %%F IN (*.*) DO IF NOT "%%~xF" == ".bat" DEL /F /S "%%F"
for /d %%i in ("c:\Temp\*") do rmdir /s /q "%%i"
pause

帮助我改善脚本,使其还可以扫描ROOT子目录中的* .bat文件,并删除所有具有其他扩展名的文件。

测试。 如果控制台中显示的是需要执行的操作,请从破坏性行中删除echo命令,并保留rmdirdel命令。

@echo off

    setlocal enableextensions

    set "root=c:\temp"

    rem For the %root% tree
    for /r "%root%" %%d in (.) do (

        rem Test for the presence of .bat files under directory 
        dir /s /b "%%~fd\*.bat" 2>nul | find /v "" > nul 
        if errorlevel 1 (

            rem If there is no .bat files under directory, remove directory
            echo(**** rmdir /s /q "%%~fd"

        ) else (

            rem There are bat files in directory or under it. Remove anything else in this directory
            for %%f in ("%%~fd\*") do if not "%%~xf"==".bat" echo(-------- del "%%~ff"

        )

    )

编辑-适应注释-如何适应代码以便处理多个根文件夹? 获取代码并将其转换为子例程,然后使用相应的根调用它。 遍历根目录列表

@echo off

    setlocal enableextensions

    for %%r in ("c:\temp" "c:\temp2" "c:\temp3") do call :processRoot %%r

    endlocal
    exit /b

:processRoot
    setlocal enableextensions disabledelayedexpansion

    set "root=%~1"

    rem For the %root% tree
    for /r "%root%" %%d in (.) do (

        rem Test for the presence of .bat files under directory 
        dir /a /s /b "%%~fd\*.bat" 2>nul | find /v "" > nul 
        if errorlevel 1 (

            rem If there is no .bat files under directory, remove directory
            echo(**** rmdir /s /q "%%~fd"

        ) else (

            rem There are bat files in directory or under it. Remove anything else in this directory
            for %%f in ("%%~fd\*") do if not "%%~xf"==".bat" echo(-------- del "%%~ff"

        )

    )
    endlocal
    goto :eof

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM