I would like to delete all folders except MYFOLDER found in a parent directory but I cant get it done?
for %%i in ("C:\Parent") do if not "%%i"=="MYFOLDER" del /f /q "%%i
could somebody please check the code?
(I'm using *.bat)
I would like to delete all folders except MYFOLDER found in a parent directory but I cant get it done?
for %%i in ("C:\Parent") do if not "%%i"=="MYFOLDER" del /f /q "%%i
could somebody please check the code?
(I'm using *.bat)
This should help you:
for /d %%i in ("C:\Parent\*") do if /i not "%%~nxi"=="MYFOLDER" del /s /q "%%i"
%%~nxi
stands for? It doesn't work for me, but %%i
does. –
Adlai Dir
all folders and check one by one if it is not MYFOLDER, if is not - delete it.
Please remove echo
when running the real job.
for /d %%i in (C:\Parent\*) do (
if /i "%%i" NEQ ".exe" echo rd /S /Q %%i
)
for /d
instead of iterating over dir
output with for /f
. –
Neuburger Or simply copy MYFOLDER to a temp folder, delete all files, and copy back MYFOLDER. Unless MYFOLDER is huge and takes time to copy, this should work just as good.
© 2022 - 2024 — McMap. All rights reserved.