Luis Diaz
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.
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.
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
1. Go to command prompt and type these commands
2. RD /S /Q C:\rootfolder\ogs
3. RD /S /Q C:\rootfolder\bin
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.
Are you going to launch this 99 times?
I don't think this is the fastest way.
ASKER
@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"
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".
As oBdA accurately points out below, this only leaves the actual conf folder, not it's content.
»bp
cd /d "C:\rootfolder\conf" && rd /s /q ..
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
»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.
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.
»bp
for /d %A in ("b:\ee\temp\rdtest\*") do @if "%~nxA" NEQ "d2" @rd /s /q %A
»bp
ASKER
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?
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:
»bp
for /d %A in ("C:\rootfolder\*") do @if "%~nxA" NEQ "conf" @rd /s /q %A
»bp
ASKER
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?
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...
»bp
for /d %A in ("C:\rootfolder\*") do @if "%~nxA" NEQ "conf" @if "%~nxA" NEQ "conf2" @rd /s /q %A
»bp
ASKER
Ok, I will test it.
Thank you very much.
Thank you very much.
ASKER
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.
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
~bp
ASKER
for /d %A in (C:\Users\ld\Downloads\Functions\*) do @if "%~nxA" NEQ "conf" @if "%~nxA" NEQ "conf2" @rd /s /q %A
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
~bp
ASKER
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.
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"
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...
»bp
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
»bp
ASKER
ASKER
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.
Thank you in advance for your help.
If you do the following command, what is the exact output?
»bp
dir C:\Users\ld\Downloads\Functions
»bp
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Bill:
This what I get with dir command on Functions folder:
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
Given that you have folders with spaces in their names, you will need to quote the folder path in the RD, as in:
»bp
for /d %A in (C:\Users\ld\Downloads\Functions\*) do @if "%~nxA" NEQ "conf" @if "%~nxA" NEQ "conf2" @rd /s /q "%A"
»bp
ASKER
Thank you Bill. Tested and it works!
Possible to add echo to know what has been deleted and the end of the command execution?
Possible to add echo to know what has been deleted and the end of the command execution?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you Bill, I tested and it works! Thank you again for your help.
Open in new window