How to? Delete subfolders but not the folder itself

Jan 21, 2011
(Take Command 18.0 x64)

If I have folders:

all containing files, I can run 'del a /sxz' to delete all the folder contents and all the folders - but what if I want to only delete the subfolders, so I want to end up with just the 'a' folder?

I've tried 'del a\* /sxz' hoping that would iterate through the a\* folders and do the 'del /sxz' on those, but it just does the same as 'del a /sxz' and tries to delete the 'a' folder.

Any other ideas?

(More information)
Even though the problem happens with local folders as well, I'm actually trying to delete folders on an FTP server, so I can't use 'for' to iterate through the sub folders.

I don't have permission to delete the 'a' folder itself on the FTP server, so I'm getting an error message, so I can't just recreate the folder after deleting it. I'm trying to prevent the error message being generated.

Charles Dye

Super Moderator
Staff member
May 20, 2008
Albuquerque, NM
If you don't have permission to delete the subdirectory, then it won't be deleted -- no problem! You can use /NE to suppress the error message. You'll probably want to do this anyway, to squelch all those file-not-found messages for empty directories.

For a local drive (not FTP), note that you cannot remove the current directory. You can PUSHD to the directory in question, and POPD afterwards, to prevent it from being removed. I don't think this approach will work for FTP, though.
Jan 21, 2011
Unfortunately the /NE doesn't squelch the errors. I'd tried /E before, but /NE doesn't work either.

>del "ftp://testuser:[email protected]/home/testuser/*" /sx /y /q /NE /e
TCC: FTP protocol error: 550 Remove directory operation failed. "/home/testuser"

(If I could get rid of the errors, that'd be sufficient)

The FTP protocol errors seem to be reported regardless of the /E or /NE setting. I don't know if there's anyway to supporess the protocol errors as well
You could try the following:

1) Search for the Directory Change Command for FTP (could be "CWD" for "Change working directory").
2) Change with this command INTO this Directory first (in your example the "a" DIR).
3) Execute your delete command sequence.

You have to ensure that point 2 is executed on the FTP server, not local. Also see the tip from samintz to handle with IFTP.

For local Directories you could use PUSD/POPD (as Charles said) or CDD as step 2 ...
Jan 21, 2011
You could use IFTP. This is untested code.
IFTP "ftp://testuser:[email protected]/home/testuser"
del /sexyz ftp:*
Unfortunately that has the same problem :-(

> iftp "ftp://testuser:[email protected]/home/testuser"

>del ftp:* /sexyz
TCC: FTP protocol error: 550 Remove directory operation failed. "/home/testuser"
     0 files deleted
I think Charles' suggestion just to redirect stderr to NULL is probably what I'll end up with.
Jan 21, 2011
If you want delete (local) only the dir b and c and further Dirs and files in this structure EXCEPT the Dir a (with files), you can use the following IF you are in a already:

del *.* /s+1 /z /x /y
Unfortunately, the '/s+1' affects the 'deleting files', but it does not affect the '/x' (deleting empty folders) action.

So, if 'a' is empty, then "del a /s+1 /x" will still delete the 'a' folder.

(If you are in the 'a' folder, then you don't need the '+1' because it won't delete the current folder - BUT you can't use that with FTP actions)
Jan 21, 2011
for /a:d /h %fldr in ("a\*") del /a: /s /e /x /y "a\%fldr"

wouldn't this solve the problem...
It would IF it was local folders - but I'm doing it on FTP folders, and FOR doesn't work with FTP.

To do it that sort of way, I'd have to use '"dir /b .... > file" to dump the list of folders to a file, then use FOR to go through that. That is WAY too complicated for what I'm trying to do.

I just thought there may be a way that I'd missed - sort of like the '/s+n' option, but which applied to the deleting empty folders option