Link to home
Start Free TrialLog in
Avatar of JayB501
JayB501

asked on

batch file to delete empty folders and files

I am using the below script that runs every night to clear files older than 1 day and delete empty folders.  Problem is if I run the script manually it asks if I want to delete the folder.  Is there a way to just have the script do it with out asking so I can get it working on a schedule?

@echo off
setlocal

rem Define base folder to Processing
set TopLevel=E:\owlTcpXfer\server\download5000-all

rem process each folder contained in base folder
for /d %%t in ("%TopLevel%\*.*") do (
    rem Display folder currently being processed
	echo Processing '%%~t' ...

    rem remove files older than one day from this folder
    rem *** add /s option to pure old files in all subfolders as well ***
    forfiles /p "%%~t" /d -1 /c "cmd /c del /s @path"

    rem Remove any empty folders below thus user folder
	for /f "delims=" %%i in ('dir /s /b /ad "%%t" ^| sort /r') do (rd "%%~i" >NUL
    )
)

Open in new window

Avatar of Michael Pfister
Michael Pfister
Flag of Germany image

Use
del /q /s  ...
and
rd /q ...
SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial