1.

Solve : remove empty subfolders?

Answer»

I have a main folder called : basic and under it are subfolders 1, 2, 3. What I would like to do is remove all the subfolders under "basic" that are empty. Is there are batchfile command that can do this? I've tried: rd c:\basic /s but that removes even "basic".Is this an exercise? Is this needed often?
Why don't you like empty folders?
at the command line type
xcopy /?
Or look HERE

If can copy files and directories and ignore empty directories.

Quote from: pearlyshells on June 21, 2010, 11:57:20 AM

I have a main folder called : basic and under it are subfolders 1, 2, 3. What I would like to do is remove all the subfolders under "basic" that are empty. Is there are batchfile command that can do this? I've tried: rd c:\basic /s but that removes even "basic".

rd /s basic is the secret. Then make new basic directory. md basic

C:\>rd /?
Removes (deletes) a directory.

RMDIR [/S] [/Q] [drive:]path
RD [/S] [/Q] [drive:]path

/S Removes all directories and files in the specified directory
in addition to the directory itself. Used to remove a directory
tree.

/Q Quiet mode, do not ask if ok to remove a directory tree with /S

C:\>rd /S basicthis would be a daily routine task so I prefer not to leave the empty folders there. It is part of a backup routine that backs up folders and files to a central repository and then another application moves it directly to tape. So, every night, a new folder is created. When the tape move OCCURS the files are purged but the folder remains..

I see the second replies suggestion is to remove the entire folder including basic with the RD command and then in the batchfile force the creation of another Basic folder. I thought of that earlier but thought there might be another command that would not require me to recreate the BASIC folder again.

ThanksThis may help.

Code: [Select]@echo off
pushd c:\basic

for /r %%r in (.) do (
DIR /b "%%r" | find /v "" > nul && echo %%r NOT empty || rd %%r && echo %%r IS empty...DELETED
)

popd

You may have to change c:\basic to the correct path.If the subfolders are ALWAYS just 1 LEVEL down from the folder called basic then a simple script like this should do the trick, run from the basic folder

Code: [Select]@echo off
echo Remove empty folders
echo Press a key to start...
pause>nul
for /f "delims=" %%D in ('dir /b /ad') do (
dir "%%D" | find "0 File(s)">nul && (
rd "%%D" && echo Removed empty directory: %%D
)
)
echo Finished
echo Press a key to quit...
pause>nul

it worked. the folders that were not empty stayed and those that were are GONE. Many , many thanks.


Discussion

No Comment Found