0

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
Mofi
  • 46,139
  • 17
  • 80
  • 143
Chaz1864
  • 3
  • 1
  • The only time you would want to use `FORFILES` is if you want to select files that are older than X amount of days. Otherwise a standard `FOR` command is the most efficient for selecting specific files. But in your case since you just want to remove everything, using the `DEL` or `RMDIR` commands would be the most efficient solution. – Squashman Feb 05 '22 at 18:01
  • Thank you for this! I had been using ForFiles for some other tasks involving the date of files. So my mind stuck with what I had been using lately and tried to force the usage of ForFiles. rmdir was definitely the easiest way to get this done! – Chaz1864 Feb 05 '22 at 20:32

1 Answers1

0

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
Queeg
  • 7,748
  • 1
  • 16
  • 42
  • Perfect would be `pushd "D:\CAP\Logs\test" 2>nul && ( rd /Q /S "D:\CAP\Logs\test" 2>nul & popd )` as described by my answer on [How to delete files/subfolders in a specific directory at the command prompt in Windows?](https://stackoverflow.com/a/50656521/3074564) That does not remove the directory `D:\CAP\Logs\test` itself, just all files and subdirectories in the directory. Keeping the directory `D:\CAP\Logs\test` itself could be important in case of special NTFS permissions set for this directory. Well this command line could be used even without stopping and restarting the *Tomcat* service. – Mofi Feb 05 '22 at 20:58
  • In my case, I wanted the test directory to be deleted along with all subfolders & files. Also, in the particular situation I'm concerned about, I need to stop the tomcat service for this issue. That said, thank you for the link, as it does help explain everything in your command line, which helps to educate me further. – Chaz1864 Feb 05 '22 at 21:09