How do I get this FOR /D command to work?

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

How do I get this FOR /D command to work?

Post by MigrationUser »

25 Dec 2010 02:37
Jason404

I have made a script that does come file moving operations, and then tries to delete any empty directories. I cannot get this part to work.

Code: Select all

for /D /r %%F IN ("%_path1%\*") DO rd "%%F"
While it can delete the top level directories in the path when they do not contain directories themselves, it fails when the directories are nested.

How can I make it first delete the directories at the deepest level? I do not want the script deleting directories when it still contains something, so I cannot use...

Code: Select all

rd /s "%%F"
Thanks.

Last edited by Jason404 (25 Dec 2010 02:38)

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

#2 25 Dec 2010 16:27
RG


This should do it.

Code: Select all

@ECHO OFF
SET TempFile=%TEMP%\%~n0.txt
SET _path1=YourFullPath
XCOPY "%_path1%" /L /S > "%TempFile%"
FOR /F "tokens=*" %%A in ('DIR "%_path1%" /AD /B /S') DO (
   FIND "%%A" "%TempFile%" > nul
   IF ERRORLEVEL 1 IF EXIST "%%A\*.*" ECHO.%%A is empty and will be deleted & RD /S /Q "%%A"
   )
DEL /q %TempFile%
Note that this code deletes the upper level folder if all subdirectories are empty so the [IF EXIST "%%A\*.*" ] is necessary to avoid an error message on the empty subfolders... which are now gone.

Windows Shell Scripting and InstallShield

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

#3 26 Dec 2010 10:46
bluesxman


This is how I'd do it:

Code: Select all

for /f "usebackq tokens=*" %%D IN (`dir /b /s /a:d "%_path1%" ^| sort /r`) DO (rmdir "%%~D" 2>nul && echo:Removed: "%%~D")
Basically it works down the directory tree attempting to remove all directories, starting with the deepest nesting. Any directories that are't empty will fail silently with no ill effect.

Last edited by bluesxman (28 Dec 2010 15:21)

cmd | *sh | ruby | chef

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

#4 01 Jan 2011 21:05
Jason404


Thanks to you both, but the use of the SORT command works very nicely there and is easier for me to understand.

Just so that I understand everything, what is the tokens option for FOR for?

Last edited by Jason404 (01 Jan 2011 21:30)

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

#5 01 Jan 2011 23:17
RG


Jason,
Tokens specifies which tokens are used. Delims is used to specify character(s) that break up the line into tokens. The default delimiters are space and tab but you can specify other characters with delims. So the line below
abc def gh ij
has 4 tokens (assuming you used the default delimiter set).
If you specify "tokens=*" as in the line below %%a inteprets the entire line as one token so %%a=abc def gh ij
for /f "tokens=*" %%a...
If you do something like this
for /f "tokens=2-3" %%a...
%%a=def
%%b=gh
You can get further details by doing
for /?
from a command prompt

Last edited by RG (01 Jan 2011 23:18)

Windows Shell Scripting and InstallShield

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

#6 04 Jan 2011 01:48
Jason404


Thanks for making the effort to explain this, but I don't understand it. It has taken me literally decades to get this far with FOR. At least now I can use it to some degree. Everything else is easy.

I want to understand FOR completely, and I think FORFILES is going to be very useful to me too, so excuse me for asking, but what is a token?

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

#7 04 Jan 2011 02:12
RG


Jason,
In the following line in my previous post
abc def gh ij
If you assume that spaces are delimiters, then the above line is broken up into 4 tokens by the delimiters (spaces). The 4 tokens are:
1. abc
2. def
3. gh
4. ij
So if you have a FOR loop something like this
for /f "tokens=1-3" %%a...
%%a=abc
%%b=def
%%c=gh

Windows Shell Scripting and InstallShield

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

#8 04 Jan 2011 02:21
Jason404


Thanks. But if tokens=* means everything found by the bracketed function, what is the difference when leaving the tokens option out completely?

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

#9 04 Jan 2011 02:58
RG


Play around with some examples. Take a text file that contains the line
abc def gh ij
Then put these lines in your bat file

for /f %%a in (textfile.txt) do echo.No token - %%a
for /f "tokens=*" %%a in (textfile.txt) do echo.tokens=* - %%a

Your results will be:
No token - abc
tokens=* - abc def gh ij

By not specifying tokens=* you only get the first token.

Windows Shell Scripting and InstallShield
Post Reply