Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.
for %%A in ("c:\research"\*"") do (
if "%%~nxA" NEQ "test1.ico" (
if "%%~nxA" NEQ "test2.ico" (
del "%%A"
)
)
)
~bp
@echo off
REM Remove all subfolders
for /D %%A in ("c:\research\*") do (
rd /S /Q "%%A"
)
REM Remove all files except 2 special files to be kept
for %%A in ("c:\research\*") do (
if "%%~nxA" NEQ "test1.ico" (
if "%%~nxA" NEQ "test2.ico" (
del "%%A"
)
)
)
~bp
REM Remove all files except 2 special files to be kept
for %%A in ("c:\research\*") do (
echo Processing: [%%A]
if "%%~nxA" NEQ "test1.ico" (
if "%%~nxA" NEQ "test2.ico" (
echo Deleting: [%%A]
del "%%A"
)
)
)
~bp
@echo off
::
:: Bye Gastone Canali
::
set filename=%~n0
set logfile="c:\temp\_%filename%.txt"
set null= 2^>^&1
set log=^>^>%logfile% 2^>^&1
:: *** no log remove the :: in the next line
:: set log=
:: ***
set Dir2del=c:\temp\test
:: *** takeown, change permission/attrib
takeown /F "%Dir2del%\*" /R /a /D Y %log%
echo y|cacls "%Dir2del%\*.*" /T /E /C /G %username%:F %log%
attrib -r -h -s "%Dir2del%\*.*" %log%
:: *** remove test if empty
rd /Q "%Dir2del%" %log%
:: *********************************************************
set RES=c:\temp\research
:: *** change DIR
(pushd "%RES%") || goto :_ERR
:: *** takeown, change permission/attrib
takeown /F * /R /a /D Y %log%
echo y|cacls *.* /T /E /C /G %username%:F %log%
attrib -r -h -s *.* %log%
:: *** Remove all subfolders
for /f "delims=:" %%D in ('dir /b /a:d') do (
rmdir /S /Q "%%D"
) %log%
:: *** Remove all files except 2 special files to be kept
for /f "delims=:" %%F in ('dir /b /a:-d^|findstr /i /v "test1.ico test2.ico"') do (
del /F /Q "%%F"
) %log%
:_ERR
:_END
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
Open in new window
will suppress any errors, and will only delete the folder if it is already empty.~bp