Link to home
Start Free TrialLog in
Avatar of HelpdeskJBC
HelpdeskJBCFlag for Austria

asked on

script to delete empty folders (comand line)

for /f "usebackq delims=" %%d in (`"dir /ad/b/s | sort /R"`) do rd "%%d"

this code works fine but only when the batch file is in the same directory
we want to use a variable in this code, or define the directory where to start the deletion process
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

Add a line before it -

cd %1

Then to specify a directory, do so at the command line - for example, if you want to run this on the C:\TEMP folder, then you would type:

MyBatch.cmd "c:\temp"

c:\temp is then assigned to %1 (quotes are required if the folder has spaces, as a best practice, just ALWAYS put quotes around it).

the CD changes into C:\temp and now your command does what you want it to do at the appropriate folder.
Avatar of HelpdeskJBC

ASKER

it work wehen it is on the same partition, but
we would like to use it on a other partition (network drive)

batchfile on drive D:\ and it should run on V:\
ASKER CERTIFIED SOLUTION
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

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
for /f "usebackq delims=" %%d in (`"dir %1 /b /o:-n /a:d"`) do rd %1"%%d"

I used
/o:-n to reverse sort it by Name (so you dont need to use Sort) (Not sure why you need this)
/a:d - To show only Folders.

use: DelEmpty.bat C:\Folder\

I hope this helps.
Please Ignore my comment. It's wrong and leew's way work's great.
Avatar of AmazingTech
AmazingTech

If you're OK with starting it the batch file with a parameter.

filename.cmd V:\

Then try this.
for /f "delims=" %%d in ('dir "%1" /ad/b/s ^| sort /R') do rd "%%d"

Open in new window