Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

batch file to delete empty folders and files

Posted on 2016-08-23
4
Medium Priority
?
64 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 29

Expert Comment

by:Michael Pfister
ID: 41766954
Use
del /q /s  ...
and
rd /q ...
1
 
LVL 58

Assisted Solution

by:Bill Prew
Bill Prew earned 600 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 29

Accepted Solution

by:
Michael Pfister earned 1400 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

604 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