Solved

batch file to delete empty folders and files

Posted on 2016-08-23
4
21 Views
Last Modified: 2016-09-25
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

0
Comment
Question by:JayB501
  • 2
4 Comments
 
LVL 28

Expert Comment

by:Michael Pfister
Comment Utility
Use
del /q /s  ...
and
rd /q ...
1
 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 150 total points (awarded by participants)
Comment Utility
The DEL you had should be fine, you were only wanting to delete one file, so there will be no prompt to confirm there.  I don't think you want DEL /Q /S, that could potentially delete more than the one file.

On the RD, if the directory is indeed not empty, then you do not want to delete it.  It will give you a warning that it isn't empty, but should not be prompting you to continue, etc.

If the folder is empty then no prompt should be displayed.

What prompts are you actually seeing that you want to skip, please be specific.

I would suggest changing the RD to this, which will suppress the "directory not empty" messages.

rd "%%~i" 2>NUL


~bp
0
 
LVL 28

Accepted Solution

by:
Michael Pfister earned 350 total points (awarded by participants)
Comment Utility
line 14: forfiles /p "%%~t" /d -1 /c "cmd /c del /s @path"

Here is the del /s and it will prompt if not combined with /q.
But Bill is correct you probably don't want to do that. So just omit the /s from the del command and it shouldn't prompt anymore.

 forfiles /p "%%~t" /d -1 /c "cmd /c del @path"

Open in new window

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now