Solved

batch file to delete empty folders and files

Posted on 2016-08-23
4
38 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 28

Expert Comment

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

691 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