Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
337 views
in Technique[技术] by (71.8m points)

How to exempt file or folder deletion in below code

@echo off
set dump_path=C:est
set dump_path1=C:estxyz
set dump_path2=C:estxyz.py
 

set max_days=0
 

forfiles -p %dump_path% -m *.* -d -%max_days% -c "cmd  /c del /q @path"
 
forfiles -p %dump_path% -d -%max_days% -c "cmd /c IF @isdir == TRUE rd /S /Q @path"

How to exempt dump_path1 and dump_path2 from in the above code. it is a periodic file and folder deletion script and i want to exempt few folder and files from deletion in the same. tried "IF not -p %dump_path1%" in the last 2 lines of the script.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (71.8m points)

You can try:

setlocal enabledelayedexpansion
set "dump_path=C:est"
set "dump_path1=!dump_path!xyz"
set "dump_path2=!dump_path!xyz.py"
set "str=!dump_path1! !dump_path2!"
set "max_days=0"
for /f "tokens=* delims=" %%a in ('forfiles -p !dump_path! -m *.* -d -!max_days!^| findstr /v /i "!str!"') do (
    set "noext=%%~dpna"
    if "!noext!"=="%%a" (call rmdir %%a) else (call del %%a)
)
endlocal

If for some reason, the files/folders you want to skip contain <"spaces">, you must break up the findstr command and search for it/them literally, i.e.:

for /f "tokens=* delims=" %%a in ('forfiles -p !dump_path! -m *.* -d -!max_days!^| findstr /v /i /c:"!dump_path1!"^| findstr /v /i /c:"!dump_path2!"') do (
    set "noext=%%~dpna"
    if "!noext!"=="%%a" (call rmdir %%a) else (call del %%a)
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
...