Folder to delete folders

I found code to delete folders, in this case deleting everything except 'n' # folders. I created 10 test folders, plus 1 that were already there. I want to delete everything except 4. The code works, it leaves 4 of my test folders, except that it also leaves another folder. Is there any attribute of another folder that is checked in a batch file that does not allow it to be deleted? It was created through work a couple of weeks ago.

Here is the code I stole (but don't really understand the details):

rem DOS - Delete Folders if # folders > n
@Echo Off
:: User Variables
:: Set this to the number of folders you want to keep
Set _NumtoKeep=4
:: Set this to the folder that contains the folders to check and delete
Set _Path=C:\MyFolder_Temp\FolderTest
If Exist "%temp%\tf}1{" Del "%temp%\tf}1{"
PushD %_Path%
Set _s=%_NumtoKeep%
If %_NumtoKeep%==1 set _s=single
  For /F "tokens=* skip=%_NumtoKeep%" %%I In ('dir "%_Path%" /AD /B /O-D /TW') Do (
    If Exist "%temp%\tf}1{" (
      Echo %%I:%%~fI >>"%temp%\tf}1{"
    ) Else (
      Echo.>"%temp%\tf}1{"
      Echo Do you wish to delete the following folders?>>"%temp%\tf}1{"
      Echo Date       Name>>"%temp%\tf}1{"
      Echo %%I:%%~fI >>"%temp%\tf}1{"
  ))
PopD
If Not Exist "%temp%\tf}1{" Echo No Folders Found to delete & Goto _Done
Type "%temp%\tf}1{" | More
Set _rdflag= /q
Goto _Removeold
Set _rdflag=
:_Removeold
For /F "tokens=1* skip=3 Delims=:" %%I In ('type "%temp%\tf}1{"') Do (
 If "%_rdflag%"=="" Echo Deleting
 rd /s%_rdflag% "%%J")
:_Done
If Exist "%temp%\tf}1{" Del "%temp%\tf}1{"
+3
source share
3 answers

The line For /Fis the key. It takes the results of part of the string in single quotes and iterates over it.

, , n _NumtoKeep .

Set _rdflag= /q, , .

+1

RD C:\BLAH /S /Q Windows

0

There are two tools that I can come up with to check directory permissions.

To remove readonly in directories use

attrib -r /s <directory_name>

To view directory permissions help

cacls /?
0
source

Source: https://habr.com/ru/post/1723514/


All Articles