Lorand G
Lorand G

Reputation: 13

Delete 2 level subfolders but leave 1 level subfolders

my problem is that we have a network share with different subfolders with ACL permissions etc.

I need to keep the 1st level but also need to delete everything in these folders.

share:\Test1\Test11\... share:\Test1\Test12\... share:\Test2\Test21\... share:\Test2\Test22\... share:\Test3\Test31\... share:\Test3\Test32\... share:\Test...

The result hast to be: share:\Test1\ share:\Test2\ share:\Test3\ share:\Test...

I found solutions with one root folder but I have multiple with different names, not all is testxxx.

Any help is appreciated. Thank you.

Upvotes: 1

Views: 65

Answers (1)

user6811411
user6811411

Reputation:

  • Iterate the 1st level folders
  • step in and out the folders with PushD/PopD
  • issue an RD /S /Q . 2>NUL this will erase all files and folders and try to erase the current folder what will fail thus requirung error output redirection to NUL

@Echo off
Pushd "share:\"
For /F "delims=" %%A in ('dir /B/AD') Do (
  Pushd "%%~A"
  RD /S /Q . 2>NUL
  PopD
)
PopD

Upvotes: 1

Related Questions