工作原理示例

工作原理示例

我正在尝试编写一个批处理文件,循环遍历目录并删除任何名为 的文件夹\SAMPLE

这是我迄今为止的代码,如能提供任何帮助我将不胜感激!

 @echo off 
 
 title WAIT !
 
 Set "sourceDir=z:\FOLDERS"
 
 IF NOT EXIST "%sourceDir%" (echo.Could not find %sourceDir% & GoTo:done)

 :: delete files 
 For /F "Delims=" %%! in ('Dir "%sourceDir%\") do (   
     rd "%%!" /s /q "%sourcedir%\ * \SAMPLE"
  )
  
 :done title,Done.......
 
 echo.&pause>nul

答案1

for /d /r Z:\Folders %d in (SAMPLE) do if exist "%d" rd /s /q "%d"

您需要在批处理文件中使用 %%d,但这是基本形式。我使用类似的结构来清理 Subversion 工作目录以删除 .svn 文件夹。

编辑:修复了 for 命令的语法。发布时我正在凭记忆工作,不在 Windows 系统上。抱歉。

答案2

凯迪拉克回答了这个问题。我把它写成了一个批处理脚本,它接受选项参数,并有一些配置,这可能有助于 OP 和其他想要做些不同事情的人。echo 语句是可选的,如果您不想在操作中输出任何输出,可以将其删除。如果有任何错误,请告诉我。

工作原理示例

此示例将通配符选项设置为 true,请参阅下面的代码以获取更多信息

dir D:\deltest

Wed 04/03/2013  07:05 PM    <DIR>          KEEPME
Wed 04/03/2013  07:05 PM    <DIR>          SAMPLE
Wed 04/03/2013  07:05 PM    <DIR>          SAMPLE2

delete-sample-dir.bat d:\deltest

Searching for *SAMPLE* in D:\deltest
Deleting the folder D:\deltest\SAMPLE
Deleting the folder D:\deltest\SAMPLE2

dir D:\deltest

Wed 04/03/2013  07:05 PM    <DIR>          KEEPME

代碼delete-sample-dir.bat

    @echo off 

    REM set the name of the directory you would like to target for deleting
    set dirname=SAMPLE

    REM set the following to "true" if you want to select any directory that includes the name, e.g., a wildcard match
    set usewildcard=true


    REM --DO NOT EDIT BELOW THIS LINE---------------

    REM sentinel value for loop
    set found=false

    REM If true surround in wildcards
    if %usewildcard% == true (
        set dirname=*%dirname%*
    ) 

    REM use current working directory or take the directory path provided as the first command line parameter
    REM NOTE: do not add a trailing backslash, that is added in the for loop, so use "C:" not "C:\"
    set directorytosearch=%cd%
    if NOT "%1%" == "" (
        set directorytosearch=%1%
    )
    echo Searching for %dirname% in %directorytosearch%


    REM /r for files
    REM /d for directories
    REM /r /d for files and directories
    for /d %%i in (%directorytosearch%\%dirname%) do (
        IF EXIST %%i (
            REM change the sentinel value
            set found=true

            echo Deleting the folder %%i
            REM Delete a folder, even if not empty, and don't prompt for confirmation
            rmdir /s /q %%i
        )
    )

    REM logic to do if no files were found
    if NOT "%found%" == "true" (
        echo No directories were found with the name of %dirname%
    )

答案3

@user213878 评论解决方案:

代碼删除文件夹调试工具

@echo off
color 1F
echo.
echo Delete Debug Folder

SET FOLDER_PATH=C:\TFS\AddIn\bin
SET SUBFOLDER_NAME=Debug
SET FULLPATH=%FOLDER_PATH%\%SUBFOLDER_NAME%
IF NOT EXIST "%FULLPATH%" (echo.Could not find %FULLPATH% &GoTo:done)

echo.
echo Deleting...
for /d /r %FOLDER_PATH% %%d in (%SUBFOLDER_NAME%) DO @if exist %%d echo "%%d" && rd /s/q "%%d"

:done title,Done.......

:EOF
echo Waiting seconds
timeout /t 3 /nobreak > NUL

相关内容