HelpdeskJBC
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
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
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:\
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
If you're OK with starting it the batch file with a parameter.
filename.cmd V:\
Then try this.
filename.cmd V:\
Then try this.
for /f "delims=" %%d in ('dir "%1" /ad/b/s ^| sort /R') do rd "%%d"
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.