删除所有文件夹的脚本除外

时间:2016-07-30 08:59:47

标签: batch-file cmd

我找到了一个删除除了某个文件夹之外的所有文件夹的脚本

for /d %i in ("C:\test\*") do if /i not "%~nxi"=="test2" rd /q "%i"

如何更改此项以添加更多我不想删除的文件夹?

1 个答案:

答案 0 :(得分:1)

@echo off
set "list=test1 test2 test3"
setlocal enableDelayedExpansion
set "delete=0"
pushd "C:\test\"
for /d %%i in (*) do (
   set "delete=1"
   for %%# in (%list%) do (
    if /i "%%i" equ "%%#" (
        set "delete=0"
        echo  %%i will be not deleted
    )
  )
  if !delete!==1 (
    rd /s /q "%%i"
  )

)
popd