Link to home
Start Free TrialLog in
Avatar of Luis Diaz
Luis DiazFlag for Colombia

asked on

Windows Cmd: fastest way to remove subfolders from a folder and excluding a folder

Hello experts,

I am looking for the fastest way to delete the various subfolders from a rootfolder and exclude some folder withtout removing the root folder and reporting the exclude folder

Example
C:\rootfolder\ contains subfolders such as:

C:\rootfolder\ogs
C:\rootfolder\bin
C:\rootfolder\conf

The command should remove C:\rootfolder\ogs and C:\rootfolder\bin and keep C:\rootfolder\conf.

The objective is to launch the command withtout saving it a bat file.

If you have questions, please contact me.

Thank you very much for your help.
Avatar of oBdA
oBdA

Go PowerShell. Test mode comes for free; this will only show what it would delete. Remove the -WhatIf at the end to run it for real:
Get-ChildItem c:\rootfolder -Directory -Exclude conf | Remove-Item -Recurse -WhatIf

Open in new window

Try this
1. Go to command prompt and type these commands
2. RD /S /Q C:\rootfolder\ogs
3. RD /S /Q C:\rootfolder\bin
Avatar of Luis Diaz

ASKER

I know this command however how do you do if you have 100 folders to delete and keep 1.
Are you going to launch this 99 times?
I don't think this is the fastest way.
@Obda: possible to have a cmd & windows batch command?
Well, nothing keeps you from calling powershell from batch/cmd. As before in test mode with -WhatIf:
powershell.exe -Command "Get-ChildItem 'c:\rootfolder' -Directory -Exclude 'conf' | Remove-Item -Recurse -WhatIf"

Open in new window

Note: the single quotes are not required as longs as the names don't contain spaces; they're just there to show how to easiest handle names with spaces in this situation.
This is probably the shortest I can think of from a command prompt.  It will display "The process cannot access the file because it is being used by another process." when it finishes but that's okay, that's because we CD to the folder we want to keep and so it can't be deleted because it's "in use".

cd /d "C:\rootfolder\conf" && rd /s /q ..

Open in new window

Edit:

As oBdA accurately points out below, this only leaves the actual conf  folder, not it's content.


»bp
And for the verbose BAT script approach you can build off of:



»bp
Careful with Bill Prew one-liner
This will keep the conf folder item itself, but it will remove any content that currently is below conf!
As I've understood your question, you want to keep the folder intact, including its content.
If you want to preserve the entire "conf" folder and it's content then it gets longer, something like this perhaps.

for /d %A in ("b:\ee\temp\rdtest\*") do @if "%~nxA" NEQ "d2" @rd /s /q %A

Open in new window


»bp
Thank you for your feedback.
Bill: I am in with your last proposal however I don't see which is the folder (and its contain) to exclude d2?

-b:\ee\temp\rdtest\* is the root folder in which are located subfolders to remove
-Excluded folder (and it contains) is which one?
Sorry, that was my test here, meant to adjust to your examples.  That would look like:

for /d %A in ("C:\rootfolder\*") do @if "%~nxA" NEQ "conf" @rd /s /q %A

Open in new window


»bp
Thank you very much Bill. Las two questions. Conf folder contain will remain intact?
Possible to include in the same command additional folder to exclude?
Thank you very much Bill. Las two questions. Conf folder contain will remain intact?

Yes.

Possible to include in the same command additional folder to exclude?

Not easily, you would nearly double the length of the command and more prone to mistakes.


»bp
Actually, maybe two folders isn't quite as bad as I was thinking, but test it carefully there...

for /d %A in ("C:\rootfolder\*") do @if "%~nxA" NEQ "conf" @if "%~nxA" NEQ "conf2" @rd /s /q %A

Open in new window


»bp
Ok, I will test it.
Thank you very much.
Bill, I tested but I got the following error message:
The system cannot find the file specified. I presume that a pushd is required. Is it possible to do a cd to get the root folder and then launch for command?

