?
Solved

batch file to delete empty folders and files

Posted on 2016-08-23
4
Medium Priority
?
45 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 56

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 benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

752 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