Skip to content

Commit c78b0a9

Browse files
committed
add /D option to FOR command to look for directories instead of files matching the "set" file specifiers
e.g. to list all directories: FOR /D %a in (*.*) DO ECHO %a
1 parent b0c2187 commit c78b0a9

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

cmd/for.c

+28-3
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,33 @@ static forErrors checkFOR(char * param /* in: command line */
4646
assert(flags);
4747

4848
*flags = 0;
49-
/* Check that first element is % then an alpha char followed by space */
5049

50+
/* skip past and process any option flags
51+
/D filespec is for directories instead of files
52+
/F ??? - not currently supported, parse file instead of perform findfirst/next
53+
/R [basepath] - not currently supported, repeat for each subdir of basepath
54+
*/
55+
if (*param == '/' || *param == '-') {
56+
param++;
57+
switch(toupper(*param++)) {
58+
case 'D':
59+
*flags |= FLAG_OPT_DIRECTORY;
60+
break;
61+
#if 0
62+
case 'F':
63+
*flags |= FLAG_OPT_FILEPARSE;
64+
break;
65+
case 'R':
66+
*flags |= FLAG_OPT_RECURSE;
67+
break;
68+
#endif
69+
default:
70+
return badVar;
71+
}
72+
param = ltrimcl(param + 1); /* skip whitespaces */
73+
}
74+
75+
/* Check that it starts with a % then an alpha char followed by space */
5176
*varS = param;
5277
if(*param == '%') { /* Check for FOR %%%%v IN syntax */
5378
while(*++param == '%');
@@ -59,7 +84,7 @@ static forErrors checkFOR(char * param /* in: command line */
5984
}
6085
}
6186

62-
#if 1
87+
#if 0
6388
/* the standard release does not support normal variables as
6489
FOR variables */
6590
return badVar;
@@ -131,7 +156,6 @@ static int doFOR (char * varname, char * varE, char * param, char * paramE,
131156
int rv;
132157
#endif
133158

134-
(void)flags;
135159
assert(varname);
136160
assert(varE);
137161
assert(param);
@@ -169,6 +193,7 @@ static int doFOR (char * varname, char * varE, char * param, char * paramE,
169193
}
170194

171195
bc->shiftlevel = 1; /* skip %0 <=> filename */
196+
bc->forFlags = flags;
172197
}
173198
return 0;
174199

include/batch.h

+7
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ struct bcontext
3737
int bclose; /* close batch file on exit */
3838
int brewind; /* rewind batch file next time */
3939
int numParams; /* number of parameters */
40+
int forFlags; /* optional flags to FOR command */
4041
};
4142

4243
/* The stack of current batch contexts.
@@ -56,3 +57,9 @@ char *find_arg_bc(struct bcontext const * const b, int n);
5657

5758
#define find_arg(num) find_arg_bc(activeBatchContext(), (num))
5859
#define getArgCur(num) find_arg_bc(bc, (num))
60+
61+
/* flags used by FOR */
62+
/* #define FLAG_HACKERY_FOR 1 */
63+
#define FLAG_OPT_DIRECTORY 2
64+
#define FLAG_OPT_FILEPARSE 4
65+
#define FLAG_OPT_RECURSE 8

shell/batch.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -443,9 +443,9 @@ char *readbatchline(int *eflag, char *textline, int size)
443443

444444
if(
445445
#ifdef FEATURE_LONG_FILENAMES
446-
lfnfor ? lfnfindfirst( fv, bc->ffind, FA_NORMAL ) == 0 :
446+
lfnfor ? lfnfindfirst( fv, bc->ffind, (bc->forFlags&FLAG_OPT_DIRECTORY? FA_DIREC:FA_NORMAL) ) == 0 :
447447
#endif
448-
sfnfindfirst( fv, ( struct ffblk * )bc->ffind, FA_NORMAL )
448+
sfnfindfirst( fv, ( struct ffblk * )bc->ffind, (bc->forFlags&FLAG_OPT_DIRECTORY? FA_DIREC:FA_NORMAL) )
449449
== 0 ) {
450450
/* found a file */
451451
*dfnfilename(fv) = '\0'; /* extract path */

0 commit comments

Comments
 (0)