Search code examples
batch-fileforfiles

Batch script to restart Windows service and delete directories


I've been working on a batch script to stop Tomcat, then recursively delete some directories, and lastly restart the service.

I got it to a point where it was completing everything, except for deleting the directories. It clears out all files of the directories, but leave the sub-folders intact. Would someone please help me figure out where I'm going wrong and what I'm missing?

I know it has to do with del @file only hitting files itself. But not sure what I need in lieu of that to clear out everything.

This is what I have so far:

ECHO Stopping Tomcat Service
sc stop Tomcat9
#waiting 30 seconds for Tomcat to stop
timeout /T 30

#deleting JMS db files
ECHO Deleting JMS DB Files
ForFiles /p "D:\CAP\Logs\test" /s /c "cmd /c del @file"
timeout /T 10

#starting Tomcat service
ECHO Starting Tomcat Service
sc start Tomcat9

pause

Solution

  • You want to delete everything inside D:\CAP\Logs\test? Then just run

    rmdir /S /Q D:\CAP\Logs\test
    mkdir D:\CAP\Logs\test