How can I delete all empty folders older than 2 days?

I am creating a Script that removes all empty folders with subfolders in the path. Now I have to do, if the folder was created 2 days ago, and its empty, it should be deleted along with other empty folders older than 2 days. And if not, it shouldn't be deleted. And I also need / need to make this remote folder get logged. I did it with 5 Filetypes, but I don't know how this would work with folders.

I'm really new to Batch so I don't know what I should be doing. I checked Google, but the results didn't match my problem. Hope someone can help me.

Here is my code that I have written so far:

@echo off

::start path / Variables
set startdir="C:\Users" 
::Initialize the Variable
set /a loop=0
::Directory c:\temp\ will be created, if the folder not exists 
if not exist c:\temp\ md c:\temp\
::Create Logfile for Deleted Filetypes in C:\Log\LOG_Useless_File_Killer.txt
echo ----------------------------------- >> C:\Log\LOG_Useless_File_Killer.txt
echo Logfile from: %date% at %time% >> C:\Log\LOG_Useless_File_Killer.txt
echo. >> C:\Log\LOG_Useless_File_Killer.txt
::this 5 Filetypes are going to be deleted immediately
del C:\Users\Thumbs.db /f /q /s >> C:\Log\LOG_Useless_File_Killer.txt
del C:\Users\desktop.ini /f /q /s >> C:\Log\LOG_Useless_File_Killer.txt 
del C:\Users\*.DS_Store /f /q /s >> C:\Log\LOG_Useless_File_Killer.txt 
del C:\Users\*._DS_Store /f /q /s >> C:\Log\LOG_Useless_File_Killer.txt 
del C:\Users\*.desktop /f /q /s >> C:\Log\LOG_Useless_File_Killer.txt 
::Writes the directorys in c:\temp\tmp.txt. 
dir /AD /b /s %startdir% > c:\temp\tmp.txt
::at goto start it will be start again
:start
::the Variable %loop% is increased by 1  
set /a loop =%loop%+1
::at 5 --> goto exit
if %loop%==5 goto exit
::Under 5 --> goto start 
else goto start
::deletes every empty folder which is written in C:\temp\tmp.txt
for /F "delims=" %%i in (c:\temp\tmp.txt) do rd "%%i" 
::--> goto start and begins again 
goto start
::%loop% has reached 5 --> exit
:exit
::Console window will be closed 
exit

pause 

exit

      

+3


source to share


1 answer


Pradeep is absolutely right on how to best use the files.

To delete folders, try this:
FORFILES -p "/ D -15 / C" cmd / c IF @isdir == TRUE rd / S / Q @path "
/ D for the number of days, you can play with the command options to meet the exact requirements.



You can also use environment variables so that you can simply delete files only for the currently logged in user. For example, you can use %HOMEPATH%\Desktop

to access the desktop of the current user. More environment variables here .

0


source







All Articles