|
Answer» I have a sub-directory that has nothing but sub-folders in it, no files.
How do I use a FOR loop to list the sub-folders?
For example:
For %f in () do echo Subfolder %f being removed. <== Display it. rd /s/q %f <=== Remove it.
for I tried *, *.* . and nothing works. I need a "wildcard" that will generate the sub-folder names.dir /b /ad will list the folders under the current directory; if you want to list subfolders as well then dir has the /s switch
FOR can process the output of a command such as dir if you
1. Use the /F switch; 2. To get the whole string (folder names may have SPACES or other token delimiters) use a "delims=" block (set delimiters to be start and end of string) 3. In the dataset parentheses enclose the command and any switches in single quotes (white space before and after are ignored so I often use it to make it clearer when explaining)
FOR /F %%A in ('command') do (SOMETHING)
You did KNOW that you use two % SIGNS before the letter for FOR metavariables in a batch script? (one at the prompt, two in a script)
Example: this will list the folders in the current folder, sorted in date order
Code: [Select]FOR /F "delims=" %%A in ( 'dir /b /ad /od' ) do ( echo %%A )
This will delete the folders in the current folder
I usually enclose the FOR variable (here "%%F") in quotes in case any of the names has spaces.
Code: [Select]echo off
md dir1 md dir2 md dir3 md dir4 md dir1\sub1 md dir2\sub2 md dir3\sub3 md dir4\sub5 md dir1\sub1\folderA md dir2\sub2\folderB md dir3\sub3\folderC md dir4\sub5\folderD
for /f "delims=" %%F in ( ' dir /b /ad ' ) do ( echo Removing %%F rd /s /q "%%F" )
|