cmd: Make 'if errorlevel' only recognize integer values.
[wine/multimedia.git] / programs / cmd / tests / test_builtins.cmd
bloba56d0a3314d698688c11918027b9b5d2f5b074f0
1 echo Tests for cmd's builtin commands
3 @echo on
4 echo ------------ Testing 'echo' [ON] --------------
5 echo word
6 echo 'singlequotedword'
7 echo "doublequotedword"
8 @echo at-echoed-word
9 echo "/?"
10 echo.
11 echo .
12 echo.word
13 echo .word
14 echo:
15 echo :
16 echo:word
17 echo :word
18 echo off now
19 echo word@space@
20 echo word@space@@space@
21 echo word
22 echo@tab@word
23 echo@tab@word @tab@
24 echo@tab@word@tab@@space@
25 @tab@echo word
26 echo @tab@word
27 echo @tab@word
28 echo@tab@@tab@word
30 @echo off
31 echo ------------ Testing 'echo' [OFF] --------------
32 echo word
33 echo 'singlequotedword'
34 echo "doublequotedword"
35 @echo at-echoed-word
36 echo "/?"
37 echo.
38 echo .
39 echo.word
40 echo .word
41 echo:
42 echo :
43 echo:word
44 echo :word
45 echo on again
46 echo word@space@
47 echo word@space@@space@
48 echo word
49 echo@tab@word
50 echo@tab@word @tab@
51 echo@tab@word@tab@@space@
52 @tab@echo word
53 echo @tab@word
54 echo @tab@word
55 echo@tab@@tab@word
57 echo ------------ Testing mixed echo modes --------------
58 echo @echo on> mixedEchoModes.cmd
59 echo if 1==1 echo foo>> mixedEchoModes.cmd
60 echo if 1==1 @echo bar>> mixedEchoModes.cmd
61 echo @echo off>> mixedEchoModes.cmd
62 echo if 1==1 echo foo2>> mixedEchoModes.cmd
63 echo if 1==1 @echo bar2>> mixedEchoModes.cmd
64 type mixedEchoModes.cmd
65 cmd /c mixedEchoModes.cmd
66 del mixedEchoModes.cmd
68 echo ------------ Testing rem --------------
69 rem Hello
70 rem Hello
71 rem Hello || foo
72 rem echo lol
73 rem echo foo & echo bar
74 rem @tab@ Hello
75 rem@tab@ Hello
76 rem@tab@echo foo & echo bar
77 @echo on
78 rem Hello
79 rem Hello
80 rem Hello || foo
81 rem echo lol
82 rem echo foo & echo bar
83 rem @tab@ Hello
84 rem@tab@ Hello
85 rem@tab@echo foo & echo bar
86 @echo off
88 echo ------------ Testing redirection operators --------------
89 mkdir foobar & cd foobar
90 echo ...stdout redirection
91 echo foo>foo
92 type foo
93 echo foo 1> foo
94 type foo
95 echo foo@tab@1> foo
96 type foo
97 echo foo 1>@tab@foo
98 type foo
99 echo foo@tab@1>@tab@foo
100 type foo
101 echo foo7 7> foo
102 type foo
103 echo foo9 9> foo
104 type foo
105 echo foo1> foo
106 type foo
107 echo foo11> foo
108 type foo
109 echo foo12> foo
110 type foo
111 del foo
112 echo1>foo
113 type foo
114 echo ...stdout appending
115 echo foo>foo
116 echo foo >>foo
117 type foo
118 del foo
119 echo foob >> foo
120 type foo
121 echo fooc 1>>foo
122 type foo
123 echo food1>>foo
124 type foo
125 echo food2>>foo
126 type foo
127 del foo
128 echo food21>>foo
129 type foo
130 del foo
131 echo foo> foo
132 echo foo7 7>> foo || (echo not supported & del foo)
133 if exist foo (type foo) else echo not supported
134 echo ...redirections within IF statements
135 if 1==1 echo foo1>bar
136 type bar & del bar
137 if 1==1 (echo foo2>bar) else echo baz2>bar
138 type bar & del bar
139 if 1==1 (echo foo3) else echo baz3>bar
140 type bar || echo file does not exist, ok
141 if 1==1 (echo foo4>bar) else echo baz4>bar
142 type bar & del bar
143 if 1==0 (echo foo5>bar) else echo baz5>bar
144 type bar & del bar
145 if 1==0 (echo foo6) else echo baz6 1>bar
146 type bar & del bar
147 if 1==0 (echo foo7 1>bar) else echo baz7>bar
148 type bar & del bar
149 if 1==0 (echo foo8 1>bar) else echo baz8>bak
150 type bak
151 if 1==1 (echo foo>bar & echo baz)
152 type bar
153 if 1==1 (
154 echo foo>bar
155 echo baz
157 type bar
158 (if 1==1 (echo A) else echo B) > C
159 type C
160 (if 1==0 (echo A) else echo B) > C
161 type C
162 (if 1==0 (echo A > B) else echo C)
163 cd .. & rd /s/q foobar
165 echo ------------ Testing ^^ escape character --------------
166 rem Using something like "echo foo^" asks for an additional char after a "More?" prompt on the following line; it's not possible to currently test that non-interactively
167 echo ^hell^o, world
168 echo hell^o, world
169 echo hell^^o, world
170 echo hell^^^o, world
171 mkdir foobar
172 echo baz> foobar\baz
173 type foobar\baz
174 type foobar^\baz
175 rd /s/q foobar
176 echo foo ^| echo bar
177 echo foo ^& echo bar
178 call :setError 0
179 echo bak ^&& echo baz 2> nul
180 echo %ErrorLevel%
181 echo foo ^> foo
182 echo ^<> foo
183 type foo
184 del foo
185 set FOO=oof
186 echo ff^%FOO%
187 set FOO=bar ^| baz
188 set FOO
189 rem FIXME: echoing %FOO% gives an error (baz not recognized) but prematurely
190 rem exits the script on windows; redirecting stdout and/or stderr doesn't help
191 echo %ErrorLevel%
192 call :setError 0
193 set FOO=bar ^^^| baz
194 set FOO
195 echo %FOO%
196 echo %ErrorLevel%
197 set FOO=
199 echo ------------ Testing 'set' --------------
200 call :setError 0
201 set FOOBAR 2> nul > nul
202 echo %ErrorLevel%
203 set FOOBAR = baz
204 echo %ErrorLevel%
205 echo %FOOBAR%FOOBAR not defined
206 echo %FOOBAR %
207 set FOOBAR 2> nul
208 set FOOBAR = baz2
209 echo %ErrorLevel%
210 echo %fOObAr %
211 set FOOBAR= bar
212 echo %ErrorLevel%
213 echo %FOOBAR%
214 set FOO
215 set FOOBAR=
216 set FOOB
217 echo %FOOBAR%FOOBAR not defined
218 set FOOBAR =
219 set FOOBA 2> nul > nul
220 echo %ErrorLevel%
221 set FOO=bar
222 echo %FOO%
223 set FOO=foo
224 set BAR=bar
225 echo %FOO%%BAR%
226 set BAR=
227 set FOO=
228 set FOO=%FOO%
229 echo %FOO%FOO not defined
230 set BAZ%=bazbaz
231 set BA
232 echo %BAZ%%
233 set BAZ%=
234 echo set "FOO=bar" should not include the quotes in the variable value
235 set "FOO=bar"
236 echo %FOO%
237 set@tab@FOO=foo
238 echo %FOO%
239 set@tab@FOO=
240 echo '%FOO%'
241 set FOO=foo@space@
242 echo '%FOO%'
243 set FOO=foo@tab@
244 echo '%FOO%'
245 set FOO=
247 echo ------------ Testing variable expansion --------------
248 call :setError 0
249 echo ~dp0 should be directory containing batch file
250 echo %~dp0
251 mkdir dummydir
252 cd dummydir
253 echo %~dp0
254 cd ..
255 rmdir dummydir
256 echo CD value %CD%
257 echo %%
258 echo P%
259 echo %P
260 echo %UNKNOWN%S
261 echo P%UNKNOWN%
262 echo P%UNKNOWN%S
263 echo %ERRORLEVEL
264 echo %ERRORLEVEL%
265 echo %ERRORLEVEL%%ERRORLEVEL%
266 echo %ERRORLEVEL%ERRORLEVEL%
267 echo %ERRORLEVEL%%
268 echo %ERRORLEVEL%%%
269 echo P%ERRORLEVEL%
270 echo %ERRORLEVEL%S
271 echo P%ERRORLEVEL%S
273 echo ------------ Testing variable substrings --------------
274 set VAR=qwerty
275 echo %VAR:~0,1%
276 echo %VAR:~0,3%
277 echo %VAR:~2,2%
278 echo '%VAR:~-2,3%'
279 echo '%VAR:~-2,1%'
280 echo %VAR:~2,-1%
281 echo %VAR:~2,-3%
282 echo '%VAR:~-2,-4%'
283 echo %VAR:~-3,-2%
284 set VAR=
286 echo ------------ Testing variable substitution --------------
287 echo ...in FOR variables
288 for %%i in ("A B" C) do echo %%i
289 rem quotes removal
290 for %%i in ("A B" C) do echo '%%~i'
291 rem fully qualified path
292 for %%f in ("C D" E) do echo %%~ff
293 rem drive letter
294 for %%i in ("F G" H) do echo %%~di
295 rem path
296 for %%d in ("I J" K) do echo %%~pd
297 rem filename
298 for %%i in ("L M" N) do echo %%~ni
299 rem file extension
300 for %%i in ("O. P.OOL" Q.TABC hello) do echo '%%~xi'
301 rem path with short path names
302 for %%I in ("R S" T ABCDEFGHIJK.LMNOP) do echo '%%~sI'
303 rem file attribute
304 for %%i in ("U V" W) do echo '%%~ai'
305 echo foo> foo
306 for %%i in (foo) do echo '%%~ai'
307 del foo
308 rem file date/time
309 rem Not fully testable, until we can grep dir's output to get foo's creation time in an envvar...
310 for %%i in ("a b" c) do echo '%%~ti'
311 rem file size
312 rem Similar issues as above
313 for %%i in ("a b" c) do echo '%%~zi'
314 rem combined options
315 for %%i in ("d e" f) do echo %%~dpi
316 for %%i in ("g h" i) do echo %%~sdi
317 for %%i in ("g h" i) do echo %%~dsi
318 for %%i in ("j k" l.eh) do echo '%%~xsi'
320 echo ...in parameters
321 for %%i in ("A B" C) do call :echoFun %%i
322 rem quotes removal
323 for %%i in ("A B" C) do call :echoFunQ %%i
324 rem fully qualified path
325 for %%f in ("C D" E) do call :echoFunF %%f
326 rem drive letter
327 for %%i in ("F G" H) do call :echoFunD %%i
328 rem path
329 for %%d in ("I J" K) do call :echoFunP %%d
330 rem filename
331 for %%i in ("L M" N) do call :echoFunN %%i
332 rem file extension
333 for %%i in ("O. P.OOL" Q.TABC hello) do call :echoFunX %%i
334 rem path with short path names
335 for %%I in ("R S" T ABCDEFGHIJK.LMNOP) do call :echoFunS %%I
336 rem NT4 aborts whole script execution when encountering ~a, ~t and ~z substitutions, preventing full testing
337 rem combined options
338 for %%i in ("d e" f) do call :echoFunDP %%i
339 for %%i in ("g h" i) do call :echoFunSD %%i
340 for %%i in ("g h" i) do call :echoFunDS %%i
341 for %%i in ("j k" l.eh) do call :echoFunXS %%i
343 goto :endEchoFuns
344 :echoFun
345 echo %1
346 goto :eof
348 :echoFunQ
349 echo '%~1'
350 goto :eof
352 :echoFunF
353 echo %~f1
354 goto :eof
356 :echoFunD
357 echo %~d1
358 goto :eof
360 :echoFunP
361 echo %~p1
362 goto :eof
364 :echoFunN
365 echo %~n1
366 goto :eof
368 :echoFunX
369 echo '%~x1'
370 goto :eof
372 :echoFunS
373 rem some NT4 workaround
374 set VAR='%~s1'
375 echo %VAR%
376 set VAR=
377 goto :eof
379 :echoFunDP
380 echo %~dp1
381 goto :eof
383 :echoFunSD
384 echo %~sd1
385 goto :eof
387 :echoFunDS
388 echo %~ds1
389 goto :eof
391 :echoFunXS
392 echo '%~xs1'
393 goto :eof
394 :endEchoFuns
396 echo ------------ Testing variable delayed expansion --------------
397 rem NT4 doesn't support this
398 echo ...default mode (load-time expansion)
399 set FOO=foo
400 echo %FOO%
401 echo !FOO!
402 if %FOO% == foo (
403 set FOO=bar
404 if %FOO% == bar (echo bar) else echo foo
407 set FOO=foo
408 if %FOO% == foo (
409 set FOO=bar
410 if !FOO! == bar (echo bar) else echo foo
413 echo ...runtime (delayed) expansion mode
414 setlocal EnableDelayedExpansion
415 set FOO=foo
416 echo %FOO%
417 echo !FOO!
418 if %FOO% == foo (
419 set FOO=bar
420 if %FOO% == bar (echo bar) else echo foo
423 set FOO=foo
424 if %FOO% == foo (
425 set FOO=bar
426 if !FOO! == bar (echo bar) else echo foo
428 echo %ErrorLevel%
429 setlocal DisableDelayedExpansion
430 echo %ErrorLevel%
431 set FOO=foo
432 echo %FOO%
433 echo !FOO!
434 set FOO=
435 echo ...using /V cmd flag
436 echo @echo off> tmp.cmd
437 echo set FOO=foo>> tmp.cmd
438 echo echo %%FOO%%>> tmp.cmd
439 echo echo !FOO!>> tmp.cmd
440 echo set FOO=>> tmp.cmd
441 cmd /V:ON /C tmp.cmd
442 cmd /V:OfF /C tmp.cmd
443 del tmp.cmd
445 echo ------------ Testing conditional execution --------------
446 echo ...unconditional ^&
447 call :setError 123 & echo foo1
448 echo bar2 & echo foo2
449 mkdir foobar & cd foobar
450 echo > foobazbar
451 cd .. & rd /s/q foobar
452 if exist foobazbar (
453 echo foobar not deleted!
454 cd ..
455 rd /s/q foobar
456 ) else echo foobar deleted
457 echo ...on success conditional ^&^&
458 call :setError 456 && echo foo3 > foo3
459 if exist foo3 (
460 echo foo3 created
461 del foo3
462 ) else echo foo3 not created
463 echo bar4 && echo foo4
464 echo ...on failure conditional ^|^|
465 call :setError 789 || echo foo5
466 echo foo6 || echo bar6 > bar6
467 if exist bar6 (
468 echo bar6 created
469 del bar6
472 echo ------------ Testing cd ------------
473 mkdir foobar
474 cd foobar
475 echo blabla > singleFile
476 dir /b
477 echo Current dir: %CD%
479 cd ..
481 cd foobar@space@
483 cd ..
485 cd @space@foobar
487 cd..
489 cd foobar
490 cd..@space@
492 if not exist foobar (cd ..)
493 cd foobar
494 cd@tab@..@tab@@space@@tab@
496 if not exist foobar (cd ..)
497 cd foobar
498 mkdir "bar bak"
499 cd "bar bak"
501 cd ..
502 cd bar bak
504 cd "bar bak@space@"@tab@@space@
506 cd ..\..
508 rd /Q/s foobar
509 mkdir foobar
510 cd /d@tab@foobar
512 cd ..
513 rd /q/s foobar
515 echo ------------ Testing type ------------
516 echo bar> foobaz
517 @echo on
518 type foobaz
519 echo ***
520 @echo off
521 type foobaz@tab@
522 echo ***
523 del foobaz
525 echo ------------ Testing NUL ------------
526 md foobar & cd foobar
527 rem NUL file (non) creation + case insensitivity
528 rem Note: "if exist" does not work with NUL, so to check for file existence we use a kludgy workaround
529 echo > bar
530 echo foo > NUL
531 dir /b /a-d
532 echo foo > nul
533 dir /b /a-d
534 echo foo > NuL
535 @tab@dir /b@tab@/a-d
536 del bar
537 rem NUL not special everywhere
538 call :setError 123
539 echo NUL> foo
540 if not exist foo (echo foo should have been created) else (
541 type foo
542 del foo
544 rem Empty file creation
545 copy nul foo > nul
546 if exist foo (
547 echo foo created
548 del foo
549 type foo
550 ) else (
551 echo ***
553 cd .. & rd foobar
555 echo ------------ Testing if/else --------------
556 echo if/else should work with blocks
557 if 0 == 0 (
558 echo if seems to work
559 ) else (
560 echo if seems to be broken
562 if 1 == 0 (
563 echo else seems to be broken
564 ) else (
565 echo else seems to work
567 if /c==/c (
568 echo if seems not to detect /c as parameter
569 ) else (
570 echo parameter detection seems to be broken
572 echo Testing case sensitivity with and without /i option
573 if bar==BAR echo if does not default to case sensitivity
574 if not bar==BAR echo if seems to default to case sensitivity
575 if /i foo==FOO echo if /i seems to work
576 if /i not foo==FOO echo if /i seems to be broken
577 if /I foo==FOO echo if /I seems to work
578 if /I not foo==FOO echo if /I seems to be broken
579 echo Testing string comparisons
580 if abc == abc (echo equal) else echo non equal
581 if abc =="abc" (echo equal) else echo non equal
582 if "abc"== abc (echo equal) else echo non equal
583 if "abc"== "abc" (echo equal) else echo non equal
584 echo Testing tabs handling
585 if@tab@1==1 echo doom
586 if @tab@1==1 echo doom
587 if 1==1 (echo doom) else@tab@echo quake
588 if@tab@not @tab@1==@tab@0 @tab@echo lol
589 if 1==0@tab@(echo doom) else echo quake
590 if 1==0 (echo doom)@tab@else echo quake
591 if 1==0 (echo doom) else@tab@echo quake
593 echo -----------Testing for -----------
594 echo ...plain FOR
595 for %%i in (A B C) do echo %%i
596 for %%i in (A B C) do echo %%I
597 for %%i in (A B C) do echo %%j
598 for %%i in (A B C) do call :forTestFun1 %%i
599 for %%i in (1,4,1) do echo %%i
600 for %%i in (A, B,C) do echo %%i
601 for %%i in (X) do echo %%i
602 for@tab@%%i in (X2) do echo %%i
603 for %%i in@tab@(X3) do echo %%i
604 for %%i in (@tab@ foo@tab@) do echo %%i
605 for@tab@ %%i in@tab@(@tab@M) do echo %%i
606 for %%i@tab@in (X)@tab@do@tab@echo %%i
607 for@tab@ %%j in@tab@(@tab@M, N, O@tab@) do echo %%j
608 for %%i in (`echo A B`) do echo %%i
609 for %%i in ('echo A B') do echo %%i
610 for %%i in ("echo A B") do echo %%i
611 for %%i in ("A B" C) do echo %%i
612 goto :endForTestFun1
613 :forTestFun1
614 echo %1
615 goto :eof
616 :endForTestFun1
617 echo ...imbricated FORs
618 for %%i in (X) do (
619 for %%j in (Y) do (
620 echo %%i %%j))
621 for %%i in (X) do (
622 for %%I in (Y) do (
623 echo %%i %%I))
624 for %%i in (A B) do (
625 for %%j in (C D) do (
626 echo %%i %%j))
627 for %%i in (A B) do (
628 for %%j in (C D) do (
629 call :forTestFun2 %%i %%j ))
630 goto :endForTestFun2
631 :forTestFun2
632 echo %1 %2
633 goto :eof
634 :endForTestFun2
635 mkdir foobar & cd foobar
636 mkdir foo
637 mkdir bar
638 mkdir baz
639 echo > bazbaz
640 echo ...basic wildcards
641 for %%i in (ba*) do echo %%i
642 echo ...for /d
643 for /d %%i in (baz foo bar) do echo %%i
644 rem FIXME for /d incorrectly parses when wildcards are used
645 rem for /d %%i in (bazb*) do echo %%i
646 rem FIXME can't test wildcard expansion here since it's listed in directory
647 rem order, and not in alphabetic order.
648 rem Proper testing would need a currently missing "sort" program implementation.
649 rem for /d %%i in (ba*) do echo %%i>> tmp
650 rem sort < tmp
651 rem del tmp
652 rem for /d %%i in (?a*) do echo %%i>> tmp
653 rem sort < tmp
654 rem del tmp
655 rem for /d %%i in (*) do echo %%i>> tmp
656 rem sort < tmp
657 rem del tmp
658 cd .. & rd /s/Q foobar
659 echo ...for /L
660 rem Some cases loop forever writing 0s, like e.g. (1,0,1), (1,a,3) or (a,b,c); those can't be tested here
661 for /L %%i in (1,2,0) do echo %%i
662 for@tab@/L %%i in (1,2,0) do echo %%i
663 for /L %%i in (1,2,6) do echo %%i
664 for /l %%i in (1 ,2,6) do echo %%i
665 for /L %%i in (a,2,3) do echo %%i
666 for /L %%i in (1,2,-1) do echo %%i
667 for /L %%i in (-4,-1,-1) do echo %%i
668 for /L %%i in (1,-2,-2) do echo %%i
669 for /L %%i in (1,2,a) do echo %%i
670 echo ErrorLevel %ErrorLevel%
671 for /L %%i in (1,a,b) do echo %%i
672 echo ErrorLevel %ErrorLevel%
673 rem FIXME: following test cases cannot be currently tested due to an inconsistent/buggy 'for /L' parsing.
674 rem for /L %%i in (a,2,b) do echo %%i
675 rem for /L %%i in (1,1,1) do echo %%i
676 rem for /L %%i in (1,-2,-1) do echo %%i
677 rem for /L %%i in (-1,-1,-1) do echo %%i
678 rem for /L %%i in (1,2, 3) do echo %%i
679 echo ...for /a
680 rem No output when using "set expr" syntax, unless in interactive mode
681 rem Need to use "set envvar=expr" to use in a batch script
682 echo ......individual operations
683 set var=0
684 set /a var=1 +2 & echo %var%
685 set /a var=1 +-2 & echo %var%
686 set /a var=1 --2 & echo %var%
687 set /a var=2* 3 & echo %var%
688 set /a var=-2* -5 & echo %var%
689 set /a var=12/3 & echo %var%
690 set /a var=13/3 & echo %var%
691 set /a var=-13/3 & echo %var%
692 rem FIXME Divide by zero should return an error, but error messages cannot be tested with current infrastructure
693 set /a var=5 %% 5 & echo %var%
694 set /a var=5 %% 3 & echo %var%
695 set /a var=5 %% -3 & echo %var%
696 set /a var=-5 %% -3 & echo %var%
697 set /a var=1 ^<^< 0 & echo %var%
698 set /a var=1 ^<^< 2 & echo %var%
699 set /a var=1 ^<^< -2 & echo %var%
700 set /a var=-1 ^<^< -2 & echo %var%
701 set /a var=-1 ^<^< 2 & echo %var%
702 set /a var=9 ^>^> 0 & echo %var%
703 set /a var=9 ^>^> 2 & echo %var%
704 set /a var=9 ^>^> -2 & echo %var%
705 set /a var=-9 ^>^> -2 & echo %var%
706 set /a var=-9 ^>^> 2 & echo %var%
707 set /a var=5 ^& 0 & echo %var%
708 set /a var=5 ^& 1 & echo %var%
709 set /a var=5 ^& 3 & echo %var%
710 set /a var=5 ^& 4 & echo %var%
711 set /a var=5 ^& 1 & echo %var%
712 set /a var=5 ^| 0 & echo %var%
713 set /a var=5 ^| 1 & echo %var%
714 set /a var=5 ^| 3 & echo %var%
715 set /a var=5 ^| 4 & echo %var%
716 set /a var=5 ^| 1 & echo %var%
717 set /a var=5 ^^ 0 & echo %var%
718 set /a var=5 ^^ 1 & echo %var%
719 set /a var=5 ^^ 3 & echo %var%
720 set /a var=5 ^^ 4 & echo %var%
721 set /a var=5 ^^ 1 & echo %var%
722 echo ......precedence and grouping
723 set /a var=4 + 2*3 & echo %var%
724 set /a var=(4+2)*3 & echo %var%
725 set /a var=4 * 3/5 & echo %var%
726 set /a var=(4 * 3)/5 & echo %var%
727 set /a var=4 * 5 %% 4 & echo %var%
728 set /a var=4 * (5 %% 4) & echo %var%
729 set /a var=3 %% (5 + 8 %% 3 ^^ 2) & echo %var%
730 set /a var=3 %% (5 + 8 %% 3 ^^ -2) & echo %var%
731 echo ......octal and hexadecimal
732 set /a var=0xf + 3 & echo %var%
733 set /a var=0xF + 3 & echo %var%
734 set /a var=015 + 2 & echo %var%
735 set /a var=3, 8+3,0 & echo %var%
736 echo ......variables
737 set /a var=foo=3, foo+1 & echo %var%
738 if defined foo (echo %foo%) else (
739 echo foo not defined
741 set /a var=foo=3, foo+=1 & echo %var%
742 set /a var=foo=3, bar=1, bar+=foo, bar & echo %var%
743 set /a var=foo*= foo & echo %var%
744 set /a var=whateverNonExistingVar & echo %var%
745 set /a var=whateverNonExistingVar + bar & echo %var%
746 set /a var=foo -= foo + 7 & echo %var%
747 set /a var=foo /= 3 + 2 & echo %var%
748 set /a var=foo=5, foo %%=2 & echo %var%
749 set /a var=foo ^<^<= 2 & echo %var%
750 set /a var=foo ^>^>= 2 & echo %var%
751 set /a var=foo ^&= 2 & echo %var%
752 set /a var=foo=5, foo ^|= 2 & echo %var%
753 set /a var=foo=5, foo ^^= 2 & echo %var%
754 set /a var=foo=19, foo %%= 4 + (bar %%= 7) & echo.
755 set foo=
756 set bar=
757 set var=
758 echo ...for /F
759 mkdir foobar & cd foobar
760 echo ......string argument
761 for /F %%i in ("a b c") do echo %%i
762 for /f %%i in ("a ") do echo %%i
763 for /f %%i in ("a") do echo %%i
764 fOr /f %%i in (" a") do echo %%i
765 for /f %%i in (" a ") do echo %%i
766 echo ......fileset argument
767 echo .........basic blank handling
768 echo a b c>foo
769 for /f %%i in (foo) do echo %%i
770 echo a >foo
771 for /f %%i in (foo) do echo %%i
772 echo a>foo
773 for /f %%i in (foo) do echo %%i
774 echo a>foo
775 for /f %%i in (foo) do echo %%i
776 echo a >foo
777 for /f %%i in (foo) do echo %%i
778 echo. > foo
779 for /f %%i in (foo) do echo %%i
780 echo. >> foo
781 echo b > foo
782 for /f %%i in (foo) do echo %%i
783 echo .........multi-line with empty lines
784 echo a Z f> foo
785 echo. >> foo
786 echo.>> foo
787 echo b bC>> foo
788 echo c>> foo
789 echo. >> foo
790 for /f %%b in (foo) do echo %%b
791 echo .........multiple files
792 echo q w > bar
793 echo.>> bar
794 echo kkk>>bar
795 for /f %%k in (foo bar) do echo %%k
796 for /f %%k in (bar foo) do echo %%k
797 rem echo ......command argument
798 rem Not implemented on NT4
799 rem FIXME: Not testable right now in wine: not implemented and would need
800 rem preliminary grep-like program implementation (e.g. like findstr or fc) even
801 rem for a simple todo_wine test
802 rem (for /f "usebackq" %%i in (`echo z a b`) do echo %%i) || echo not supported
803 rem (for /f usebackq %%i in (`echo z a b`) do echo %%i) || echo not supported
804 echo ......eol option
805 for /f "eol=@" %%i in (" ad") do echo %%i
806 for /f "eol=@" %%i in (" z@y") do echo %%i
807 for /f "eol=|" %%i in ("a|d") do echo %%i
808 for /f "eol=@" %%i in ("@y") do echo %%i > output_file
809 if not exist output_file (echo no output) else (del output_file)
810 for /f "eol==" %%i in ("=y") do echo %%i > output_file
811 if not exist output_file (echo no output) else (del output_file)
812 echo ......delims option
813 for /f "delims=|" %%i in ("a|d") do echo %%i
814 for /f "delims=|" %%i in ("a |d") do echo %%i
815 for /f "delims=|" %%i in ("a d|") do echo %%i
816 for /f "delims=| " %%i in ("a d|") do echo %%i
817 for /f "delims==" %%i in ("C r=d|") do echo %%i
818 for /f "delims=" %%i in ("foo bar baz") do echo %%i
819 for /f "delims=" %%i in ("c:\foo bar baz\..") do echo %%~fi
820 echo ......skip option
821 echo a > foo
822 echo b >> foo
823 echo c >> foo
824 for /f "skip=2" %%i in (foo) do echo %%i
825 for /f "skip=3" %%i in (foo) do echo %%i > output_file
826 if not exist output_file (echo no output) else (del output_file)
827 for /f "skip=4" %%i in (foo) do echo %%i > output_file
828 if not exist output_file (echo no output) else (del output_file)
829 cd ..
830 rd /s/q foobar
832 echo -----------Testing del /a-----------
833 del /f/q *.test > nul
834 echo r > r.test
835 attrib +r r.test
836 echo not-r > not-r.test
838 if not exist not-r.test echo not-r.test not found before delete, bad
839 del /a:-r *.test
840 if not exist not-r.test echo not-r.test not found after delete, good
842 if not exist r.test echo r.test not found before delete, bad
843 if exist r.test echo r.test found before delete, good
844 del /a:r *.test
845 if not exist r.test echo r.test not found after delete, good
846 if exist r.test echo r.test found after delete, bad
848 echo ------------ Testing del /q --------------
849 mkdir del_q_dir
850 cd del_q_dir
851 echo abc > file1
852 echo abc > file2.dat
853 rem If /q doesn't work, cmd will prompt and the test case should hang
854 del /q * > nul
855 for %%a in (1 2.dat) do if exist file%%a echo del /q * failed on file%%a
856 for %%a in (1 2.dat) do if not exist file%%a echo del /q * succeeded on file%%a
857 cd ..
858 rmdir del_q_dir
860 echo ------------ Testing del /s --------------
861 mkdir "foo bar"
862 cd "foo bar"
863 mkdir "foo:"
864 echo hi > file1.dat
865 echo there > file2.dat
866 echo bub > file3.dat
867 echo bye > "file with spaces.dat"
868 cd ..
869 del /s file1.dat > nul
870 del file2.dat /s > nul
871 del "file3.dat" /s > nul
872 del "file with spaces.dat" /s > nul
873 cd "foo bar"
874 for %%f in (1 2 3) do if exist file%%f.dat echo Del /s failed on file%%f
875 for %%f in (1 2 3) do if exist file%%f.dat del file%%f.dat
876 if exist "file with spaces.dat" echo Del /s failed on "file with spaces.dat"
877 if exist "file with spaces.dat" del "file with spaces.dat"
878 rmdir "foo:"
879 cd ..
880 rmdir "foo bar"
882 echo ------------ Testing rename ------------
883 mkdir foobar & cd foobar
884 echo ... ren and rename are synonymous ...
885 echo > foo
886 rename foo bar
887 if exist foo echo foo should be renamed!
888 if exist bar echo foo renamed to bar
889 ren bar foo
890 if exist bar echo bar should be renamed!
891 if exist foo echo bar renamed to foo
892 echo ... name collision ...
893 echo foo>foo
894 echo bar>bar
895 ren foo bar 2> nul
896 type foo
897 type bar
898 rem no-op
899 ren foo foo
900 mkdir baz
901 ren foo baz\abc
902 echo ... rename read-only files ...
903 echo > file1
904 attrib +r file1
905 ren file1 file2
906 if not exist file1 (
907 if exist file2 (
908 echo read-only file renamed
910 ) else (
911 echo read-only file not renamed!
913 echo ... rename directories ...
914 mkdir rep1
915 ren rep1 rep2
916 if not exist rep1 (
917 if exist rep2 (
918 echo dir renamed
921 attrib +r rep2
922 ren rep2 rep1
923 if not exist rep2 (
924 if exist rep1 (
925 echo read-only dir renamed
928 echo ... rename in other directory ...
929 if not exist baz\abc (
930 echo rename impossible in other directory
931 if exist foo echo original file still present
932 ) else (
933 echo shouldn't rename in other directory!
934 if not exist foo echo original file not present anymore
936 cd .. & rd /s/q foobar
938 echo ------------ Testing move ------------
939 mkdir foobar & cd foobar
940 echo ... file move ...
941 echo >foo
942 move foo bar > nul 2>&1
943 if not exist foo (
944 if exist bar (
945 echo file move succeeded
948 echo bar>bar
949 echo baz> baz
950 move /Y bar baz > nul 2>&1
951 if not exist bar (
952 if exist baz (
953 echo file move with overwrite succeeded
955 ) else (
956 echo file overwrite impossible!
957 del bar
959 type baz
961 attrib +r baz
962 move baz bazro > nul 2>&1
963 if not exist baz (
964 if exist bazro (
965 echo read-only files are moveable
966 move bazro baz > nul 2>&1
968 ) else (
969 echo read-only file not moved!
971 attrib -r baz
972 mkdir rep
973 move baz rep > nul 2>&1
974 if not exist baz (
975 if exist rep\baz (
976 echo file moved in subdirectory
979 call :setError 0
980 move rep\baz . > nul 2>&1
981 move /Y baz baz > nul 2>&1
982 if errorlevel 1 (
983 echo moving a file to itself should be a no-op!
984 ) else (
985 echo moving a file to itself is a no-op
987 echo ErrorLevel: %ErrorLevel%
988 call :setError 0
989 del baz
990 echo ... directory move ...
991 mkdir foo\bar
992 mkdir baz
993 echo baz2>baz\baz2
994 move baz foo\bar > nul 2>&1
995 if not exist baz (
996 if exist foo\bar\baz\baz2 (
997 echo simple directory move succeeded
1000 call :setError 0
1001 mkdir baz
1002 move baz baz > nul 2>&1
1003 echo moving a directory to itself gives error; errlevel %ErrorLevel%
1004 echo ...... dir in dir move ......
1005 rd /s/q foo
1006 mkdir foo bar
1007 echo foo2>foo\foo2
1008 echo bar2>bar\bar2
1009 move foo bar > nul 2>&1
1010 if not exist foo (
1011 if exist bar (
1012 dir /b /ad bar
1013 dir /b /a-d bar
1014 dir /b bar\foo
1017 cd .. & rd /s/q foobar
1019 echo ------------ Testing mkdir ------------
1020 call :setError 0
1021 echo ... md and mkdir are synonymous ...
1022 mkdir foobar
1023 echo %ErrorLevel%
1024 rmdir foobar
1025 md foobar
1026 echo %ErrorLevel%
1027 rmdir foobar
1028 echo ... creating an already existing directory/file must fail ...
1029 mkdir foobar
1030 md foobar
1031 echo %ErrorLevel%
1032 rmdir foobar
1033 echo > foobar
1034 mkdir foobar
1035 echo %ErrorLevel%
1036 del foobar
1037 echo ... multilevel path creation ...
1038 mkdir foo
1039 echo %ErrorLevel%
1040 mkdir foo\bar\baz
1041 echo %ErrorLevel%
1042 cd foo
1043 echo %ErrorLevel%
1044 cd bar
1045 echo %ErrorLevel%
1046 cd baz
1047 echo %ErrorLevel%
1048 echo > ..\..\bar2
1049 mkdir ..\..\..\foo\bar2
1050 echo %ErrorLevel%
1051 del ..\..\bar2
1052 mkdir ..\..\..\foo\bar2
1053 echo %ErrorLevel%
1054 rmdir ..\..\..\foo\bar2
1055 cd ..
1056 rmdir baz
1057 cd ..
1058 rmdir bar
1059 cd ..
1060 rmdir foo
1061 echo %ErrorLevel%
1062 echo ... trailing backslashes ...
1063 mkdir foo\\\\
1064 echo %ErrorLevel%
1065 if exist foo (rmdir foo & echo dir created
1066 ) else ( echo dir not created )
1067 echo %ErrorLevel%
1068 echo ... invalid chars ...
1069 mkdir ?
1070 echo mkdir ? gives errorlevel %ErrorLevel%
1071 call :setError 0
1072 mkdir ?\foo
1073 echo mkdir ?\foo gives errorlevel %ErrorLevel%
1074 call :setError 0
1075 mkdir foo\?
1076 echo mkdir foo\? gives errorlevel %ErrorLevel%
1077 if exist foo (rmdir foo & echo ok, foo created
1078 ) else ( echo foo not created )
1079 call :setError 0
1080 mkdir foo\bar\?
1081 echo mkdir foo\bar\? gives errorlevel %ErrorLevel%
1082 call :setError 0
1083 if not exist foo (
1084 echo bad, foo not created
1085 ) else (
1086 cd foo
1087 if exist bar (
1088 echo ok, foo\bar created
1089 rmdir bar
1091 cd ..
1092 rmdir foo
1094 echo ... multiple directories at once ...
1095 mkdir foobaz & cd foobaz
1096 mkdir foo bar\baz foobar
1097 if exist foo (echo foo created) else echo foo not created!
1098 if exist bar (echo bar created) else echo bar not created!
1099 if exist foobar (echo foobar created) else echo foobar not created!
1100 if exist bar\baz (echo bar\baz created) else echo bar\baz not created!
1101 cd .. & rd /s/q foobaz
1102 call :setError 0
1103 mkdir foo\*
1104 echo mkdir foo\* errorlevel %ErrorLevel%
1105 if exist foo (rmdir foo & echo ok, foo created
1106 ) else ( echo bad, foo not created )
1108 echo ----------- Testing rmdir -----------
1109 call :setError 0
1110 rem rd and rmdir are synonymous
1111 mkdir foobar
1112 rmdir foobar
1113 echo %ErrorLevel%
1114 if not exist foobar echo dir removed
1115 mkdir foobar
1116 rd foobar
1117 echo %ErrorLevel%
1118 if not exist foobar echo dir removed
1119 rem Removing nonexistent directory
1120 rmdir foobar
1121 echo %ErrorLevel%
1122 rem Removing single-level directories
1123 echo > foo
1124 rmdir foo
1125 echo %ErrorLevel%
1126 if exist foo echo file not removed
1127 del foo
1128 mkdir foo
1129 echo > foo\bar
1130 rmdir foo
1131 echo %ErrorLevel%
1132 if exist foo echo non-empty dir not removed
1133 del foo\bar
1134 mkdir foo\bar
1135 rmdir foo
1136 echo %ErrorLevel%
1137 if exist foo echo non-empty dir not removed
1138 rmdir foo\bar
1139 rmdir foo
1140 rem Recursive rmdir
1141 mkdir foo\bar\baz
1142 rmdir /s /Q foo
1143 if not exist foo (
1144 echo recursive rmdir succeeded
1145 ) else (
1146 rd foo\bar\baz
1147 rd foo\bar
1148 rd foo
1150 mkdir foo\bar\baz
1151 echo foo > foo\bar\brol
1152 rmdir /s /Q foo
1153 if not exist foo (
1154 echo recursive rmdir succeeded
1155 ) else (
1156 rd foo\bar\baz
1157 del foo\bar\brol
1158 rd foo\bar
1159 rd foo
1161 rem multiples directories at once
1162 mkdir foobaz & cd foobaz
1163 mkdir foo
1164 mkdir bar\baz
1165 mkdir foobar
1166 rd /s/q foo bar foobar
1167 if not exist foo (echo foo removed) else echo foo not removed!
1168 if not exist bar (echo bar removed) else echo bar not removed!
1169 if not exist foobar (echo foobar removed) else echo foobar not removed!
1170 if not exist bar\baz (echo bar\baz removed) else echo bar\baz not removed!
1171 cd .. & rd /s/q foobaz
1173 echo ----------- Testing pushd/popd -----------
1175 echo ...popd is no-op when dir stack is empty
1176 popd
1178 echo ...pushing non-existing dir
1179 pushd foobar
1181 echo ...basic behaviour
1182 mkdir foobar\baz
1183 pushd foobar
1185 popd
1187 pushd foobar
1188 pushd baz
1190 popd
1192 pushd baz
1193 popd
1195 popd
1197 pushd .
1198 cd foobar\baz
1199 pushd ..
1201 popd
1202 popd
1204 rd /s/q foobar
1206 echo ------------ Testing attrib --------------
1207 rem FIXME Add tests for archive, hidden and system attributes + mixed attributes modifications
1208 mkdir foobar & cd foobar
1209 echo foo original contents> foo
1210 attrib foo
1211 echo > bar
1212 echo ... read-only attribute
1213 rem Read-only files cannot be altered or deleted, unless forced
1214 attrib +R foo
1215 attrib foo
1216 dir /Ar /B
1217 echo bar>> foo
1218 type foo
1219 del foo > NUL 2>&1
1220 if exist foo (
1221 echo Read-only file not deleted
1222 ) else (
1223 echo Should not delete read-only file!
1225 del /F foo
1226 if not exist foo (
1227 echo Read-only file forcibly deleted
1228 ) else (
1229 echo Should delete read-only file with del /F!
1230 attrib -r foo
1231 del foo
1233 cd .. & rd /s/q foobar
1234 echo ... recursive behaviour
1235 mkdir foobar\baz & cd foobar
1236 echo > level1
1237 echo > whatever
1238 echo > baz\level2
1239 attrib baz\level2
1240 cd ..
1241 attrib +R l*vel? /S > nul 2>&1
1242 cd foobar
1243 attrib level1
1244 attrib baz\level2
1245 echo > bar
1246 attrib bar
1247 cd .. & rd /s/q foobar
1248 echo ... folders processing
1249 mkdir foobar
1250 attrib foobar
1251 cd foobar
1252 mkdir baz
1253 echo toto> baz\toto
1254 attrib +r baz /s /d > nul 2>&1
1255 attrib baz
1256 attrib baz\toto
1257 echo lulu>>baz\toto
1258 type baz\toto
1259 echo > baz\lala
1260 rem Oddly windows allows file creation in a read-only directory...
1261 if exist baz\lala (echo file created in read-only dir) else echo file not created
1262 cd .. & rd /s/q foobar
1264 echo ------------ Testing assoc --------------
1265 rem FIXME Can't test error messages in the current test system, so we have to use some kludges
1266 rem FIXME Revise once || conditional execution is fixed
1267 mkdir foobar & cd foobar
1268 echo ...setting association
1269 assoc .foo > baz
1270 type baz
1271 echo ***
1273 assoc .foo=bar
1274 assoc .foo
1276 rem association set system-wide
1277 echo @echo off> tmp.cmd
1278 echo echo +++>> tmp.cmd
1279 echo assoc .foo>> tmp.cmd
1280 cmd /c tmp.cmd
1282 echo ...resetting association
1283 assoc .foo=
1284 assoc .foo > baz
1285 type baz
1286 echo ***
1288 rem association removal set system-wide
1289 cmd /c tmp.cmd > baz
1290 type baz
1291 echo ***
1292 cd .. & rd /s/q foobar
1294 echo ------------ Testing ftype --------------
1295 rem FIXME Can't test error messages in the current test system, so we have to use some kludges
1296 rem FIXME Revise once || conditional execution is fixed
1297 mkdir foobar & cd foobar
1298 echo ...setting association
1299 ftype footype> baz
1300 type baz
1301 echo ***
1303 ftype footype=foo_opencmd
1304 assoc .foo=footype
1305 ftype footype
1307 rem association set system-wide
1308 echo @echo off> tmp.cmd
1309 echo echo +++>> tmp.cmd
1310 echo ftype footype>> tmp.cmd
1311 cmd /c tmp.cmd
1313 echo ...resetting association
1314 assoc .foo=
1316 rem Removing a file type association doesn't work on XP due to a bug, so a workaround is needed
1317 setlocal EnableDelayedExpansion
1318 set FOO=original value
1319 ftype footype=
1320 ftype footype > baz
1321 for /F %%i in ('type baz') do (set FOO=buggyXP)
1322 rem Resetting actually works on wine/NT4, but is reported as failing due to the peculiar test (and non-support for EnabledDelayedExpansion)
1323 rem FIXME Revisit once a grep-like program like ftype is implemented
1324 rem (e.g. to check baz's size using dir /b instead)
1325 echo !FOO!
1327 rem cleanup registry
1328 echo REGEDIT4> regCleanup.reg
1329 echo.>> regCleanup.reg
1330 echo [-HKEY_CLASSES_ROOT\footype]>> regCleanup.reg
1331 regedit /s regCleanup.reg
1332 set FOO=
1333 endlocal
1334 cd .. & rd /s/q foobar
1336 echo ------------ Testing CALL ------------
1337 mkdir foobar & cd foobar
1338 echo ... external script ...
1339 echo echo foo %%1> foo.cmd
1340 call foo
1341 call foo.cmd 8
1342 echo echo %%1 %%2 > foo.cmd
1343 call foo.cmd foo
1344 call foo.cmd foo bar
1345 call foo.cmd foo ""
1346 call foo.cmd "" bar
1347 call foo.cmd foo ''
1348 call foo.cmd '' bar
1349 del foo.cmd
1351 echo ... internal routines ...
1352 call :testRoutine :testRoutine
1353 goto :endTestRoutine
1354 :testRoutine
1355 echo bar %1
1356 goto :eof
1357 :endTestRoutine
1359 call :testRoutineArgs foo
1360 call :testRoutineArgs foo bar
1361 call :testRoutineArgs foo ""
1362 call :testRoutineArgs "" bar
1363 call :testRoutineArgs foo ''
1364 call :testRoutineArgs '' bar
1365 goto :endTestRoutineArgs
1366 :testRoutineArgs
1367 echo %1 %2
1368 goto :eof
1369 :endTestRoutineArgs
1371 echo ... with builtins ...
1372 call mkdir foo
1373 echo %ErrorLevel%
1374 if exist foo (echo foo created) else echo foo should exist!
1375 rmdir foo
1376 set FOOBAZ_VAR=foobaz
1377 call echo Should expand %FOOBAZ_VAR%
1378 set FOOBAZ_VAR=
1379 echo>batfile
1380 call dir /b
1381 echo>robinfile
1382 if 1==1 call del batfile
1383 dir /b
1384 if exist batfile echo batfile shouldn't exist
1385 rem ... but not for 'if' or 'for'
1386 call if 1==1 echo bar 2> nul
1387 echo %ErrorLevel%
1388 call :setError 0
1389 call for %%i in (foo bar baz) do echo %%i 2> nul
1390 echo %ErrorLevel%
1391 rem First look for programs in the path before trying a builtin
1392 echo echo non-builtin dir> dir.cmd
1393 call dir /b
1394 cd .. & rd /s/q foobar
1396 echo ------------ Testing SHIFT ------------
1398 call :shiftFun p1 p2 p3 p4 p5
1399 goto :endShiftFun
1401 :shiftFun
1402 echo '%1' '%2' '%3' '%4' '%5'
1403 shift
1404 echo '%1' '%2' '%3' '%4' '%5'
1405 shift@tab@ /1
1406 echo '%1' '%2' '%3' '%4' '%5'
1407 shift /2
1408 echo '%1' '%2' '%3' '%4' '%5'
1409 shift /-1
1410 echo '%1' '%2' '%3' '%4' '%5'
1411 shift /0
1412 echo '%1' '%2' '%3' '%4' '%5'
1413 goto :eof
1414 :endShiftFun
1416 echo ------------ Testing cmd invocation ------------
1417 rem FIXME: only a stub ATM
1418 echo ... a batch file can delete itself ...
1419 echo del foo.cmd>foo.cmd
1420 cmd /q /c foo.cmd
1421 if not exist foo.cmd (
1422 echo file correctly deleted
1423 ) else (
1424 echo file should be deleted!
1425 del foo.cmd
1427 echo ... a batch file can alter itself ...
1428 echo echo bar^>foo.cmd>foo.cmd
1429 cmd /q /c foo.cmd > NUL 2>&1
1430 if exist foo.cmd (
1431 type foo.cmd
1432 del foo.cmd
1433 ) else (
1434 echo file not created!
1437 echo ------------ Testing setlocal/endlocal ------------
1438 call :setError 0
1439 rem Note: setlocal EnableDelayedExtension already tested in the variable delayed expansion test section
1440 mkdir foobar & cd foobar
1441 echo ...enable/disable extensions
1442 setlocal DisableEXTensions
1443 echo ErrLev: %ErrorLevel%
1444 endlocal
1445 echo ErrLev: %ErrorLevel%
1446 echo @echo off> tmp.cmd
1447 echo echo ErrLev: %%ErrorLevel%%>> tmp.cmd
1448 rem Enabled by default
1449 cmd /C tmp.cmd
1450 cmd /E:OfF /C tmp.cmd
1451 cmd /e:oN /C tmp.cmd
1453 rem FIXME: creating file before setting envvar value to prevent parsing-time evaluation (due to EnableDelayedExpansion not being implemented/available yet)
1454 echo ...setlocal with corresponding endlocal
1455 echo @echo off> test.cmd
1456 echo echo %%VAR%%>> test.cmd
1457 echo setlocal>> test.cmd
1458 echo set VAR=localval>> test.cmd
1459 echo echo %%VAR%%>> test.cmd
1460 echo endlocal>> test.cmd
1461 echo echo %%VAR%%>> test.cmd
1462 set VAR=globalval
1463 call test.cmd
1464 echo %VAR%
1465 set VAR=
1466 echo ...setlocal with no corresponding endlocal
1467 echo @echo off> test.cmd
1468 echo echo %%VAR%%>> test.cmd
1469 echo setlocal>> test.cmd
1470 echo set VAR=localval>> test.cmd
1471 echo echo %%VAR%%>> test.cmd
1472 set VAR=globalval
1473 call test.cmd
1474 echo %VAR%
1475 set VAR=
1476 cd .. & rd /q/s foobar
1478 echo -----------Testing Errorlevel-----------
1479 rem WARNING: Do *not* add tests using ErrorLevel after this section
1480 should_not_exist 2> nul > nul
1481 echo %ErrorLevel%
1482 rem nt 4.0 doesn't really support a way of setting errorlevel, so this is weak
1483 rem See http://www.robvanderwoude.com/exit.php
1484 call :setError 1
1485 echo %ErrorLevel%
1486 if errorlevel 2 echo errorlevel too high, bad
1487 if errorlevel 1 echo errorlevel just right, good
1488 if errorlevel 01 echo errorlevel with leading zero just right, good
1489 if errorlevel -1 echo errorlevel with negative number OK
1490 if errorlevel 0x1 echo hexa should not be recognized!
1491 if errorlevel 1a echo invalid error level recognized!
1492 call :setError 0
1493 echo abc%ErrorLevel%def
1494 if errorlevel 1 echo errorlevel nonzero, bad
1495 if not errorlevel 1 echo errorlevel zero, good
1496 if not errorlevel 0x1 echo hexa should not be recognized!
1497 if not errorlevel 1a echo invalid error level recognized!
1498 rem Now verify that setting a real variable hides its magic variable
1499 set errorlevel=7
1500 echo %ErrorLevel% should be 7
1501 if errorlevel 7 echo setting var worked too well, bad
1502 call :setError 3
1503 echo %ErrorLevel% should still be 7
1505 echo -----------Testing GOTO-----------
1506 if a==a goto dest1
1507 :dest1
1508 echo goto with no leading space worked
1509 if b==b goto dest2
1510 :dest2
1511 echo goto with a leading space worked
1512 if c==c goto dest3
1513 :dest3
1514 echo goto with a leading tab worked
1515 if d==d goto dest4
1516 :dest4@space@
1517 echo goto with a following space worked
1519 echo ---------- Testing combined CALLs/GOTOs -----------
1520 echo @echo off>foo.cmd
1521 echo goto :eof>>foot.cmd
1522 echo :eof>>foot.cmd
1523 echo echo world>>foo.cmd
1525 echo @echo off>foot.cmd
1526 echo echo cheball>>foot.cmd
1527 echo.>>foot.cmd
1528 echo call :bar>>foot.cmd
1529 echo if "%%1"=="deleteMe" (del foot.cmd)>>foot.cmd
1530 echo goto :eof>>foot.cmd
1531 echo.>>foot.cmd
1532 echo :bar>>foot.cmd
1533 echo echo barbare>>foot.cmd
1534 echo goto :eof>>foot.cmd
1536 call foo.cmd
1537 call foot
1538 call :bar
1539 del foo.cmd
1540 rem Script execution stops after the following line
1541 foot deleteMe
1542 call :foo
1543 call :foot
1544 goto :endFuns
1546 :foot
1547 echo foot
1549 :foo
1550 echo foo
1551 goto :eof
1553 :endFuns
1555 :bar
1556 echo bar
1557 call :foo
1559 :baz
1560 echo baz
1561 goto :eof
1563 echo Final message is not output since earlier 'foot' processing stops script execution
1564 echo Do NOT add any tests below this line
1566 echo -----------Done, jumping to EOF-----------
1567 goto :eof
1568 rem Subroutine to set errorlevel and return
1569 rem in windows nt 4.0, this always sets errorlevel 1, since /b isn't supported
1570 :setError
1571 exit /B %1
1572 rem This line runs under cmd in windows NT 4, but not in more modern versions.