-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterate.bat
223 lines (188 loc) · 4.46 KB
/
iterate.bat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
::
:: iterate diretory of files and execute an optional command as
:: "command file"
::
:: version: 1.0.2
:: last changed: 02.11.2023
::
@echo off
setlocal ENABLEDELAYEDEXPANSION
set prog_name=%~n0
set my_dir="%~dp0"
set "my_dir=%my_dir:~1,-2%"
set dir=.\
set cmd=
set args1=
set args2=
set /a r=0
set /a mode=0
set /a execute=0
set mask="*"
set /a verbose=0
set /a STEPPING_MODE=1
GOTO :ParseParams
:ParseParams
if [%1]==[/?] goto help
if /i [%1]==[/h] goto help
if /i [%1]==[/help] goto help
IF /i "%~1"=="/a1" (
SET args1=%~2
SHIFT
goto reParseParams
)
IF /i "%~1"=="/args1" (
SET args1=%~2
SHIFT
goto reParseParams
)
IF /i "%~1"=="/a2" (
SET args2=%~2
SHIFT
goto reParseParams
)
IF /i "%~1"=="/args2" (
SET args2=%~2
SHIFT
goto reParseParams
)
IF /i "%~1"=="/d" (
SET dir=%~2
SHIFT
goto reParseParams
)
IF /i "%~1"=="/dir" (
SET dir=%~2
SHIFT
goto reParseParams
)
IF /i "%~1"=="/c" (
SET cmd="%~2"
SHIFT
goto reParseParams
)
IF /i "%~1"=="/cmd" (
SET cmd="%~2"
SHIFT
goto reParseParams
)
IF /i "%~1"=="/m" (
SET mask="%~2"
SHIFT
goto reParseParams
)
IF /i "%~1"=="/mask" (
SET mask="%~2"
SHIFT
goto reParseParams
)
IF /i "%~1"=="/r" (
SET /a r=1
goto reParseParams
)
IF /i "%~1"=="/recursive" (
SET /a r=1
goto reParseParams
)
IF /i "%~1"=="/s" (
SET /a mode=STEPPING_MODE
goto reParseParams
)
IF /i "%~1"=="/step" (
SET /a mode=STEPPING_MODE
goto reParseParams
)
IF /i "%~1"=="/x" (
SET /a execute=1
goto reParseParams
)
IF /i "%~1"=="/execute" (
SET /a execute=1
goto reParseParams
)
IF /i "%~1"=="/v" (
SET /a verbose=1
goto reParseParams
)
:reParseParams
SHIFT
if [%1]==[] goto main
GOTO :ParseParams
:main
if %verbose% == 1 (
echo dir = %dir%
echo cmd = %cmd%
echo execute = %execute%
echo r = %r%
echo mode = %mode%
echo.
)
if not exist %dir% (
echo [e] dir "%dir%" does not exist!
exit /b -1
)
if %r% == 0 (
for %%p in (%dir%\%mask%) do (
if %mode% == %STEPPING_MODE% (
pause
)
echo ---------------------------
echo %%p
echo ---------------------------
if [%cmd%] NEQ [] (
%cmd:~1,-1% %args1% "%%p" %args2%
) else (
if %execute% EQU 1 (
"%%p"
))
echo.
)
) else (
for /r %dir% %%p in (%mask%) do (
if %mode% == %STEPPING_MODE% (
pause
)
echo ---------------------------
echo %%p
echo ---------------------------
if [%cmd%] NEQ [] (
%cmd:~1,-1% %args1% "%%p" %args2%
) else (
if %execute% EQU 1 (
"%%p"
))
echo.
)
)
endlocal
exit /B 0
:: :printSep
:: setlocal
:: set str=%1
:: set str=%str:~1,-1%
:: echo str : %str%
:: set /a pos=0
:: :NextChar
:: echo|set /p="-"
:: if "!str:~%pos%,1!" NEQ "" goto NextChar
:: set /a pos=pos+1
:: echo.
:: endlocal
:usage
echo Usage: %prog_name% [/d ^<path^>] [/c ^<command^>] [/a1 ^<args1^>] [/a2 ^<args2^>] [/m ^<mask^>] [/r] [/s] [/v]
exit /B 0
:help
call :usage
echo.
echo Options:
echo /d: The directory to iterate. Default ".\"
echo /c: An optional command to execute. Will be executed as "command args1 %%file%% args2".
echo /a1: Optional arguments to command. Will be executed as "command args1 %%file%% args2".
echo /a2: Optional arguments to command. Will be executed as "command args1 %%file%% args2".
echo /r: Iterate directories recursively.
echo /s: Stepping mode. Requires confirmation before processing a file.
echo /m: Search mask. I.e. "/m *.exe" to only list exe files.
echo.
echo Other:
echo /h: Print this.
echo /v: verbose mode.
exit /B 0