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
HelpdeskJBCAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lee W, MVPTechnology and Business Process AdvisorCommented:
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.
0
HelpdeskJBCAuthor Commented:
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:\
0
Lee W, MVPTechnology and Business Process AdvisorCommented:
Then use

cd /d %1

/d indicates that CD should change to the drive letter as well.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

MagaduCommented:
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.
0
MagaduCommented:
Please Ignore my comment. It's wrong and leew's way work's great.
0
AmazingTechCommented:
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

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.