I have this bat file created, it is supposed to look at at directory and keep X amount of recent folders (It calculates it by date modified) and deletes the rest. Now I have been testing it out and it works great with empty folders. Once I have files in the folders it keeps giving me "the directory is not empty" and wont let me delete. I have been at it for a bit and could use some help. I need it to delete folders with anything in them. Feel free to try it with your own path. If you feel you have a better code feel free to post. Thanks in advance :
@Echo Off
:: User Variables
:: Set this to the number of folders you want to keep
Set _NumtoKeep=5
:: Set this to the folder that contains the folders to check and delete
Set _Path=C:\Test\databackup
If Exist "%temp%\tf}1{" Del "%temp%\tf}1{"
PushD %_Path%
Set _s=%_NumtoKeep%
If %_NumtoKeep%==1 set _s=single
Echo Please wait, searching for folders other than the %_s% most recent
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
::If you want a prompt to come out
::_Prompt1
::Set /P _resp=Delete All, None, or Prompt for each (A/N/P)?
::If /I "%_resp:~0,1%"=="N" Goto _Done
::If /I "%_resp:~0,1%"=="A" Goto _Removeold
::If /I NOT "%_resp:~0,1%"=="P" (del
::Echo (A/N/P only please)&Goto _Prompt1
::Set _rdflag=
:_Removeold
For /F "tokens=1* skip=3 Delims=:" %%I In ('type "%temp%\tf}1{"') Do (
If "%_rdflag%"=="" echo Deleting
pause
rd /S /q%_rdflag% "%%J")
:_Done
If Exist "%temp%\tf}1{" del "%temp%\tf}1{"
pause
Deleting the oldest folders in a directory
Moderator: DosItHelp
Re: Deleting the oldest folders in a directory
Do you have full acces to these folders? Can they be deleted by GUI?
I find your code a bit difficult to read so I didn't really looked into it very deep.
But deleting should not be a problem unless you do not have enough rights or files are in use in which case you delete them at HKLM logon stage.
You probably need cacls in combination with attrib
I usually get that error when trying to delete folders that have open files in it
You can close open files in windows but I would have to google the procedure (without reboot) to close them myself.
For deleting I use this (but don't use this code as is obviously) It raises acces levels before attempting deletion:
I find your code a bit difficult to read so I didn't really looked into it very deep.
But deleting should not be a problem unless you do not have enough rights or files are in use in which case you delete them at HKLM logon stage.
You probably need cacls in combination with attrib
"the directory is not empty"
I usually get that error when trying to delete folders that have open files in it
You can close open files in windows but I would have to google the procedure (without reboot) to close them myself.
For deleting I use this (but don't use this code as is obviously) It raises acces levels before attempting deletion:
Code: Select all
:RDF ( /FullPathFile: "STRING/BYVAL" /Sho_OK /Sho_ERR_nexist /Sho_ERR_ndeleted /Int_ERR_ndeleted /Int_ERR_nexist /AtStage.HKLM )
::
:: SJAB v1.142c beta
::
::INPUT:
:: @FullPathFile STRING/BYVAL Required; @FullPathFile to delete
::
:: @Sho_OK STRING Optional; output 1 >> :ToConsole ()
:: @Sho_ERR_nexist STRING Optional; output 2 >> :ToConsole ()
:: @Sho_ERR_ndeleted STRING Optional; output 2 >> :ToConsole ()
::
:: @AtStage.HKLM STRING Optional; output 2 >> :AtStage.HKLM.RemoveDir ()
:: @Int_ERR_nexist STRING Optional; Halt on error nexist
:: @Int_ERR_ndeleted STRING Optional; Halt on error ndeleted
::
::OUTPUT:
:: @ERR INT Return; 0 for succes, 1 for failure
::
SetLocal
::(
::set "Debug.Sub=!Sub.%Sub.Depth%.Name!"
::(
::%Debug% "" "inside !Sub.%Sub.Depth%.Name! () Sub.!Sub.Depth!.Par.AsIS:" &echo.!Sub.%Sub.Depth%.Par.AsIS!_
set "Sub.!Sub.Depth!.Use=:RDF ( /FullPathFile: "STRING/BYVAL" /Sho_OK /Sho_ERR_nexist /Sho_ERR_ndeleted /Int_ERR_ndeleted /Int_ERR_nexist /AtStage.HKLM )"
::)
::%Debug% "PROC PAR"
::(
set "Par.FIX=FullPathFile"
set "Par.OPT=Sho_OK Sho_ERR_nexist Sho_ERR_ndeleted Int_ERR_nexist Int_ERR_ndeleted AtStage.HKLM"
%PROC.PAR.FAST% "()"
for %%! in ( FullPathFile ) do %Get.ByVal% "%%~!"
for %%! in (
"nexist"
"ndeleted"
) do if defined Int_ERR_%%~! (
::
set "Sho_ERR_%%~!=/Sho_ERR_%%~!"
)
for %%! in (
"PrefetchCOMMAND"
) do if defined Sho_ERR_ndeleted (
::
set "%%~!="
) else set "%%~!=2>nul"
for %%! in (
"TMP"
) do (
%ERR.Get.PathExists.TokenVAR% %()% >nul
::
if !ERR! equ 0 (
::
set /a TMP.CleanUp = 0
) else (
set /a TMP.CleanUp = 1
::
%MD.FAST% "TMP" /Int_ERR
)
)
::%Debug.Equals% for %%! in ( FullPathFile Sho_OK Sho_ERR_nexist Sho_ERR_ndeleted Int_ERR_ndeleted Int_ERR_nexist AtStage.HKLM ) do echo. %%~!=!%%~!!_
::)
::%Debug% "VALID"
::(
for %%! in (
"FullPathFile"
) do (
%Slash.Rem.LastDoubleorSingle.TokenVAR% %()%
::
set "%%~!.IsDIR="
::
if exist "!%%~!!\" (
::
set /a %%~!.IsDIR = 1
) else if exist "!%%!!" (
::
set /a %%~!.IsDIR = 0
)
if not defined %%~!.IsDIR (
::
if defined Sho_ERR_nexist (
::
echo. &set /p "?=not exist %%~!: '!%%~!!' ^!" <nul
)
::
if defined Int_ERR_nexist (
::
%Pause.UnivLang% %()%
::
%ERR.Set.VALID% /not_exist: %%~!
)
)
)
::)
::%Debug% "Perform"
::(
::%Debug% " Become owner"
::(
%OpenWorkFile% "()"
::
for %%! in (
"!FullPath.WorkFile!\WorkFile.!WorkFile!.CMD"
) do (
>"%%~!" (echo.j)
::
>nul 2>&1 cacls "!FullPathFile!" /t /e /c /p "Administrators":F < "%%~!"
::
>nul 2>&1 del /f /q "%%~!"
)
::
%CloseWorkFile% %()% >nul
::)
::%Debug% " Release folder, file protection"
::(
>nul 2>&1 attrib -r -a -s -h /S /D "!FullPathFile!"
::)
::%Debug% " supressed RDF: '!FullPathFile!'"
::(
if defined FullPathFile.IsDIR if !FullPathFile.IsDIR! equ 1 (
::
2>nul rd /s /q "!FullPathFile!"
) else >nul 2>&1 del /f /q "!FullPathFile!"
::)
if defined FullPathFile.IsDIR if !FullPathFile.IsDIR! equ 1 if exist "!FullPathFile!\" (
::%Debug% " still exists, Prefetched rd: ""!FullPathFile!"" "
echo.>nul
%PrefetchCOMMAND% rd /s /q "!FullPathFile!"
)
set /a ERR = 0
::
if defined FullPathFile.IsDIR if !FullPathFile.IsDIR! equ 1 (
::
if exist "!FullPathFile!\" set /a ERR = 1
) else if exist "!FullPathFile!" set /a ERR = 1
::%Debug% " ERR=!ERR!_"
if !ERR! equ 1 if defined AtStage.HKLM (
::
set /a ERR = 0
for %%! in (
"Int_ERR"
) do if defined %%~!_ndeleted (
::
set "%%~!=/%%~!"
) else set "%%~!="
%RDF.AtStage.HKLM% /FullPathFile: "!FullPathFile!" !Sho_OK! /Int_ERR
)
if !ERR! neq 0 (
::
if defined Sho_ERR_ndeleted (
::
echo.
::
set /p "?= RDF: '!FullPathFile!' [FAILED] ^!" <nul
)
::
if defined Int_ERR_ndeleted (
::
%pause.UnivLang% %()%
::
%ERR.Set.THIS% "FullPathFile=!FullPathFile!_" "RDF: '!FullPathFile!' [FAILED] ^!"
)
) else if defined Sho_OK (
::
echo.
::
set /p "?= RDF: '!FullPathFile!' [OK]" <nul
)
set /a ERR.MEM = !ERR!
::(
if !TMP.CleanUp! neq 0 (
::
%RDF.AtStage.HKLM% /FullPathFile: "TMP" /Sho_OK /Int_ERR
)
::)
set /a ERR = !ERR.MEM!
if not defined FullPathFile.IsDIR if defined Sho_ERR_nexist (
::
set /a ERR = 1
)
::)
::
::%Debug% /Pause "outside !Sub.%Sub.Depth%.Name! ()"
(
EndLocal
::
set /a ERR = %ERR%
)
::
goto :eof ()
::)