Thank you in advance for your help.
Should not need a pushd, please paste your command here,

~bp
for /d %A in (C:\Users\ld\Downloads\Functions\*) do @if "%~nxA" NEQ "conf" @if "%~nxA" NEQ "conf2" @rd /s /q %A

Open in new window

And I launch the command from
C:\Windows\system32

Thank you
On mobile, harder to type, but does Functions folder exist in that location, and does it contain sub folders?

~bp
Yes they exist and it contains the subfolders:


@User generated imageObda I tested the following in cmd but I don't get the delete:

powershell.exe -Command "Get-ChildItem 'C:\Users\ld16\Downloads\Functions\*' -Directory -Exclude 'conf' | Remove-Item -Recurse"

Thank you in advance for your help.
That's because of the "\*" you added at the end of the path.
I added the test mode -WhatIf again, so that you'll see what it would delete.
If you want to see what it actually deletes, you can add -Verbose.
powershell.exe -Command "Get-ChildItem 'C:\Users\ld16\Downloads\Functions' -Directory -Exclude 'conf' | Remove-Item -Recurse -WhatIf"

Open in new window

Not sure what you are doing wrong there, or what is "interesting" about your files / folders, but I constructed a test here and it worked fine.  As long as you have privs to the folders, etc...

Sun 02/03/2019 12:03:58.06 B:\EE\EE29134389>tree /a /f

Folder PATH listing for volume bill

B:.
|   EE29134389.bat
|
\---Users
    \---ld
        \---Downloads
            \---Functions
                +---conf
                |   |   f3.txt
                |   |
                |   \---d3
                |           f13.txt
                |
                +---empty
                +---conf2
                |   |   f4.txt
                |   |
                |   \---d4
                |           f14.txt
                |
                +---logs
                |   |   f1.txt
                |   |
                |   \---d1
                |           f11.txt
                |
                \---bin
                    |   f2.txt
                    |
                    \---d2
                            f12.txt


Sun 02/03/2019 12:04:02.77 B:\EE\EE29134389>for /d %A in (B:\EE\EE29134389\Users\ld\Downloads\Functions\*) do @if "%~nxA" NEQ "conf" @if "%~nxA" NEQ "conf2" @rd /s /q %A

Sun 02/03/2019 12:04:08.67 B:\EE\EE29134389>tree /a /f

Folder PATH listing for volume bill

B:.
|   EE29134389.bat
|
\---Users
    \---ld
        \---Downloads
            \---Functions
                +---conf
                |   |   f3.txt
                |   |
                |   \---d3
                |           f13.txt
                |
                \---conf2
                    |   f4.txt
                    |
                    \---d4
                            f14.txt

Open in new window


»bp
@oBda I tested and it works!
User generated imageIf I want to add multiple folders to exclude such as conf and conf2 how should I proceed.
Bill: When I tested without the Function\* it works however it remove the various Function folder I suppose this * is not properly interpreted by cmd.
Thank you in advance for your help.
If you do the following command, what is the exact output?

dir C:\Users\ld\Downloads\Functions

Open in new window


»bp
SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Bill:
This what I get with dir command on Functions folder:

02-Feb-19  08:20 PM    <DIR>          .
02-Feb-19  08:20 PM    <DIR>          ..
02-Feb-19  08:18 PM    <DIR>          conf
02-Feb-19  08:18 PM    <DIR>          conf2
02-Feb-19  08:18 PM    <DIR>          New folder
               0 File(s)              0 bytes
               5 Dir(s)  341,961,846,784 bytes free

Open in new window

Given that you have folders with spaces in their names, you will need to quote the folder path in the RD, as in:

for /d %A in (C:\Users\ld\Downloads\Functions\*) do @if "%~nxA" NEQ "conf" @if "%~nxA" NEQ "conf2" @rd /s /q "%A"

Open in new window


»bp
Thank you Bill. Tested and it works!
Possible to add echo to know what has been deleted and the end of the command execution?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you Bill, I tested and it works! Thank you again for your help.