Radix
Radix

Reputation: 1347

Batch File to Delete Specific Subfolders

I have three directories, and each of these directories contains 8 subdirectories, and each of those subdirectories contains 2 folders that I want to delete.

The structure:

[Edited the path-name]

C:/Process/CIM
             /AB_CIM
                    /LOG
                    /DATA
             /CC_CIM
                    /LOG
                    /DATA
             /CIM_RD
                    ...
           ...

C:/Process/DIM
             /AB_DIM
                    /LOG
                    /DATA
             /CC_DIM
                    /LOG
                    /DATA
             /DIM_RD
                    ...
             ...

C:/Process/RRS
             ...

So basically I want to remove LOG and DATA in all of those subdirectories. I thought of doing this using two separate batch files***:

The first batch file would contain:

call secondBatchFile.cmd CIM
call secondBatchFile.cmd DIM
call secondBatchFile.cmd RRS

Now, I'm not sure how to write the second batch file since those subdirectories have different names (but a common part: AB_*, CC_*, *_RD, etc). I was wondering if someone could help me with it.

Thanks for your help in advance.

*** The reason for doing this using two separate batch files is that I might sometimes need to keep LOG and DATA for one of the parent directories (e.g. CIM), so this way I can just comment out only one line of the first batch file and then run it.

Upvotes: 0

Views: 1372

Answers (2)

E Brown
E Brown

Reputation: 722

If the 8 sub-folders are always the same pattern, i.e. AB_*, CC_*, *_RD, etc, the second batch file could be something like:

cd C:\%1%

rmdir AB_%1%\LOG
rmdir AB_%1%\DATA

rmdir CC_%1%\LOG
rmdir CC_%1%\DATA

rmdir %1%_RD\LOG
rmdir %1%_RD\DATA
...

cd c:\

Upvotes: 1

Marc
Marc

Reputation: 11633

You could do something like this if you're confident that LOG and DATA folders in other directories won't be picked up. Comment out the actual delete in the code below and review the .dat file output before executing.

REM Output all LOG and DATA sub-directories into corresponding DAT files
dir /ad/s/b log* > log_directories.dat
dir /ad/s/b data* > data_directories.dat

REM remove every entry listed in log_directories.dat
for /f %%i in (log_directories.dat) do rd/s %%i

REM remove every entry listed in data_directories.dat
for /f %%i in (data_directories.dat) do rd/s %%i

If you run this from C:, you're probably going to get directories you don't want. But assuming all of your targets are grouped under a dedicated sub-directory (and assuming you run the .bat from that dedicated directory), this won't be a problem.

And by default, this solution gives you your desired log of which directories it will be deleting (log will be overwritten for each run though).

Upvotes: 1

Related Questions