M
M
maxe-1012018-08-30 13:58:50
cmd/bat
maxe-101, 2018-08-30 13:58:50

How to create a Bat-ik that would delete folders with a few exceptions?

Hello, there was a problem with creating a script that should recursively delete folders along the path C:\Users\Desktop\Bat\Test, while not touching 3 folders and 2 files. Can't make an exception for more than 1 folder.
Please help and if possible, describe what the actions mean. I 've been fiddling with this all day :(
Here's what I could write

@echo off
setlocal enableextensions enabledelayedexpansion

:: Дирректория
set Folder=C:\Users\vasma\Desktop\Bat\Test

:: Поддиректория (или файл), которая НЕ должна удаляться
set NotDeleted="conf" "log" "dumps"

for /f "tokens=*" %%i in ('dir "%Folder%" /b /a:d') do (
  set /a bDelete = 1

  for %%j in (%NotDeleted%) do (
    if /i "%%i" equ "%%~j" set /a bDelete = 0
  )
if !bDelete! equ 1 rd /s /q "%Folder%\%%i"
)

endlocal
exit /b 0

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
maxe-101, 2018-08-30
@maxe-101

The script is working, it deletes folders and their contents, except for the specified one, but does not automatically delete files located in the root folder!
My problem was in the wrong spelling of my folders in the script.

M
moropsk, 2018-08-30
@moropsk

And if on Powershell (I use it myself)
https://gallery.technet.microsoft.com/Delete-files...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question