PNouma2121
PNouma2121

Reputation: 67

How to delete a specific sub-folder in a parent folder using a batch file?

I would like to create a batch file deleting or removing the folder C:\temp\root\students\type1, its subfolder and all the files.

The folders and files I do have are as follows:

C:\temp
C:\temp\root
C:\temp\root\students
C:\temp\root\tutors
C:\temp\root\students\type1
C:\temp\root\students\type2
C:\temp\root\tutors\type1
C:\temp\root\tutors\type2
C:\temp\root\students\type1\details.txt
C:\temp\root\students\type1\assignment1
C:\temp\root\students\type1\assignment1\results.txt

The folder C:\temp\root\students\type1\assignment1 is specified in the batch file.

I would like to go one folder/directory up and delete or remove C:\temp\root\students\type1 in my batch file (test.bat).

Please help me with this.

Upvotes: 2

Views: 432

Answers (2)

Anon Coward
Anon Coward

Reputation: 10825

You can use a for loop to get the parent folder for a given folder:

set target_dir=C:\temp\root\students\type1.test\assignment1

for %%a in ("%target_dir%") do (
    echo Removing %%~dpa%
    rd /s/q %%~dpa%
)

Upvotes: 1

Use this:

rd /s /q C:\temp\root\students\type1

It will remove files and folders recursively, caution, without prompt. It's kind of a substitute for the good old deltree. Best.

EDIT: within your notepad, create your mygoodbatch.bat with this content:

md C:\temp
md C:\temp\root
md C:\temp\root\students
md C:\temp\root\tutors
md C:\temp\root\students\type1
md C:\temp\root\students\type2
md C:\temp\root\tutors\type1
md C:\temp\root\tutors\type2
md C:\temp\root\students\type1\details.txt
md C:\temp\root\students\type1\assignment1
md C:\temp\root\students\type1\assignment1\results.txt
rd /s /q C:\temp\root\students\type1

(md is the make dir dos command, so all the lines beginning with it are creating the folders - the last line is the one to del your level 4AA)

Save this batch on the root of your disk and then run it. I really wish this is what you want. What I yet didn't understand is: you are creating a directory structure and, at the same time, excluding level 4AA... is that it?

Upvotes: 2

Related Questions