-1

I've previously posted here about asking how to clear files from subdirectories and it turned out pretty well.

Since I'm dealing with student accounts (I'm the school lab custodian) and I need to clear them out before the new school year starts, I decided to make a batch file for it.

Students tend to make folders of their documents within their respective accounts. I need a way to delete those folders without deleting the directory where it is placed.

For a visual context: Student accounts hierarchy chart

I need to clear out the contents at the level of CL01-01 without it being deleted as well.

I tried using rmdir but it wiped out all directories under STUDENTS (thankfully, I'm still on the testing phase of this and used a dummy STUDENTS folder)

  • Please read my answer on [How to delete files/subfolders in a specific directory at the command prompt in Windows?](https://stackoverflow.com/a/50656521/3074564) There can be used: `for /D %%I in ("C:\Path\To\STUDENTS\*") do for /D %%J in ("%%I\*") do for /D %%K in ("%%J\*") do pushd "%%K" 2>nul && ( rd /Q /S "%%K" 2>nul & popd )` – Mofi Jun 03 '22 at 05:51
  • `for /D` ignores hidden directories. So if there are hidden subdirectories in `STUDENTS` or in `CL`, `CS`, ... or in `CL01`, `CL02`, `C"01`, ..., there is required in the batch file the slower executed command line: `for /F "eol=| delims=" %%I in ('dir "C:\Path\To\STUDENTS\*" /AD-L /B 2^>nul') do for /F "eol=| delims=" %%J in ('dir "C:\Path\To\STUDENTS\%%I\*" /AD-L /B 2^>nul') do for /F "eol=| delims=" %%K in ('dir "C:\Path\To\STUDENTS\%%I\%%J\*" /AD-L /B 2^>nul') do pushd "C:\Path\To\STUDENTS\%%I\%%J\%%K" 2>nul && ( rd /Q /S "C:\Path\To\STUDENTS\%%I\%%J\%%K" 2>nul & popd )` – Mofi Jun 03 '22 at 05:54
  • That long command line results in processing also the hidden directories and ignores just junctions and symbolic directory links in the first three folder levels. – Mofi Jun 03 '22 at 05:55

0 Answers0