Solved

batch file to delete empty folders and files

Posted on 2016-08-23
4
24 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
ID: 41766954
Use
del /q /s  ...
and
rd /q ...
1
 
LVL 52

Assisted Solution

by:Bill Prew
Bill Prew earned 150 total points (awarded by participants)
ID: 41766976
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)
ID: 41786216
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

914 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

17 Experts available now in Live!

Get 1:1 Help Now