Deleting 0 bytes in Different Directories

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Deleting 0 bytes in Different Directories

Post by MigrationUser »

13 May 2013 14:22
shadowfury0409

Yo! I'm just a newbie in using CMD command line. and I really need your help for this one. I just want a batch file or cmd line command that will delete 0 bytes folder in different directory (C:\ ; C:\New Folder ; C:\Test ; C:\Users\ProfileName\Desktop ; \C:\Users\ProfileName\Documents ). Please include some explanation so that i can fully understand every line. Please. Please Please. Thanks in Advance. :pray: :pray: :pray:

Last edited by shadowfury0409 (17 May 2013 03:19)

----------------------------

#2 13 May 2013 15:22
npocmaka

Code: Select all

@echo off
setLocal EnableDelayedExpansion
for /f "tokens=3 delims= " %%A in ('dir   /s /a-d %1 2^>nul  ^| find "File"') do (
	set /A size=%%A

)

if !size! equ 0 (
	echo "%~1" is empty
	rd /s /q "%~1"
) else (
        echo "%~1" is not empty
)
endLocal
----------------------------

#3 13 May 2013 15:47
shadowfury0409


Thanks alot. where should i put the directory where i want to delete the 0 byte folder? is it in the "FILE" thing?

----------------------------

#4 13 May 2013 15:50
npocmaka


just pass it as the first parameter to the subroutine/batfile .It's the %1 ..

----------------------------

#5 13 May 2013 16:07
shadowfury0409


Are you saying that i will add the directory C:\ProgramData to 1% and this should be like this?

Code: Select all

for /f "tokens=3 delims= " %%A in ('dir   /s /a-d %1C:\ProgramData 2^>nul  ^| find "File"')
Sorry for the trouble man. I'm a noob when it comes to cmd.

----------------------------

#6 13 May 2013 17:55
npocmaka


here's a good explanation about command line parameters -> https://www.robvanderwoude.com/parameters.php

Just save the snippet as file e.g. deletempty.bat and call it like this:

call deletempty.bat c:\myDir

----------------------------

#7 14 May 2013 00:40
foxidrive


Your code is going to check only one directory.

I think the OP wants to delete every empty folder in a tree.

I think this will do that - test it in a sample directory.
EG: if you CD to "c:\test folder\" and run it then it will process every folder under "c:\test folder\"

Code: Select all

@echo off
for /f "delims=" %%a in ('dir /ad /b /s ^|sort /r') do rd "%%a" 2>nul
Last edited by foxidrive (14 May 2013 00:44)

----------------------------

#8 14 May 2013 13:53
shadowfury0409


Thank you npocmaka and foxidrive. :)

----------------------------

#9 14 May 2013 14:14
shadowfury0409

Code: Select all

@echo off
for /f "delims=" %%a in ('dir /ad /b /s ^|sort /r') do rd "%%a" 2>nul
is there a way to convert this to a plain cmd line? I mean i just want to paste the line in cmd.
I tried to paste this directly to cmd and it didnt work out. Thanks in advance. :)

----------------------------

#10 14 May 2013 14:51
bluesxman


Change %%a to %a to use at the command line.

cmd | *sh | ruby | chef

----------------------------

#11 14 May 2013 15:08
shadowfury0409
bluesxman wrote:

Change %%a to %a to use at the command line.
Thanks for the hepl. it worked.

Just another question. what if I want to delete 0 byte folders of different directories? I tried this one but it didnt work.

cd C:\Test\ for /f "delims=" %a in ('dir /ad /b /s ^|sort /r') do rd "%a" 2>nul
C:\New Folder\ for /f "delims=" %a in ('dir /ad /b /s ^|sort /r') do rd "%a" 2>nul
C:\New Folder2\New\ for /f "delims=" %a in ('dir /ad /b /s ^|sort /r') do rd "%a" 2>nul
cd\

----------------------------

#12 14 May 2013 16:11
foxidrive


From a cmd prompt:

Code: Select all

for /f "delims=" %a in ('dir "C:\New Folder2\New\" /ad /b /s ^|sort /r') do rd "%a" 2>nul
Last edited by foxidrive (14 May 2013 16:11)

----------------------------

#13 14 May 2013 16:24
shadowfury0409
foxidrive wrote:

From a cmd prompt:

Code: Select all

    for /f "delims=" %a in ('dir "C:\New Folder2\New\" /ad /b /s ^|sort /r') do rd "%a" 2>nul
WOW! now its working. Thanks man. You helped me alot. :)

Code: Select all

for /f "delims=" %a in ('dir "C:\New Folder" "C:\Test" /ad /b /s ^|sort /r') do rd "%a" 2>nul
----------------------------

#14 16 May 2013 18:44
shadowfury0409


I Just Noticed that RD command also deleted an empty folder within a folder(subfolder). What if I just want to delete an empty folder in a directory?
Just in C:\Test and not in C:\Test\Subfolders?
Is there any other command for this?

----------------------------

#15 16 May 2013 18:51
npocmaka


try without /s switch

----------------------------

#16 16 May 2013 20:20
shadowfury0409


tried without the /s and nothing happened. :(

----------------------------

#17 16 May 2013 20:30
npocmaka

Code: Select all

for /f "delims=" %a in ('dir "C:\some_dir" /ad /b ') do  rd  "%~dpa\%a">nul
This should work..

Last edited by npocmaka (16 May 2013 20:30)

----------------------------

#18 17 May 2013 02:38
shadowfury0409


for /f "delims=" %a in ('dir "C:\New Folder" /ad /b') do rd "%~dpa\%a">nul
still not working.. it says

C:\>rd "C:\\New Folder (2)" 1>nul
The system cannot find find the file specified.

i thinnk there's something wrong with the C:\\ (double slash)

----------------------------

#19 17 May 2013 03:35
foxidrive


From the command line, this should work.

Code: Select all

pushd "c:\new folder" &&for /f "delims=" %a in ('dir /ad /b') do rd  "%~fa" 2>nul
The double && is important, so that it doesn't delete folders if the directory is mistyped.

Last edited by foxidrive (17 May 2013 03:39)

----------------------------

#20 17 May 2013 10:17
shadowfury0409
foxidrive wrote:

From the command line, this should work.

Code: Select all

    pushd "c:\new folder" &&for /f "delims=" %a in ('dir /ad /b') do rd  "%~fa" 2>nul

    The double && is important, so that it doesn't delete folders if the directory is mistyped.

Thanks a lot.
Post Reply