I need a batch file that prompts the user for a file path, and the first 3 letters in the folder name. Then the batch goes and deletes all the folders and files in the folders with these 3 letters, but keeps the folder starting with the 3 letters. I got a batch file that deletes all files in folder and sub-folders, but I also need it to delete the sub-folders. Here was my code for deleting all files:
@echo off
set /p filepath=Path to Main Directory:
set /p string=First Letters of Sub-Folders to be Cleansed:
FOR /D %%G in ("%filepath%\%string%*") DO del "%%G\*.*" /q /s
echo Done
pause
Then I tried a lot of different ways to also delete sub folders of %%G. I got as far where it names the sub folders of %%G here:
@echo off
set /p filepath=Path to Main Directory:
set /p string=First Letters of Sub-Folders to be Cleansed:
FOR /D %%G in ("%filepath%\%string%*") DO for /r /d %%i in (%%G\*) do del "%%G\*.*" /q && echo %%i
echo Done
pause
But when I try to add && rd /s /q %%i at the end it says file and path not found.
No need to recursively look for folders in your “exp1” folder. You can simply use FOR /D to list the child folders and then use RD /Q /S to remove the entire folder hierarchy rooted on each child. Also, since all the child folders are being removed anyway, there is no need to do a recursive file delete.