Assorted spelling fixes.
[wine/multimedia.git] / programs / cmd / tests / test_builtins.cmd
blob7f90549b8833770809f49d8def38eddcb941e501
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
29 echo @tab@ on @space@
31 @echo off
32 echo off@tab@@space@
33 @echo noecho1
34 @echo noecho2
35 @@@@@echo echo3
36 echo ------------ Testing 'echo' [OFF] ------------
37 echo word
38 echo 'singlequotedword'
39 echo "doublequotedword"
40 @echo at-echoed-word
41 echo "/?"
42 echo.
43 echo .
44 echo.word
45 echo .word
46 echo:
47 echo :
48 echo:word
49 echo :word
50 echo on again
51 echo word@space@
52 echo word@space@@space@
53 echo word
54 echo@tab@word
55 echo@tab@word @tab@
56 echo@tab@word@tab@@space@
57 @tab@echo word
58 echo @tab@word
59 echo @tab@word
60 echo@tab@@tab@word
62 echo ------------ Testing mixed echo modes ------------
63 echo @echo on> mixedEchoModes.cmd
64 echo if 1==1 echo foo>> mixedEchoModes.cmd
65 echo if 1==1 @echo bar>> mixedEchoModes.cmd
66 echo @echo off>> mixedEchoModes.cmd
67 echo if 1==1 echo foo2>> mixedEchoModes.cmd
68 echo if 1==1 @echo bar2>> mixedEchoModes.cmd
69 type mixedEchoModes.cmd
70 cmd /c mixedEchoModes.cmd
71 del mixedEchoModes.cmd
73 echo ------------ Testing parameterization ------------
74 call :TestParm a b c
75 call :TestParm "a b c"
76 call :TestParm "a b"\c
77 call :TestParm a=~`+,.{}!+b
78 call :TestParm a;b
79 call :TestParm "a;b"
80 call :TestParm a^;b
81 call :TestParm a[b]{c}(d)e
82 call :TestParm a&echo second line
83 call :TestParm a b,,,c
84 call :TestParm a==b;;c
85 call :TestParm a,,, b
86 goto :TestRem
88 :TestParm
89 echo '%1', '%2', '%3'
90 goto :eof
92 :TestRem
93 echo ------------ Testing rem ------------
94 rem Hello
95 rem Hello
96 rem Hello || foo
97 rem echo lol
98 rem echo foo & echo bar
99 rem @tab@ Hello
100 rem@tab@ Hello
101 rem@tab@echo foo & echo bar
102 @echo on
103 rem Hello
104 rem Hello
105 rem Hello || foo
106 rem echo lol
107 rem echo foo & echo bar
108 rem @tab@ Hello
109 rem@tab@ Hello
110 rem@tab@echo foo & echo bar
111 @echo off
113 echo ------------ Testing redirection operators ------------
114 mkdir foobar & cd foobar
115 echo --- stdout redirection
116 echo foo>foo
117 type foo
118 echo foo 1> foo
119 type foo
120 echo foo@tab@1> foo
121 type foo
122 echo foo 1>@tab@foo
123 type foo
124 echo foo@tab@1>@tab@foo
125 type foo
126 echo foo7 7> foo
127 type foo
128 echo foo9 9> foo
129 type foo
130 echo foo1> foo
131 type foo
132 echo foo11> foo
133 type foo
134 echo foo12> foo
135 type foo
136 echo foo13>"foo"
137 type foo
138 echo foo14>."\foo"
139 type foo
140 echo foo15>."\f"oo
141 type foo
142 del foo
143 echo1>foo
144 type foo
145 echo --- stdout appending
146 echo foo>foo
147 echo foo >>foo
148 type foo
149 del foo
150 echo foob >> foo
151 type foo
152 echo fooc 1>>foo
153 type foo
154 echo food1>>foo
155 type foo
156 echo food2>>"foo"
157 type foo
158 del foo
159 echo food21>>foo
160 type foo
161 del foo
162 echo foo> foo
163 echo foo7 7>> foo || (echo not supported & del foo)
164 if exist foo (type foo) else echo not supported
165 echo --- redirections within IF statements
166 if 1==1 echo foo1>bar
167 type bar & del bar
168 echo -----
169 if 1==1 (echo foo2>bar) else echo baz2>bar
170 type bar & del bar
171 if 1==1 (echo foo3) else echo baz3>bar
172 type bar || echo file does not exist, ok
173 if 1==1 (echo foo4>bar) else echo baz4>bar
174 type bar & del bar
175 if 1==0 (echo foo5>bar) else echo baz5>bar
176 type bar & del bar
177 if 1==0 (echo foo6) else echo baz6 1>bar
178 type bar & del bar
179 if 1==0 (echo foo7 1>bar) else echo baz7>bar
180 type bar & del bar
181 if 1==0 (echo foo8 1>bar) else echo baz8>bak
182 type bak
183 if 1==1 (echo foo>bar & echo baz)
184 type bar
185 if 1==1 (
186 echo foo>bar
187 echo baz
189 type bar
190 (if 1==1 (echo A) else echo B) > C
191 type C
192 (if 1==0 (echo A) else echo B) > C
193 type C
194 (if 1==0 (echo A > B) else echo C)
195 cd .. & rd /s/q foobar
197 echo ------------ Testing circumflex escape character ------------
198 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
199 echo ^hell^o, world
200 echo hell^o, world
201 echo hell^^o, world
202 echo hell^^^o, world
203 echo hello^
204 world
205 echo hello^
207 world
208 echo hello^
211 echo finished
212 mkdir foobar
213 echo baz> foobar\baz
214 type foobar\baz
215 type foobar^\baz
216 rd /s/q foobar
217 echo foo ^| echo bar
218 echo foo ^& echo bar
219 call :setError 0
220 echo bak ^&& echo baz 2> nul
221 echo %ErrorLevel%
222 echo foo ^> foo
223 echo ^<> foo
224 type foo
225 del foo
226 set FOO=oof
227 echo ff^%FOO%
228 set FOO=bar ^| baz
229 set FOO
230 rem FIXME: echoing %FOO% gives an error (baz not recognized) but prematurely
231 rem exits the script on windows; redirecting stdout and/or stderr doesn't help
232 echo %ErrorLevel%
233 call :setError 0
234 set FOO=bar ^^^| baz
235 set FOO
236 echo %FOO%
237 echo %ErrorLevel%
238 set FOO=
240 echo ------------ Testing 'set' ------------
241 call :setError 0
242 set FOOBAR 2> nul > nul
243 echo %ErrorLevel%
244 set FOOBAR = baz
245 echo %ErrorLevel%
246 echo %FOOBAR%FOOBAR not defined
247 echo %FOOBAR %
248 set FOOBAR 2> nul
249 set FOOBAR = baz2
250 echo %ErrorLevel%
251 echo %fOObAr %
252 set FOOBAR= bar
253 echo %ErrorLevel%
254 echo %FOOBAR%
255 set FOO
256 set FOOBAR=
257 set FOOB
258 echo %FOOBAR%FOOBAR not defined
259 set FOOBAR =
260 set FOOBA 2> nul > nul
261 echo %ErrorLevel%
262 set FOO=bar
263 echo %FOO%
264 set FOO=foo
265 set BAR=bar
266 echo %FOO%%BAR%
267 set BAR=
268 set FOO=
269 set FOO=%FOO%
270 echo %FOO%FOO not defined
271 set BAZ%=bazbaz
272 set BA
273 echo %BAZ%%
274 set BAZ%=
275 echo set "FOO=bar" should not include the quotes in the variable value
276 set "FOO=bar"
277 echo %FOO%
278 set@tab@FOO=foo
279 echo %FOO%
280 set@tab@FOO=
281 echo '%FOO%'
282 set FOO=foo@space@
283 echo '%FOO%'
284 set FOO=foo@tab@
285 echo '%FOO%'
286 set FOO=
288 echo ------------ Testing variable expansion ------------
289 call :setError 0
290 echo ~dp0 should be directory containing batch file
291 echo %~dp0
292 mkdir dummydir
293 cd dummydir
294 echo %~dp0
295 cd ..
296 rmdir dummydir
297 echo CD value %CD%
298 echo %%
299 echo P%
300 echo %P
301 echo %UNKNOWN%S
302 echo P%UNKNOWN%
303 echo P%UNKNOWN%S
304 echo %ERRORLEVEL
305 echo %ERRORLEVEL%
306 echo %ERRORLEVEL%%ERRORLEVEL%
307 echo %ERRORLEVEL%ERRORLEVEL%
308 echo %ERRORLEVEL%%
309 echo %ERRORLEVEL%%%
310 echo P%ERRORLEVEL%
311 echo %ERRORLEVEL%S
312 echo P%ERRORLEVEL%S
314 echo ------------ Testing variable substrings ------------
315 set VAR=qwerty
316 echo %VAR:~0,1%
317 echo %VAR:~0,3%
318 echo %VAR:~2,2%
319 echo '%VAR:~-2,3%'
320 echo '%VAR:~-2,1%'
321 echo %VAR:~2,-1%
322 echo %VAR:~2,-3%
323 echo '%VAR:~-2,-4%'
324 echo %VAR:~-3,-2%
325 set VAR=
327 echo ------------ Testing variable substitution ------------
328 echo --- in FOR variables
329 for %%i in ("A B" C) do echo %%i
330 rem check works when prefix with @
331 @for %%i in ("A B" C) do echo %%i
332 rem quotes removal
333 for %%i in ("A B" C) do echo '%%~i'
334 rem fully qualified path
335 for %%f in ("C D" E) do echo %%~ff
336 rem drive letter
337 for %%i in ("F G" H) do echo %%~di
338 rem path
339 for %%d in ("I J" K) do echo %%~pd
340 rem filename
341 for %%i in ("L M" N) do echo %%~ni
342 rem file extension
343 for %%i in ("O. P.OOL" Q.TABC hello) do echo '%%~xi'
344 rem path with short path names
345 for %%I in ("R S" T ABCDEFGHIJK.LMNOP) do echo '%%~sI'
346 rem file attribute
347 for %%i in ("U V" W) do echo '%%~ai'
348 echo foo> foo
349 for %%i in (foo) do echo '%%~ai'
350 for %%i in (foo) do echo '%%~zi'
351 del foo
352 rem file date/time
353 rem Not fully testable, until we can grep dir's output to get foo's creation time in an envvar...
354 for %%i in ("a b" c) do echo '%%~ti'
355 rem file size
356 rem Similar issues as above
357 for %%i in ("a b" c) do echo '%%~zi'
358 rem combined options
359 for %%i in ("d e" f) do echo %%~dpi
360 for %%i in ("g h" i) do echo %%~sdi
361 for %%i in ("g h" i) do echo %%~dsi
362 for %%i in ("j k" l.eh) do echo '%%~xsi'
364 echo --- in parameters
365 for %%i in ("A B" C) do call :echoFun %%i
366 rem quotes removal
367 for %%i in ("A B" C) do call :echoFunQ %%i
368 rem fully qualified path
369 for %%f in ("C D" E) do call :echoFunF %%f
370 rem drive letter
371 for %%i in ("F G" H) do call :echoFunD %%i
372 rem path
373 for %%d in ("I J" K) do call :echoFunP %%d
374 rem filename
375 for %%i in ("L M" N) do call :echoFunN %%i
376 rem file extension
377 for %%i in ("O. P.OOL" Q.TABC hello) do call :echoFunX %%i
378 rem path with short path names
379 for %%I in ("R S" T ABCDEFGHIJK.LMNOP) do call :echoFunS %%I
380 rem NT4 aborts whole script execution when encountering ~a, ~t and ~z substitutions, preventing full testing
381 rem combined options
382 for %%i in ("d e" f) do call :echoFunDP %%i
383 for %%i in ("g h" i) do call :echoFunSD %%i
384 for %%i in ("g h" i) do call :echoFunDS %%i
385 for %%i in ("j k" l.eh) do call :echoFunXS %%i
387 goto :endEchoFuns
388 :echoFun
389 echo %1
390 goto :eof
392 :echoFunQ
393 echo '%~1'
394 goto :eof
396 :echoFunF
397 echo %~f1
398 goto :eof
400 :echoFunD
401 echo %~d1
402 goto :eof
404 :echoFunP
405 echo %~p1
406 goto :eof
408 :echoFunN
409 echo %~n1
410 goto :eof
412 :echoFunX
413 echo '%~x1'
414 goto :eof
416 :echoFunS
417 rem some NT4 workaround
418 set VAR='%~s1'
419 echo %VAR%
420 set VAR=
421 goto :eof
423 :echoFunDP
424 echo %~dp1
425 goto :eof
427 :echoFunSD
428 echo %~sd1
429 goto :eof
431 :echoFunDS
432 echo %~ds1
433 goto :eof
435 :echoFunXS
436 echo '%~xs1'
437 goto :eof
438 :endEchoFuns
440 echo ------------ Testing variable delayed expansion ------------
441 rem NT4 doesn't support this
442 echo --- default mode (load-time expansion)
443 set FOO=foo
444 echo %FOO%
445 echo !FOO!
446 if %FOO% == foo (
447 set FOO=bar
448 if %FOO% == bar (echo bar) else echo foo
451 set FOO=foo
452 if %FOO% == foo (
453 set FOO=bar
454 if !FOO! == bar (echo bar) else echo foo
457 echo --- runtime (delayed) expansion mode
458 setlocal EnableDelayedExpansion
459 set FOO=foo
460 echo %FOO%
461 echo !FOO!
462 if %FOO% == foo (
463 set FOO=bar
464 if %FOO% == bar (echo bar) else echo foo
467 set FOO=foo
468 if %FOO% == foo (
469 set FOO=bar
470 if !FOO! == bar (echo bar) else echo foo
472 echo %ErrorLevel%
473 setlocal DisableDelayedExpansion
474 echo %ErrorLevel%
475 set FOO=foo
476 echo %FOO%
477 echo !FOO!
478 set FOO=
479 echo --- using /V cmd flag
480 echo @echo off> tmp.cmd
481 echo set FOO=foo>> tmp.cmd
482 echo echo %%FOO%%>> tmp.cmd
483 echo echo !FOO!>> tmp.cmd
484 echo set FOO=>> tmp.cmd
485 cmd /V:ON /C tmp.cmd
486 cmd /V:OfF /C tmp.cmd
487 del tmp.cmd
489 echo ------------ Testing conditional execution ------------
490 echo --- unconditional ampersand
491 call :setError 123 & echo foo1
492 echo bar2 & echo foo2
493 mkdir foobar & cd foobar
494 echo > foobazbar
495 cd .. & rd /s/q foobar
496 if exist foobazbar (
497 echo foobar not deleted!
498 cd ..
499 rd /s/q foobar
500 ) else echo foobar deleted
501 echo --- on success conditional and
502 call :setError 456 && echo foo3 > foo3
503 if exist foo3 (
504 echo foo3 created
505 del foo3
506 ) else echo foo3 not created
507 echo bar4 && echo foo4
508 echo --- on failure conditional or
509 call :setError 789 || echo foo5
510 echo foo6 || echo bar6 > bar6
511 if exist bar6 (
512 echo bar6 created
513 del bar6
516 echo ------------ Testing cd ------------
517 mkdir foobar
518 cd foobar
519 echo blabla > singleFile
520 dir /b
521 echo Current dir: %CD%
523 cd ..
525 cd foobar@space@
527 cd ..
529 cd @space@foobar
531 cd..
533 cd foobar
534 cd..@space@
536 if not exist foobar (cd ..)
537 cd foobar
538 cd@tab@..@tab@@space@@tab@
540 if not exist foobar (cd ..)
541 cd foobar
542 mkdir "bar bak"
543 cd "bar bak"
545 cd ..
546 cd ".\bar bak"
548 cd ..
549 cd .\"bar bak"
551 cd ..
552 cd bar bak
554 cd "bar bak@space@"@tab@@space@
556 cd ..\..
558 rd /Q/s foobar
559 mkdir foobar
560 cd /d@tab@foobar
562 cd ..
563 rd /q/s foobar
565 echo ------------ Testing type ------------
566 echo bar> foobaz
567 @echo on
568 type foobaz
569 echo ---
570 @echo off
571 type foobaz@tab@
572 echo ---1
573 type ."\foobaz"
574 echo ---2
575 type ".\foobaz"
576 echo ---3
577 del foobaz
579 echo ------------ Testing NUL ------------
580 md foobar & cd foobar
581 rem NUL file (non) creation + case insensitivity
582 rem Note: "if exist" does not work with NUL, so to check for file existence we use a kludgy workaround
583 echo > bar
584 echo foo > NUL
585 dir /b /a-d
586 echo foo > nul
587 dir /b /a-d
588 echo foo > NuL
589 @tab@dir /b@tab@/a-d
590 del bar
591 rem NUL not special everywhere
592 call :setError 123
593 echo NUL> foo
594 if not exist foo (echo foo should have been created) else (
595 type foo
596 del foo
598 rem Empty file creation
599 copy nul foo > nul
600 if exist foo (
601 echo foo created
602 del foo
603 type foo
604 ) else (
605 echo ***
607 cd .. & rd foobar
609 echo ------------ Testing if/else ------------
610 echo --- if/else should work with blocks
611 if 0 == 0 (
612 echo if seems to work
613 ) else (
614 echo if seems to be broken
616 if 1 == 0 (
617 echo else seems to be broken
618 ) else (
619 echo else seems to work
621 if /c==/c (
622 echo if seems not to detect /c as parameter
623 ) else (
624 echo parameter detection seems to be broken
627 echo --- case sensitivity with and without /i option
628 if bar==BAR echo if does not default to case sensitivity
629 if not bar==BAR echo if seems to default to case sensitivity
630 if /i foo==FOO echo if /i seems to work
631 if /i not foo==FOO echo if /i seems to be broken
632 if /I foo==FOO echo if /I seems to work
633 if /I not foo==FOO echo if /I seems to be broken
635 echo --- string comparisons
636 if abc == abc (echo equal) else echo non equal
637 if abc =="abc" (echo equal) else echo non equal
638 if "abc"== abc (echo equal) else echo non equal
639 if "abc"== "abc" (echo equal) else echo non equal
641 echo --- tabs handling
642 if@tab@1==1 echo doom
643 if @tab@1==1 echo doom
644 if 1==1 (echo doom) else@tab@echo quake
645 if@tab@not @tab@1==@tab@0 @tab@echo lol
646 if 1==0@tab@(echo doom) else echo quake
647 if 1==0 (echo doom)@tab@else echo quake
648 if 1==0 (echo doom) else@tab@echo quake
650 echo --- comparison operators
651 rem NT4 misevaluates conditionals in for loops so we have to use subroutines as workarounds
652 rem Imbricated for loops parameters are currently not expanded correctly; this prevents usage of simpler imbricated for loops in tests
653 echo ------ for strings
654 rem NT4 stops processing of the whole batch file as soon as it finds a
655 rem comparison operator non fully uppercased, such as lss instead of LSS, so we
656 rem can't test those here.
657 if LSS LSS LSSfoo (echo LSS string can be used as operand for LSS comparison)
658 if LSS LSS LSS (echo bar)
659 if 1.1 LSS 1.10 (echo floats are handled as strings)
660 if "9" LSS "10" (echo numbers in quotes recognized!) else echo numbers in quotes are handled as strings
661 if not "-1" LSS "1" (echo negative numbers as well) else echo NT4
662 if /i foo LSS FoOc echo if /i seems to work for LSS
663 if /I not foo LSS FOOb echo if /I seems to be broken for LSS
664 set STR_PARMS=A B AB BA AA
665 for %%i in (%STR_PARMS%) do call :LSStest %%i A
666 for %%i in (%STR_PARMS%) do call :LSStest %%i B
667 for %%i in (%STR_PARMS%) do call :LSStest %%i AB
668 for %%i in (%STR_PARMS%) do call :LSStest %%i BA
669 for %%i in (%STR_PARMS%) do call :LSStest %%i AA
670 if b LSS B (echo b LSS B) else echo NT4
671 if /I b LSS B echo b LSS B insensitive
672 if b LSS A echo b LSS A
673 if /I b LSS A echo b LSS A insensitive
674 if a LSS B (echo a LSS B) else echo NT4
675 if /I a LSS B echo a LSS B insensitive
676 if A LSS b echo A LSS b
677 if /I A LSS b echo A LSS b insensitive
678 for %%i in (%STR_PARMS%) do call :LEQtest %%i A
679 for %%i in (%STR_PARMS%) do call :LEQtest %%i B
680 for %%i in (%STR_PARMS%) do call :LEQtest %%i AB
681 for %%i in (%STR_PARMS%) do call :LEQtest %%i BA
682 for %%i in (%STR_PARMS%) do call :LEQtest %%i AA
683 if b LEQ B (echo b LEQ B) else echo NT4
684 if /I b LEQ B echo b LEQ B insensitive
685 if b LEQ A echo b LEQ A
686 if /I b LEQ A echo b LEQ A insensitive
687 if a LEQ B (echo a LEQ B) else echo NT4
688 if /I a LEQ B echo a LEQ B insensitive
689 if A LEQ b echo A LEQ b
690 if /I A LEQ b echo A LEQ b insensitive
691 for %%i in (%STR_PARMS%) do call :EQUtest %%i A
692 for %%i in (%STR_PARMS%) do call :EQUtest %%i B
693 for %%i in (%STR_PARMS%) do call :EQUtest %%i AB
694 for %%i in (%STR_PARMS%) do call :EQUtest %%i BA
695 for %%i in (%STR_PARMS%) do call :EQUtest %%i AA
696 if /I A EQU a echo A EQU a insensitive
697 for %%i in (%STR_PARMS%) do call :NEQtest %%i A
698 for %%i in (%STR_PARMS%) do call :NEQtest %%i B
699 for %%i in (%STR_PARMS%) do call :NEQtest %%i AB
700 for %%i in (%STR_PARMS%) do call :NEQtest %%i BA
701 for %%i in (%STR_PARMS%) do call :NEQtest %%i AA
702 for %%i in (%STR_PARMS%) do call :GEQtest %%i A
703 for %%i in (%STR_PARMS%) do call :GEQtest %%i B
704 for %%i in (%STR_PARMS%) do call :GEQtest %%i AB
705 for %%i in (%STR_PARMS%) do call :GEQtest %%i BA
706 for %%i in (%STR_PARMS%) do call :GEQtest %%i AA
707 for %%i in (%STR_PARMS%) do call :GTRtest %%i A
708 for %%i in (%STR_PARMS%) do call :GTRtest %%i B
709 for %%i in (%STR_PARMS%) do call :GTRtest %%i AB
710 for %%i in (%STR_PARMS%) do call :GTRtest %%i BA
711 for %%i in (%STR_PARMS%) do call :GTRtest %%i AA
712 echo ------ for numbers
713 if -1 LSS 1 (echo negative numbers handled)
714 if not -1 LSS -10 (echo negative numbers handled)
715 if not 9 LSS 010 (echo octal handled)
716 if not -010 LSS -8 (echo also in negative form)
717 if 4 LSS 0x5 (echo hexa handled)
718 if not -1 LSS -0x1A (echo also in negative form)
719 if 11 LSS 101 (echo 11 LSS 101)
720 set INT_PARMS=0 1 10 9
721 for %%i in (%INT_PARMS%) do call :LSStest %%i 0
722 for %%i in (%INT_PARMS%) do call :LSStest %%i 1
723 for %%i in (%INT_PARMS%) do call :LSStest %%i 10
724 for %%i in (%INT_PARMS%) do call :LSStest %%i 9
725 for %%i in (%INT_PARMS%) do call :LEQtest %%i 0
726 for %%i in (%INT_PARMS%) do call :LEQtest %%i 1
727 for %%i in (%INT_PARMS%) do call :LEQtest %%i 10
728 for %%i in (%INT_PARMS%) do call :LEQtest %%i 9
729 for %%i in (%INT_PARMS%) do call :EQUtest %%i 0
730 for %%i in (%INT_PARMS%) do call :EQUtest %%i 1
731 for %%i in (%INT_PARMS%) do call :EQUtest %%i 10
732 for %%i in (%INT_PARMS%) do call :EQUtest %%i 9
733 if 011 EQU 9 (echo octal ok)
734 if 0xA1 EQU 161 (echo hexa ok)
735 if 0xA1 EQU "161" (echo hexa should be recognized) else (echo string/hexa compare ok)
736 if "0xA1" EQU 161 (echo hexa should be recognized) else (echo string/hexa compare ok)
737 for %%i in (%INT_PARMS%) do call :NEQtest %%i 0
738 for %%i in (%INT_PARMS%) do call :NEQtest %%i 1
739 for %%i in (%INT_PARMS%) do call :NEQtest %%i 10
740 for %%i in (%INT_PARMS%) do call :NEQtest %%i 9
741 for %%i in (%INT_PARMS%) do call :GEQtest %%i 0
742 for %%i in (%INT_PARMS%) do call :GEQtest %%i 1
743 for %%i in (%INT_PARMS%) do call :GEQtest %%i 10
744 for %%i in (%INT_PARMS%) do call :GEQtest %%i 9
745 for %%i in (%INT_PARMS%) do call :GTRtest %%i 0
746 for %%i in (%INT_PARMS%) do call :GTRtest %%i 1
747 for %%i in (%INT_PARMS%) do call :GTRtest %%i 10
748 for %%i in (%INT_PARMS%) do call :GTRtest %%i 9
749 echo ------ for numbers and stringified numbers
750 if not "1" EQU 1 (echo strings and integers not equal) else echo foo
751 if not 1 EQU "1" (echo strings and integers not equal) else echo foo
752 if '1' EQU 1 echo '1' EQU 1
753 if 1 EQU '1' echo 1 EQU '1'
754 if not "1" GEQ 1 (echo foo) else echo bar
755 if "10" GEQ "1" echo "10" GEQ "1"
756 if '1' GEQ 1 (echo '1' GEQ 1) else echo NT4
757 if 1 GEQ "1" echo 1 GEQ "1"
758 if "1" GEQ "1" echo "1" GEQ "1"
759 if '1' GEQ "1" echo '1' GEQ "1"
760 if "10" GEQ "1" echo "10" GEQ "1"
761 if not 1 GEQ '1' (echo non NT4) else echo 1 GEQ '1'
762 for %%i in ("1" '1') do call :GEQtest %%i '1'
763 if "10" GEQ '1' (echo "10" GEQ '1') else echo foo
764 if 1 GEQ "10" (echo 1 GEQ "10") else echo foo
765 if "1" GEQ "10" (echo 1 GEQ "10") else echo foo
766 if '1' GEQ "10" (echo '1' GEQ "10") else echo foo
767 if "10" GEQ "10" (echo "10" GEQ "10")
768 goto :endIfCompOpsSubroutines
770 rem IF subroutines helpers
771 :LSStest
772 if %1 LSS %2 echo %1 LSS %2
773 goto :eof
774 :LEQtest
775 if %1 LEQ %2 echo %1 LEQ %2
776 goto :eof
777 :EQUtest
778 if %1 EQU %2 echo %1 EQU %2
779 goto :eof
780 :NEQtest
781 if %1 NEQ %2 echo %1 NEQ %2
782 goto :eof
783 :GEQtest
784 if %1 GEQ %2 echo %1 GEQ %2
785 goto :eof
786 :GTRtest
787 if %1 GTR %2 echo %1 GTR %2
788 goto :eof
790 :endIfCompOpsSubroutines
791 set STR_PARMS=
792 set INT_PARMS=
794 echo ------------ Testing for ------------
795 echo --- plain FOR
796 for %%i in (A B C) do echo %%i
797 for %%i in (A B C) do echo %%I
798 for %%i in (A B C) do echo %%j
799 for %%i in (A B C) do call :forTestFun1 %%i
800 for %%i in (1,4,1) do echo %%i
801 for %%i in (A, B,C) do echo %%i
802 for %%i in (X) do echo %%i
803 for@tab@%%i in (X2) do echo %%i
804 for %%i in@tab@(X3) do echo %%i
805 for %%i in (@tab@ foo@tab@) do echo %%i
806 for@tab@ %%i in@tab@(@tab@M) do echo %%i
807 for %%i@tab@in (X)@tab@do@tab@echo %%i
808 for@tab@ %%j in@tab@(@tab@M, N, O@tab@) do echo %%j
809 for %%i in (`echo A B`) do echo %%i
810 for %%i in ('echo A B') do echo %%i
811 for %%i in ("echo A B") do echo %%i
812 for %%i in ("A B" C) do echo %%i
813 goto :endForTestFun1
814 :forTestFun1
815 echo %1
816 goto :eof
817 :endForTestFun1
818 echo --- imbricated FORs
819 for %%i in (X) do (
820 for %%j in (Y) do (
821 echo %%i %%j))
822 for %%i in (X) do (
823 for %%I in (Y) do (
824 echo %%i %%I))
825 for %%i in (A B) do (
826 for %%j in (C D) do (
827 echo %%i %%j))
828 for %%i in (A B) do (
829 for %%j in (C D) do (
830 call :forTestFun2 %%i %%j ))
831 goto :endForTestFun2
832 :forTestFun2
833 echo %1 %2
834 goto :eof
835 :endForTestFun2
836 mkdir foobar & cd foobar
837 mkdir foo
838 mkdir bar
839 mkdir baz
840 echo > bazbaz
841 echo --- basic wildcards
842 for %%i in (ba*) do echo %%i
843 echo --- for /d
844 for /d %%i in (baz foo bar) do echo %%i 2>&1
845 rem Confirm we don't match files:
846 for /d %%i in (bazb*) do echo %%i 2>&1
847 for /d %%i in (bazb2*) do echo %%i 2>&1
848 rem Show we pass through non wildcards
849 for /d %%i in (PASSED) do echo %%i
850 for /d %%i in (xxx) do (
851 echo %%i - Should be xxx
852 echo Expected second line
854 rem Show we issue no messages on failures
855 for /d %%i in (FAILED?) do echo %%i 2>&1
856 for /d %%i in (FAILED?) do (
857 echo %%i - Unexpected!
858 echo FAILED Unexpected second line
860 for /d %%i in (FAILED*) do echo %%i 2>&1
861 for /d %%i in (FAILED*) do (
862 echo %%i - Unexpected!
863 echo FAILED Unexpected second line
865 rem FIXME can't test wildcard expansion here since it's listed in directory
866 rem order, and not in alphabetic order.
867 rem Proper testing would need a currently missing "sort" program implementation.
868 rem for /d %%i in (ba*) do echo %%i>> tmp
869 rem sort < tmp
870 rem del tmp
871 rem for /d %%i in (?a*) do echo %%i>> tmp
872 rem sort < tmp
873 rem del tmp
874 rem for /d %%i in (*) do echo %%i>> tmp
875 rem sort < tmp
876 rem del tmp
877 echo > baz\bazbaz
878 goto :TestForR
880 :SetExpected
881 del temp.bat 2>nul
882 call :WriteLine set found=N
883 for /l %%i in (1,1,%expectedresults%) do (
884 call :WriteLine if "%%%%expectedresults.%%i%%%%"=="%%%%1" set found=Y
885 call :WriteLine if "%%%%found%%%%"=="Y" set expectedresults.%%i=
886 call :WriteLine if "%%%%found%%%%"=="Y" goto :eof
888 call :WriteLine echo Got unexpected result: "%%%%1"
889 goto :eof
891 :WriteLine
892 echo %*>> temp.bat
893 goto :EOF
895 :ValidateExpected
896 del temp.bat 2>nul
897 for /l %%i in (1,1,%expectedresults%) do call :WriteLine if not "%%%%expectedresults.%%i%%%%"=="" echo Found missing result: "%%%%expectedresults.%%i%%%%"
898 call temp.bat
899 del temp.bat 2>nul
900 goto :eof
902 :TestForR
903 rem %CD% does not tork on NT4 so use the following workaround
904 for /d %%i in (.) do set CURDIR=%%~dpnxi
906 echo --- for /R
907 echo Plain directory enumeration
908 set expectedresults=4
909 set expectedresults.1=%CURDIR%\.
910 set expectedresults.2=%CURDIR%\bar\.
911 set expectedresults.3=%CURDIR%\baz\.
912 set expectedresults.4=%CURDIR%\foo\.
913 call :SetExpected
914 for /R %%i in (.) do call temp.bat %%i
915 call :ValidateExpected
917 echo Plain directory enumeration from provided root
918 set expectedresults=4
919 set expectedresults.1=%CURDIR%\.
920 set expectedresults.2=%CURDIR%\bar\.
921 set expectedresults.3=%CURDIR%\baz\.
922 set expectedresults.4=%CURDIR%\foo\.
923 if "%CD%"=="" goto :SkipBrokenNT4
924 call :SetExpected
925 for /R "%CURDIR%" %%i in (.) do call temp.bat %%i
926 call :ValidateExpected
927 :SkipBrokenNT4
929 echo File enumeration
930 set expectedresults=2
931 set expectedresults.1=%CURDIR%\baz\bazbaz
932 set expectedresults.2=%CURDIR%\bazbaz
933 call :SetExpected
934 for /R %%i in (baz*) do call temp.bat %%i
935 call :ValidateExpected
937 echo File enumeration from provided root
938 set expectedresults=2
939 set expectedresults.1=%CURDIR%\baz\bazbaz
940 set expectedresults.2=%CURDIR%\bazbaz
941 call :SetExpected
942 for /R %%i in (baz*) do call temp.bat %%i
943 call :ValidateExpected
945 echo Mixed enumeration
946 set expectedresults=6
947 set expectedresults.1=%CURDIR%\.
948 set expectedresults.2=%CURDIR%\bar\.
949 set expectedresults.3=%CURDIR%\baz\.
950 set expectedresults.4=%CURDIR%\baz\bazbaz
951 set expectedresults.5=%CURDIR%\bazbaz
952 set expectedresults.6=%CURDIR%\foo\.
953 call :SetExpected
954 for /R %%i in (. baz*) do call temp.bat %%i
955 call :ValidateExpected
957 echo Mixed enumeration from provided root
958 set expectedresults=6
959 set expectedresults.1=%CURDIR%\.
960 set expectedresults.2=%CURDIR%\bar\.
961 set expectedresults.3=%CURDIR%\baz\.
962 set expectedresults.4=%CURDIR%\baz\bazbaz
963 set expectedresults.5=%CURDIR%\bazbaz
964 set expectedresults.6=%CURDIR%\foo\.
965 call :SetExpected
966 for /R %%i in (. baz*) do call temp.bat %%i
967 call :ValidateExpected
969 echo With duplicates enumeration
970 set expectedresults=12
971 set expectedresults.1=%CURDIR%\bar\bazbaz
972 set expectedresults.2=%CURDIR%\bar\fred
973 set expectedresults.3=%CURDIR%\baz\bazbaz
974 set expectedresults.4=%CURDIR%\baz\bazbaz
975 set expectedresults.5=%CURDIR%\baz\bazbaz
976 set expectedresults.6=%CURDIR%\baz\fred
977 set expectedresults.7=%CURDIR%\bazbaz
978 set expectedresults.8=%CURDIR%\bazbaz
979 set expectedresults.9=%CURDIR%\bazbaz
980 set expectedresults.10=%CURDIR%\foo\bazbaz
981 set expectedresults.11=%CURDIR%\foo\fred
982 set expectedresults.12=%CURDIR%\fred
983 call :SetExpected
984 for /R %%i in (baz* bazbaz fred ba*) do call temp.bat %%i
985 call :ValidateExpected
987 echo Strip missing wildcards, keep unwildcarded names
988 set expectedresults=6
989 set expectedresults.1=%CURDIR%\bar\jim
990 set expectedresults.2=%CURDIR%\baz\bazbaz
991 set expectedresults.3=%CURDIR%\baz\jim
992 set expectedresults.4=%CURDIR%\bazbaz
993 set expectedresults.5=%CURDIR%\foo\jim
994 set expectedresults.6=%CURDIR%\jim
995 call :SetExpected
996 for /R %%i in (baz* fred* jim) do call temp.bat %%i
997 call :ValidateExpected
999 echo for /R passed
1000 cd .. & rd /s/Q foobar
1001 echo --- for /L
1002 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
1003 for /L %%i in (1,2,0) do echo %%i
1004 for@tab@/L %%i in (1,2,0) do echo %%i
1005 for /L %%i in (1,2,6) do echo %%i
1006 for /l %%i in (1 ,2,6) do echo %%i
1007 for /L %%i in (a,2,3) do echo %%i
1008 for /L %%i in (1,2,-1) do echo %%i
1009 for /L %%i in (-4,-1,-1) do echo %%i
1010 for /L %%i in (1,-2,-2) do echo %%i
1011 for /L %%i in (1,2,a) do echo %%i
1012 echo ErrorLevel %ErrorLevel%
1013 for /L %%i in (1,a,b) do echo %%i
1014 echo ErrorLevel %ErrorLevel%
1015 rem Test boundaries
1016 for /l %%i in (1,1,4) do echo %%i
1017 for /l %%i in (1,2,4) do echo %%i
1018 for /l %%i in (4,-1,1) do echo %%i
1019 for /l %%i in (4,-2,1) do echo %%i
1020 for /l %%i in (1,-1,4) do echo %%i
1021 for /l %%i in (4,1,1) do echo %%i
1022 for /L %%i in (a,2,b) do echo %%i
1023 for /L %%i in (1,1,1) do echo %%i
1024 for /L %%i in (1,-2,-1) do echo %%i
1025 for /L %%i in (-1,-1,-1) do echo %%i
1026 for /L %%i in (1,2, 3) do echo %%i
1027 rem Test zero iteration skips the body of the for
1028 for /L %%i in (2,2,1) do (
1029 echo %%i
1030 echo FAILED
1032 echo --- for /a
1033 rem No output when using "set expr" syntax, unless in interactive mode
1034 rem Need to use "set envvar=expr" to use in a batch script
1035 echo ------ individual operations
1036 set var=0
1037 set /a var=1 +2 & echo %var%
1038 set /a var=1 +-2 & echo %var%
1039 set /a var=1 --2 & echo %var%
1040 set /a var=2* 3 & echo %var%
1041 set /a var=-2* -5 & echo %var%
1042 set /a var=12/3 & echo %var%
1043 set /a var=13/3 & echo %var%
1044 set /a var=-13/3 & echo %var%
1045 rem FIXME Divide by zero should return an error, but error messages cannot be tested with current infrastructure
1046 set /a var=5 %% 5 & echo %var%
1047 set /a var=5 %% 3 & echo %var%
1048 set /a var=5 %% -3 & echo %var%
1049 set /a var=-5 %% -3 & echo %var%
1050 set /a var=1 ^<^< 0 & echo %var%
1051 set /a var=1 ^<^< 2 & echo %var%
1052 set /a var=1 ^<^< -2 & echo %var%
1053 set /a var=-1 ^<^< -2 & echo %var%
1054 set /a var=-1 ^<^< 2 & echo %var%
1055 set /a var=9 ^>^> 0 & echo %var%
1056 set /a var=9 ^>^> 2 & echo %var%
1057 set /a var=9 ^>^> -2 & echo %var%
1058 set /a var=-9 ^>^> -2 & echo %var%
1059 set /a var=-9 ^>^> 2 & echo %var%
1060 set /a var=5 ^& 0 & echo %var%
1061 set /a var=5 ^& 1 & echo %var%
1062 set /a var=5 ^& 3 & echo %var%
1063 set /a var=5 ^& 4 & echo %var%
1064 set /a var=5 ^& 1 & echo %var%
1065 set /a var=5 ^| 0 & echo %var%
1066 set /a var=5 ^| 1 & echo %var%
1067 set /a var=5 ^| 3 & echo %var%
1068 set /a var=5 ^| 4 & echo %var%
1069 set /a var=5 ^| 1 & echo %var%
1070 set /a var=5 ^^ 0 & echo %var%
1071 set /a var=5 ^^ 1 & echo %var%
1072 set /a var=5 ^^ 3 & echo %var%
1073 set /a var=5 ^^ 4 & echo %var%
1074 set /a var=5 ^^ 1 & echo %var%
1075 echo ------ precedence and grouping
1076 set /a var=4 + 2*3 & echo %var%
1077 set /a var=(4+2)*3 & echo %var%
1078 set /a var=4 * 3/5 & echo %var%
1079 set /a var=(4 * 3)/5 & echo %var%
1080 set /a var=4 * 5 %% 4 & echo %var%
1081 set /a var=4 * (5 %% 4) & echo %var%
1082 set /a var=3 %% (5 + 8 %% 3 ^^ 2) & echo %var%
1083 set /a var=3 %% (5 + 8 %% 3 ^^ -2) & echo %var%
1084 echo ------ octal and hexadecimal
1085 set /a var=0xf + 3 & echo %var%
1086 set /a var=0xF + 3 & echo %var%
1087 set /a var=015 + 2 & echo %var%
1088 set /a var=3, 8+3,0 & echo %var%
1089 echo ------ variables
1090 set /a var=foo=3, foo+1 & echo %var%
1091 if defined foo (echo %foo%) else (
1092 echo foo not defined
1094 set /a var=foo=3, foo+=1 & echo %var%
1095 set /a var=foo=3, bar=1, bar+=foo, bar & echo %var%
1096 set /a var=foo*= foo & echo %var%
1097 set /a var=whateverNonExistingVar & echo %var%
1098 set /a var=whateverNonExistingVar + bar & echo %var%
1099 set /a var=foo -= foo + 7 & echo %var%
1100 set /a var=foo /= 3 + 2 & echo %var%
1101 set /a var=foo=5, foo %%=2 & echo %var%
1102 set /a var=foo ^<^<= 2 & echo %var%
1103 set /a var=foo ^>^>= 2 & echo %var%
1104 set /a var=foo ^&= 2 & echo %var%
1105 set /a var=foo=5, foo ^|= 2 & echo %var%
1106 set /a var=foo=5, foo ^^= 2 & echo %var%
1107 set /a var=foo=19, foo %%= 4 + (bar %%= 7) & echo.
1108 set foo=
1109 set bar=
1110 set var=
1111 echo --- for /F
1112 mkdir foobar & cd foobar
1113 echo ------ string argument
1114 rem NT4 does not support usebackq
1115 for /F %%i in ("a b c") do echo %%i
1116 for /f usebackq %%i in ('a b c') do echo %%i>output_file
1117 if not exist output_file (echo no output) else (type output_file & del output_file)
1118 for /f %%i in ("a ") do echo %%i
1119 for /f usebackq %%i in ('a ') do echo %%i>output_file
1120 if not exist output_file (echo no output) else (type output_file & del output_file)
1121 for /f %%i in ("a") do echo %%i
1122 for /f usebackq %%i in ('a') do echo %%i>output_file
1123 if not exist output_file (echo no output) else (type output_file & del output_file)
1124 fOr /f %%i in (" a") do echo %%i
1125 for /f usebackq %%i in (' a') do echo %%i>output_file
1126 if not exist output_file (echo no output) else (type output_file & del output_file)
1127 for /f %%i in (" a ") do echo %%i
1128 for /f usebackq %%i in (' a ') do echo %%i>output_file
1129 if not exist output_file (echo no output) else (type output_file & del output_file)
1130 echo ------ fileset argument
1131 echo --------- basic blank handling
1132 echo a b c>foo
1133 for /f %%i in (foo) do echo %%i
1134 echo a >foo
1135 for /f %%i in (foo) do echo %%i
1136 echo a>foo
1137 for /f %%i in (foo) do echo %%i
1138 echo a>foo
1139 for /f %%i in (foo) do echo %%i
1140 echo a >foo
1141 for /f %%i in (foo) do echo %%i
1142 echo. > foo
1143 for /f %%i in (foo) do echo %%i
1144 echo. >> foo
1145 echo b > foo
1146 for /f %%i in (foo) do echo %%i
1147 echo --------- multi-line with empty lines
1148 echo a Z f> foo
1149 echo. >> foo
1150 echo.>> foo
1151 echo b bC>> foo
1152 echo c>> foo
1153 echo. >> foo
1154 for /f %%b in (foo) do echo %%b
1155 echo --------- multiple files
1156 echo q w > bar
1157 echo.>> bar
1158 echo kkk>>bar
1159 for /f %%k in (foo bar) do echo %%k
1160 for /f %%k in (bar foo) do echo %%k
1161 echo ------ command argument
1162 rem Not implemented on NT4, need to skip it as no way to get output otherwise
1163 if "%CD%"=="" goto :SkipFORFcmdNT4
1164 for /f %%i in ('echo.Passed1') do echo %%i
1165 for /f "usebackq" %%i in (`echo.Passed2`) do echo %%i
1166 for /f usebackq %%i in (`echo.Passed3`) do echo %%i
1167 goto :ContinueFORF
1168 :SkipFORFcmdNT4
1169 for /l %%i in (1,1,3) do echo Missing functionality - Broken%%i
1170 :ContinueFORF
1171 rem FIXME: Rest not testable right now in wine: not implemented and would need
1172 rem preliminary grep-like program implementation (e.g. like findstr or fc) even
1173 rem for a simple todo_wine test
1174 rem (for /f "usebackq" %%i in (`echo z a b`) do echo %%i) || echo not supported
1175 rem (for /f usebackq %%i in (`echo z a b`) do echo %%i) || echo not supported
1176 echo ------ eol option
1177 if "%CD%"=="" goto :SkipFORFeolNT4
1178 echo Line one>foo
1179 echo and Line two>>foo
1180 echo Line three>>foo
1181 for /f "eol=L" %%i in (foo) do echo %%i
1182 for /f "eol=a" %%i in (foo) do echo %%i
1183 del foo
1184 goto :ContinueFORFeol
1185 :SkipFORFeolNT4
1186 for /l %%i in (1,1,3) do echo Broken NT4 functionality%%i
1187 :ContinueFORFeol
1188 for /f "eol=@" %%i in (" ad") do echo %%i
1189 for /f "eol=@" %%i in (" z@y") do echo %%i
1190 for /f "eol=|" %%i in ("a|d") do echo %%i
1191 for /f "eol=@" %%i in ("@y") do echo %%i > output_file
1192 if not exist output_file (echo no output) else (del output_file)
1193 for /f "eol==" %%i in ("=y") do echo %%i > output_file
1194 if not exist output_file (echo no output) else (del output_file)
1195 echo ------ delims option
1196 for /f "delims=|" %%i in ("a|d") do echo %%i
1197 for /f "delims=|" %%i in ("a |d") do echo %%i
1198 for /f "delims=|" %%i in ("a d|") do echo %%i
1199 for /f "delims=| " %%i in ("a d|") do echo %%i
1200 for /f "delims==" %%i in ("C r=d|") do echo %%i
1201 for /f "delims=" %%i in ("foo bar baz") do echo %%i
1202 for /f "delims=" %%i in ("c:\foo bar baz\..") do echo %%~fi
1203 echo ------ skip option
1204 echo a > foo
1205 echo b >> foo
1206 echo c >> foo
1207 for /f "skip=2" %%i in (foo) do echo %%i
1208 for /f "skip=3" %%i in (foo) do echo %%i > output_file
1209 if not exist output_file (echo no output) else (del output_file)
1210 for /f "skip=4" %%i in (foo) do echo %%i > output_file
1211 if not exist output_file (echo no output) else (del output_file)
1212 for /f "skip=02" %%i in (foo) do echo %%i
1213 for /f "skip=0x2" %%i in (foo) do echo %%i
1214 for /f "skip=1" %%i in ("skipme") do echo %%i > output_file
1215 if not exist output_file (echo no output) else (del output_file)
1216 cd ..
1217 rd /s/q foobar
1219 echo ------------ Testing del /a ------------
1220 del /f/q *.test > nul
1221 echo r > r.test
1222 attrib +r r.test
1223 echo not-r > not-r.test
1225 if not exist not-r.test echo not-r.test not found before delete, bad
1226 del /a:-r *.test
1227 if not exist not-r.test echo not-r.test not found after delete, good
1229 if not exist r.test echo r.test not found before delete, bad
1230 if exist r.test echo r.test found before delete, good
1231 del /a:r *.test
1232 if not exist r.test echo r.test not found after delete, good
1233 if exist r.test echo r.test found after delete, bad
1235 echo ------------ Testing del /q ------------
1236 mkdir del_q_dir
1237 cd del_q_dir
1238 echo abc > file1
1239 echo abc > file2.dat
1240 rem If /q doesn't work, cmd will prompt and the test case should hang
1241 del /q * > nul
1242 for %%a in (1 2.dat) do if exist file%%a echo del /q * failed on file%%a
1243 for %%a in (1 2.dat) do if not exist file%%a echo del /q * succeeded on file%%a
1244 cd ..
1245 rmdir del_q_dir
1247 echo ------------ Testing del /s ------------
1248 mkdir "foo bar"
1249 cd "foo bar"
1250 mkdir "foo:"
1251 echo hi > file1.dat
1252 echo there > file2.dat
1253 echo bub > file3.dat
1254 echo bye > "file with spaces.dat"
1255 cd ..
1256 del /s file1.dat > nul
1257 del file2.dat /s > nul
1258 del "file3.dat" /s > nul
1259 del "file with spaces.dat" /s > nul
1260 cd "foo bar"
1261 for %%f in (1 2 3) do if exist file%%f.dat echo Del /s failed on file%%f
1262 for %%f in (1 2 3) do if exist file%%f.dat del file%%f.dat
1263 if exist "file with spaces.dat" echo Del /s failed on "file with spaces.dat"
1264 if exist "file with spaces.dat" del "file with spaces.dat"
1265 rmdir "foo:"
1266 cd ..
1267 rmdir "foo bar"
1269 echo ------------ Testing rename ------------
1270 mkdir foobar & cd foobar
1271 echo --- ren and rename are synonymous
1272 echo > foo
1273 rename foo bar
1274 if exist foo echo foo should be renamed!
1275 if exist bar echo foo renamed to bar
1276 ren bar foo
1277 if exist bar echo bar should be renamed!
1278 if exist foo echo bar renamed to foo
1279 echo --- name collision
1280 echo foo>foo
1281 echo bar>bar
1282 ren foo bar 2> nul
1283 type foo
1284 type bar
1285 rem no-op
1286 ren foo foo
1287 mkdir baz
1288 ren foo baz\abc
1289 echo --- rename read-only files
1290 echo > file1
1291 attrib +r file1
1292 ren file1 file2
1293 if not exist file1 (
1294 if exist file2 (
1295 echo read-only file renamed
1297 ) else (
1298 echo read-only file not renamed!
1300 echo --- rename directories
1301 mkdir rep1
1302 ren rep1 rep2
1303 if not exist rep1 (
1304 if exist rep2 (
1305 echo dir renamed
1308 attrib +r rep2
1309 ren rep2 rep1
1310 if not exist rep2 (
1311 if exist rep1 (
1312 echo read-only dir renamed
1315 echo --- rename in other directory
1316 if not exist baz\abc (
1317 echo rename impossible in other directory
1318 if exist foo echo original file still present
1319 ) else (
1320 echo shouldn't rename in other directory!
1321 if not exist foo echo original file not present anymore
1323 cd .. & rd /s/q foobar
1325 echo ------------ Testing move ------------
1326 mkdir foobar & cd foobar
1327 echo --- file move
1328 echo >foo
1329 move foo bar > nul 2>&1
1330 if not exist foo (
1331 if exist bar (
1332 echo file move succeeded
1335 echo bar>bar
1336 echo baz> baz
1337 move /Y bar baz > nul 2>&1
1338 if not exist bar (
1339 if exist baz (
1340 echo file move with overwrite succeeded
1342 ) else (
1343 echo file overwrite impossible!
1344 del bar
1346 type baz
1348 attrib +r baz
1349 move baz bazro > nul 2>&1
1350 if not exist baz (
1351 if exist bazro (
1352 echo read-only files are moveable
1353 move bazro baz > nul 2>&1
1355 ) else (
1356 echo read-only file not moved!
1358 attrib -r baz
1359 mkdir rep
1360 move baz rep > nul 2>&1
1361 if not exist baz (
1362 if exist rep\baz (
1363 echo file moved in subdirectory
1366 call :setError 0
1367 move rep\baz . > nul 2>&1
1368 move /Y baz baz > nul 2>&1
1369 if errorlevel 1 (
1370 echo moving a file to itself should be a no-op!
1371 ) else (
1372 echo moving a file to itself is a no-op
1374 echo ErrorLevel: %ErrorLevel%
1375 call :setError 0
1376 del baz
1377 echo --- directory move
1378 mkdir foo\bar
1379 mkdir baz
1380 echo baz2>baz\baz2
1381 move baz foo\bar > nul 2>&1
1382 if not exist baz (
1383 if exist foo\bar\baz\baz2 (
1384 echo simple directory move succeeded
1387 call :setError 0
1388 mkdir baz
1389 move baz baz > nul 2>&1
1390 echo moving a directory to itself gives error; errlevel %ErrorLevel%
1391 echo ------ dir in dir move
1392 rd /s/q foo
1393 mkdir foo bar
1394 echo foo2>foo\foo2
1395 echo bar2>bar\bar2
1396 move foo bar > nul 2>&1
1397 if not exist foo (
1398 if exist bar (
1399 dir /b /ad bar
1400 dir /b /a-d bar
1401 dir /b bar\foo
1404 cd .. & rd /s/q foobar
1406 echo ------------ Testing mkdir ------------
1407 call :setError 0
1408 echo --- md and mkdir are synonymous
1409 mkdir foobar
1410 echo %ErrorLevel%
1411 rmdir foobar
1412 md foobar
1413 echo %ErrorLevel%
1414 rmdir foobar
1415 echo --- creating an already existing directory/file must fail
1416 mkdir foobar
1417 md foobar
1418 echo %ErrorLevel%
1419 rmdir foobar
1420 echo > foobar
1421 mkdir foobar
1422 echo %ErrorLevel%
1423 del foobar
1424 echo --- multilevel path creation
1425 mkdir foo
1426 echo %ErrorLevel%
1427 mkdir foo\bar\baz
1428 echo %ErrorLevel%
1429 cd foo
1430 echo %ErrorLevel%
1431 cd bar
1432 echo %ErrorLevel%
1433 cd baz
1434 echo %ErrorLevel%
1435 echo > ..\..\bar2
1436 mkdir ..\..\..\foo\bar2
1437 echo %ErrorLevel%
1438 del ..\..\bar2
1439 mkdir ..\..\..\foo\bar2
1440 echo %ErrorLevel%
1441 rmdir ..\..\..\foo\bar2
1442 cd ..
1443 rmdir baz
1444 cd ..
1445 rmdir bar
1446 cd ..
1447 rmdir foo
1448 echo %ErrorLevel%
1449 echo --- trailing backslashes
1450 mkdir foo\\\\
1451 echo %ErrorLevel%
1452 if exist foo (rmdir foo & echo dir created
1453 ) else ( echo dir not created )
1454 echo %ErrorLevel%
1455 echo --- invalid chars
1456 mkdir ?
1457 echo mkdir ? gives errorlevel %ErrorLevel%
1458 call :setError 0
1459 mkdir ?\foo
1460 echo mkdir ?\foo gives errorlevel %ErrorLevel%
1461 call :setError 0
1462 mkdir foo\?
1463 echo mkdir foo\? gives errorlevel %ErrorLevel%
1464 if exist foo (rmdir foo & echo ok, foo created
1465 ) else ( echo foo not created )
1466 call :setError 0
1467 mkdir foo\bar\?
1468 echo mkdir foo\bar\? gives errorlevel %ErrorLevel%
1469 call :setError 0
1470 if not exist foo (
1471 echo bad, foo not created
1472 ) else (
1473 cd foo
1474 if exist bar (
1475 echo ok, foo\bar created
1476 rmdir bar
1478 cd ..
1479 rmdir foo
1481 echo --- multiple directories at once
1482 mkdir foobaz & cd foobaz
1483 mkdir foo bar\baz foobar "bazbaz" .\"zabzab"
1484 if exist foo (echo foo created) else echo foo not created!
1485 if exist bar (echo bar created) else echo bar not created!
1486 if exist foobar (echo foobar created) else echo foobar not created!
1487 if exist bar\baz (echo bar\baz created) else echo bar\baz not created!
1488 if exist bazbaz (echo bazbaz created) else echo bazbaz not created!
1489 if exist zabzab (echo zabzab created) else echo zabzab not created!
1490 cd .. & rd /s/q foobaz
1491 call :setError 0
1492 mkdir foo\*
1493 echo mkdir foo\* errorlevel %ErrorLevel%
1494 if exist foo (rmdir foo & echo ok, foo created
1495 ) else ( echo bad, foo not created )
1497 echo ------------ Testing rmdir ------------
1498 call :setError 0
1499 rem rd and rmdir are synonymous
1500 mkdir foobar
1501 rmdir foobar
1502 echo %ErrorLevel%
1503 if not exist foobar echo dir removed
1504 mkdir foobar
1505 rd foobar
1506 echo %ErrorLevel%
1507 if not exist foobar echo dir removed
1508 rem Removing nonexistent directory
1509 rmdir foobar
1510 echo %ErrorLevel%
1511 rem Removing single-level directories
1512 echo > foo
1513 rmdir foo
1514 echo %ErrorLevel%
1515 if exist foo echo file not removed
1516 del foo
1517 mkdir foo
1518 echo > foo\bar
1519 rmdir foo
1520 echo %ErrorLevel%
1521 if exist foo echo non-empty dir not removed
1522 del foo\bar
1523 mkdir foo\bar
1524 rmdir foo
1525 echo %ErrorLevel%
1526 if exist foo echo non-empty dir not removed
1527 rmdir foo\bar
1528 rmdir foo
1529 rem Recursive rmdir
1530 mkdir foo\bar\baz
1531 rmdir /s /Q foo
1532 if not exist foo (
1533 echo recursive rmdir succeeded
1534 ) else (
1535 rd foo\bar\baz
1536 rd foo\bar
1537 rd foo
1539 mkdir foo\bar\baz
1540 echo foo > foo\bar\brol
1541 rmdir /s /Q foo 2>&1
1542 if not exist foo (
1543 echo recursive rmdir succeeded
1544 ) else (
1545 rd foo\bar\baz
1546 del foo\bar\brol
1547 rd foo\bar
1548 rd foo
1550 rem multiples directories at once
1551 mkdir foobaz & cd foobaz
1552 mkdir foo
1553 mkdir bar\baz
1554 mkdir foobar
1555 rd /s/q foo bar foobar
1556 if not exist foo (echo foo removed) else echo foo not removed!
1557 if not exist bar (echo bar removed) else echo bar not removed!
1558 if not exist foobar (echo foobar removed) else echo foobar not removed!
1559 if not exist bar\baz (echo bar\baz removed) else echo bar\baz not removed!
1560 cd .. & rd /s/q foobaz
1562 echo ------------ Testing pushd/popd ------------
1564 echo --- popd is no-op when dir stack is empty
1565 popd
1567 echo --- pushing non-existing dir
1568 pushd foobar
1570 echo --- basic behaviour
1571 mkdir foobar\baz
1572 pushd foobar
1574 popd
1576 pushd foobar
1577 pushd baz
1579 popd
1581 pushd baz
1582 popd
1584 popd
1586 pushd .
1587 cd foobar\baz
1588 pushd ..
1590 popd
1591 popd
1593 rd /s/q foobar
1595 echo ------------ Testing attrib ------------
1596 rem FIXME Add tests for archive, hidden and system attributes + mixed attributes modifications
1597 mkdir foobar & cd foobar
1598 echo foo original contents> foo
1599 attrib foo
1600 echo > bar
1601 echo --- read-only attribute
1602 rem Read-only files cannot be altered or deleted, unless forced
1603 attrib +R foo
1604 attrib foo
1605 dir /Ar /B
1606 echo bar>> foo
1607 type foo
1608 del foo > NUL 2>&1
1609 if exist foo (
1610 echo Read-only file not deleted
1611 ) else (
1612 echo Should not delete read-only file!
1614 del /F foo
1615 if not exist foo (
1616 echo Read-only file forcibly deleted
1617 ) else (
1618 echo Should delete read-only file with del /F!
1619 attrib -r foo
1620 del foo
1622 cd .. & rd /s/q foobar
1623 echo --- recursive behaviour
1624 mkdir foobar\baz & cd foobar
1625 echo > level1
1626 echo > whatever
1627 echo > baz\level2
1628 attrib baz\level2
1629 cd ..
1630 attrib +R l*vel? /S > nul 2>&1
1631 cd foobar
1632 attrib level1
1633 attrib baz\level2
1634 echo > bar
1635 attrib bar
1636 cd .. & rd /s/q foobar
1637 echo --- folders processing
1638 mkdir foobar
1639 attrib foobar
1640 cd foobar
1641 mkdir baz
1642 echo toto> baz\toto
1643 attrib +r baz /s /d > nul 2>&1
1644 attrib baz
1645 attrib baz\toto
1646 echo lulu>>baz\toto
1647 type baz\toto
1648 echo > baz\lala
1649 rem Oddly windows allows file creation in a read-only directory...
1650 if exist baz\lala (echo file created in read-only dir) else echo file not created
1651 cd .. & rd /s/q foobar
1653 echo ------------ Testing assoc ------------
1654 rem FIXME Can't test error messages in the current test system, so we have to use some kludges
1655 rem FIXME Revise once || conditional execution is fixed
1656 mkdir foobar & cd foobar
1657 echo --- setting association
1658 assoc .foo > baz
1659 type baz
1660 echo ---
1662 assoc .foo=bar
1663 assoc .foo
1665 rem association set system-wide
1666 echo @echo off> tmp.cmd
1667 echo echo +++>> tmp.cmd
1668 echo assoc .foo>> tmp.cmd
1669 cmd /c tmp.cmd
1671 echo --- resetting association
1672 assoc .foo=
1673 assoc .foo > baz
1674 type baz
1675 echo ---
1677 rem association removal set system-wide
1678 cmd /c tmp.cmd > baz
1679 type baz
1680 echo ---
1681 cd .. & rd /s/q foobar
1683 echo ------------ Testing ftype ------------
1684 rem FIXME Can't test error messages in the current test system, so we have to use some kludges
1685 rem FIXME Revise once || conditional execution is fixed
1686 mkdir foobar & cd foobar
1687 echo --- setting association
1688 ftype footype> baz
1689 type baz
1690 echo ---
1692 ftype footype=foo_opencmd
1693 assoc .foo=footype
1694 ftype footype
1696 rem association set system-wide
1697 echo @echo off> tmp.cmd
1698 echo echo +++>> tmp.cmd
1699 echo ftype footype>> tmp.cmd
1700 cmd /c tmp.cmd
1702 echo --- resetting association
1703 assoc .foo=
1705 rem Removing a file type association doesn't work on XP due to a bug, so a workaround is needed
1706 setlocal EnableDelayedExpansion
1707 set FOO=original value
1708 ftype footype=
1709 ftype footype > baz
1710 for /F %%i in ('type baz') do (set FOO=buggyXP)
1711 rem Resetting actually works on wine/NT4, but is reported as failing due to the peculiar test (and non-support for EnabledDelayedExpansion)
1712 rem FIXME Revisit once a grep-like program like ftype is implemented
1713 rem (e.g. to check baz's size using dir /b instead)
1714 echo !FOO!
1716 rem cleanup registry
1717 echo REGEDIT4> regCleanup.reg
1718 echo.>> regCleanup.reg
1719 echo [-HKEY_CLASSES_ROOT\footype]>> regCleanup.reg
1720 regedit /s regCleanup.reg
1721 set FOO=
1722 endlocal
1723 cd .. & rd /s/q foobar
1725 echo ------------ Testing CALL ------------
1726 mkdir foobar & cd foobar
1727 echo --- external script
1728 echo echo foo %%1> foo.cmd
1729 call foo
1730 call foo.cmd 8
1731 echo echo %%1 %%2 > foo.cmd
1732 call foo.cmd foo
1733 call foo.cmd foo bar
1734 call foo.cmd foo ""
1735 call foo.cmd "" bar
1736 call foo.cmd foo ''
1737 call foo.cmd '' bar
1738 del foo.cmd
1740 echo --- internal routines
1741 call :testRoutine :testRoutine
1742 goto :endTestRoutine
1743 :testRoutine
1744 echo bar %1
1745 goto :eof
1746 :endTestRoutine
1748 call :testRoutineArgs foo
1749 call :testRoutineArgs foo bar
1750 call :testRoutineArgs foo ""
1751 call :testRoutineArgs "" bar
1752 call :testRoutineArgs foo ''
1753 call :testRoutineArgs '' bar
1754 goto :endTestRoutineArgs
1755 :testRoutineArgs
1756 echo %1 %2
1757 goto :eof
1758 :endTestRoutineArgs
1760 echo --- with builtins
1761 call mkdir foo
1762 echo %ErrorLevel%
1763 if exist foo (echo foo created) else echo foo should exist!
1764 rmdir foo
1765 set FOOBAZ_VAR=foobaz
1766 call echo Should expand %FOOBAZ_VAR%
1767 set FOOBAZ_VAR=
1768 echo>batfile
1769 call dir /b
1770 echo>robinfile
1771 if 1==1 call del batfile
1772 dir /b
1773 if exist batfile echo batfile shouldn't exist
1774 rem ... but not for 'if' or 'for'
1775 call if 1==1 echo bar 2> nul
1776 echo %ErrorLevel%
1777 call :setError 0
1778 call for %%i in (foo bar baz) do echo %%i 2> nul
1779 echo %ErrorLevel%
1780 rem First look for programs in the path before trying a builtin
1781 echo echo non-builtin dir> dir.cmd
1782 call dir /b
1783 del dir.cmd
1784 rem The below line equates to call (, which does nothing, then the
1785 rem subsequent lines are executed.
1786 call (
1787 echo Line one
1788 echo Line two
1790 rem The below line equates to call if, which always fails, then the
1791 rem subsequent lines are executed. Note cmd.exe swallows all lines
1792 rem starting with )
1793 call if 1==1 (
1794 echo Get if
1795 ) else (
1796 echo ... and else!
1798 call call call echo passed
1799 cd .. & rd /s/q foobar
1801 echo ------------ Testing SHIFT ------------
1803 call :shiftFun p1 p2 p3 p4 p5
1804 goto :endShiftFun
1806 :shiftFun
1807 echo '%1' '%2' '%3' '%4' '%5'
1808 shift
1809 echo '%1' '%2' '%3' '%4' '%5'
1810 shift@tab@ /1
1811 echo '%1' '%2' '%3' '%4' '%5'
1812 shift /2
1813 echo '%1' '%2' '%3' '%4' '%5'
1814 shift /-1
1815 echo '%1' '%2' '%3' '%4' '%5'
1816 shift /0
1817 echo '%1' '%2' '%3' '%4' '%5'
1818 goto :eof
1819 :endShiftFun
1821 echo ------------ Testing cmd invocation ------------
1822 rem FIXME: only a stub ATM
1823 echo --- a batch file can delete itself
1824 echo del foo.cmd>foo.cmd
1825 cmd /q /c foo.cmd
1826 if not exist foo.cmd (
1827 echo file correctly deleted
1828 ) else (
1829 echo file should be deleted!
1830 del foo.cmd
1832 echo --- a batch file can alter itself
1833 echo echo bar^>foo.cmd>foo.cmd
1834 cmd /q /c foo.cmd > NUL 2>&1
1835 if exist foo.cmd (
1836 type foo.cmd
1837 del foo.cmd
1838 ) else (
1839 echo file not created!
1842 echo ---------- Testing copy
1843 md foobar2
1844 cd foobar2
1845 rem Note echo adds 0x0d 0x0a on the end of the line in the file
1846 echo AAA> file1
1847 echo BBBBBB> file2
1848 echo CCCCCCCCC> file3
1849 md dir1
1850 goto :testcopy
1852 :CheckExist
1853 if exist "%1" (
1854 echo Passed: Found expected %1
1855 ) else (
1856 echo Failed: Did not find expected %1
1858 del /q "%1" >nul 2>&1
1859 shift
1860 if not "%1"=="" goto :CheckExist
1861 goto :eof
1863 :CheckNotExist
1864 if not exist "%1" (
1865 echo Passed: Did not find %1
1866 ) else (
1867 echo Failed: Unexpectedly found %1
1868 del /q "%1" >nul 2>&1
1870 shift
1871 if not "%1"=="" goto :CheckNotExist
1872 goto :eof
1874 rem Note: No way to check file size on NT4 so skip the test
1875 :CheckFileSize
1876 if not exist "%1" (
1877 echo Failed: File missing when requested filesize check [%2]
1878 goto :ContinueFileSizeChecks
1880 for %%i in (%1) do set filesize=%%~zi
1881 if "%filesize%"=="%2" (
1882 echo Passed: file size check on %1 [%filesize%]
1883 ) else (
1884 if "%filesize%"=="%%~zi" (
1885 echo Skipping file size check on NT4
1886 ) else (
1887 echo Failed: file size check on %1 [%filesize% != %2]
1890 :ContinueFileSizeChecks
1891 shift
1892 shift
1893 if not "%1"=="" goto :CheckFileSize
1894 goto :eof
1896 :testcopy
1898 rem -----------------------
1899 rem Simple single file copy
1900 rem -----------------------
1901 rem Simple single file copy, normally used syntax
1902 copy file1 dummy.file >nul 2>&1
1903 if errorlevel 1 echo Incorrect errorlevel
1904 call :CheckExist dummy.file
1906 rem Simple single file copy, destination supplied as two forms of directory
1907 copy file1 dir1 >nul 2>&1
1908 if errorlevel 1 echo Incorrect errorlevel
1909 call :CheckExist dir1\file1
1911 copy file1 dir1\ >nul 2>&1
1912 if errorlevel 1 echo Incorrect errorlevel
1913 call :CheckExist dir1\file1
1915 rem Simple single file copy, destination supplied as fully qualified destination
1916 copy file1 dir1\file99 >nul 2>&1
1917 if errorlevel 1 echo Incorrect errorlevel
1918 call :CheckExist dir1\file99
1920 rem Simple single file copy, destination not supplied
1921 cd dir1
1922 copy ..\file1 >nul 2>&1
1923 if errorlevel 1 echo Incorrect errorlevel
1924 call :CheckExist file1
1925 cd ..
1927 rem Simple single file copy, destination supplied as nonexistent directory
1928 copy file1 dir2\ >nul 2>&1
1929 if not errorlevel 1 echo Incorrect errorlevel
1930 call :CheckNotExist dir2 dir2\file1
1932 rem -----------------------
1933 rem Wildcarded copy
1934 rem -----------------------
1935 rem Simple single file copy, destination supplied as two forms of directory
1936 copy file? dir1 >nul 2>&1
1937 if errorlevel 1 echo Incorrect errorlevel
1938 call :CheckExist dir1\file1 dir1\file2 dir1\file3
1940 copy file* dir1\ >nul 2>&1
1941 if errorlevel 1 echo Incorrect errorlevel
1942 call :CheckExist dir1\file1 dir1\file2 dir1\file3
1944 rem Simple single file copy, destination not supplied
1945 cd dir1
1946 copy ..\file*.* >nul 2>&1
1947 if errorlevel 1 echo Incorrect errorlevel
1948 call :CheckExist file1 file2 file3
1949 cd ..
1951 rem Simple wildcarded file copy, destination supplied as nonexistent directory
1952 copy file? dir2\ >nul 2>&1
1953 if not errorlevel 1 echo Incorrect errorlevel
1954 call :CheckNotExist dir2 dir2\file1 dir2\file2 dir2\file3
1956 rem ------------------------------------------------
1957 rem Confirm overwrite works (cannot test prompting!)
1958 rem ------------------------------------------------
1959 copy file1 testfile >nul 2>&1
1960 copy /y file2 testfile >nul 2>&1
1961 call :CheckExist testfile
1963 rem ------------------------------------------------
1964 rem Test concatenation
1965 rem ------------------------------------------------
1966 rem simple case, no wildcards
1967 copy file1+file2 testfile >nul 2>&1
1968 if errorlevel 1 echo Incorrect errorlevel
1969 call :CheckExist testfile
1971 rem simple case, wildcards, no concatenation
1972 copy file* testfile >nul 2>&1
1973 if errorlevel 1 echo Incorrect errorlevel
1974 call :CheckExist testfile
1976 rem simple case, wildcards, and concatenation
1977 echo ddddd > fred
1978 copy file*+fred testfile >nul 2>&1
1979 if errorlevel 1 echo Incorrect errorlevel
1980 call :CheckExist testfile
1982 rem simple case, wildcards, and concatenation
1983 copy fred+file* testfile >nul 2>&1
1984 if errorlevel 1 echo Incorrect errorlevel
1985 call :CheckExist testfile
1987 rem Calculate destination name
1988 copy fred+file* dir1 >nul 2>&1
1989 if errorlevel 1 echo Incorrect errorlevel
1990 call :CheckExist dir1\fred
1992 rem Calculate destination name
1993 copy fred+file* dir1\ >nul 2>&1
1994 if errorlevel 1 echo Incorrect errorlevel
1995 call :CheckExist dir1\fred
1997 rem Calculate destination name (none supplied)
1998 cd dir1
1999 copy ..\fred+..\file* >nul 2>&1
2000 if errorlevel 1 echo Incorrect errorlevel
2001 call :CheckExist fred
2003 copy ..\fr*+..\file1 >nul 2>&1
2004 if errorlevel 1 echo Incorrect errorlevel
2005 call :CheckExist fred
2006 cd ..
2008 rem ******************************************************************
2009 rem ASCII and BINARY tests
2010 rem Note: hard coded numbers deliberate because need to ensure whether
2011 rem an additional EOF has been added or not. There is no way to handle
2012 rem EOFs in batch, so assume if a single byte appears, its an EOF!
2013 rem ******************************************************************
2015 rem Confirm original sizes of file1,2,3
2016 call :CheckFileSize file1 5 file2 8 file3 11
2018 cd dir1
2020 rem ----------------------------------------------
2021 rem Show concatenation defaults copy to ascii mode
2022 rem ----------------------------------------------
2023 rem Simple default copy source to destination (should not append EOF 5)
2024 copy ..\file1 file1_default >nul 2>&1
2025 call :CheckFileSize file1_default 5
2027 rem Simple binary copy source to destination (should not append EOF 5)
2028 copy /b ..\file1 file1_default2 >nul 2>&1
2029 call :CheckFileSize file1_default2 5
2031 rem Simple ascii copy source to destination (should append EOF 5+1, 8+1, 11+1)
2032 copy /a ..\file1 file1_plus_eof >nul 2>&1
2033 call :CheckFileSize file1_plus_eof 6
2034 copy /a ..\file2 file2_plus_eof >nul 2>&1
2035 call :CheckFileSize file2_plus_eof 9
2036 copy /a ..\file3 file3_plus_eof >nul 2>&1
2037 call :CheckFileSize file3_plus_eof 12
2039 rem Concat 2 files, ascii mode - (only one EOF on the end 5+8+1)
2040 copy /a ..\file1+..\file2 file12_plus_eof >nul 2>&1
2041 call :CheckFileSize file12_plus_eof 14
2043 rem Concat 2 files, binary mode - (no EOF on the end 5+8)
2044 copy /b ..\file1+..\file2 file12_no_eof >nul 2>&1
2045 call :CheckFileSize file12_no_eof 13
2047 rem Concat 2 files, default mode - (one EOF on the end 5+8+1)
2048 copy ..\file1+..\file2 file12_eof2 >nul 2>&1
2049 call :CheckFileSize file12_eof2 14
2051 rem --------------------------------------------------------------
2052 rem Show ascii source copy stops at first EOF, binary does the lot
2053 rem --------------------------------------------------------------
2054 copy file1_plus_eof /b file1_binary_srccopy /b >nul 2>&1
2055 call :CheckFileSize file1_binary_srccopy 6
2057 copy file1_plus_eof /a file1_ascii_srccopy /b >nul 2>&1
2058 call :CheckFileSize file1_ascii_srccopy 5
2060 rem --------------------------------------------------------------
2061 rem Show results of concatenating files (ending in EOFs) and /a /b
2062 rem --------------------------------------------------------------
2064 rem Default and ascii copy reads as ascii, stripping EOFs, so 6-1 + 9-1 + 12-1 + 1
2065 copy file1_plus_eof+file2_plus_eof+file3_plus_eof file123_default_copy >nul 2>&1
2066 call :CheckFileSize file123_default_copy 25
2067 copy /a file1_plus_eof+file2_plus_eof+file3_plus_eof file123_ascii_copy >nul 2>&1
2068 call :CheckFileSize file123_ascii_copy 25
2070 rem In binary mode, we get 3 eofs, so 6 + 9 + 12 = 27
2071 copy /b file1_plus_eof + file2_plus_eof + file3_plus_eof file123_binary_copy >nul 2>&1
2072 call :CheckFileSize file123_binary_copy 27
2074 rem We can select which we want the eofs from by postfixing it with /a or /b
2075 rem so here have first and third with eof, second as ascii 6 + 9-1 + 12
2076 copy file1_plus_eof /b + file2_plus_eof /a + file3_plus_eof /b file123_mixed_copy1 >nul 2>&1
2077 call :CheckFileSize file123_mixed_copy1 26
2079 rem By postfixing the destination with /a, we ask for an ascii destination which appends EOF
2080 rem so here have first and third with eof, second as ascii 6 + 9-1 + 12 + extra EOF
2081 rem Note the delta between this and the previous one also shows that the destination
2082 rem ascii/binary is inherited from the last /a or /b on the line
2083 copy file1_plus_eof /b + file2_plus_eof /a + file3_plus_eof /b file123_mixed_copy2 /a >nul 2>&1
2084 call :CheckFileSize file123_mixed_copy2 27
2086 rem so here have second with eof, first and third as ascii 6-1 + 9 + 12-1
2087 rem Note the delta between the next two also shows that the destination ascii/binary is
2088 rem inherited from the last /a or /b on the line, so the first has an extra EOF
2089 copy file1_plus_eof /a + file2_plus_eof /b + file3_plus_eof /a file123_mixed_copy3 >nul 2>&1
2090 call :CheckFileSize file123_mixed_copy3 26
2091 copy file1_plus_eof /a + file2_plus_eof /b + file3_plus_eof /a file123_mixed_copy4 /b >nul 2>&1
2092 call :CheckFileSize file123_mixed_copy4 25
2094 rem -------------------------------------------------------------------------------------------
2095 rem This shows when concatenating, an ascii destination always adds on an EOF but when we
2096 rem are not concatenating, its a direct copy regardless of destination if being read as binary
2097 rem -------------------------------------------------------------------------------------------
2099 rem All 3 have eof's, plus an extra = 6 + 9 + 12 + eof
2100 copy /b file1_plus_eof + file2_plus_eof + file3_plus_eof file123_mixed_copy5 /a >nul 2>&1
2101 call :CheckFileSize file123_mixed_copy5 28
2103 rem All 2 have eof's, plus an extra = 6 + 12 + eof
2104 copy /b file1_plus_eof + file3_plus_eof file123_mixed_copy6 /a >nul 2>&1
2105 call :CheckFileSize file123_mixed_copy6 19
2107 rem One file has EOF, but doesn't get an extra one, i.e. 6
2108 copy /b file1_plus_eof file123_mixed_copy7 /a >nul 2>&1
2109 call :CheckFileSize file123_mixed_copy7 6
2111 rem Syntax means concatenate so ascii destination kicks in
2112 copy /b file1_plus_eof* file123_mixed_copy8 /a >nul 2>&1
2113 call :CheckFileSize file123_mixed_copy8 7
2115 del *.* /q
2116 cd ..
2118 rem ---------------------------------------
2119 rem Error combinations
2120 rem ---------------------------------------
2121 rem Specify source directory but name is a file
2122 call :setError 0
2123 copy file1\ dir1\ >NUL 2>&1
2124 if errorlevel 1 echo Passed: errorlevel invalid check 1
2125 if not errorlevel 1 echo Failed: errorlevel invalid check 1
2126 call :CheckNotExist dir1\file1
2128 rem Overwrite same file
2129 call :setError 0
2130 copy file1 file1 >NUL 2>&1
2131 if errorlevel 1 echo Passed: errorlevel invalid check 2
2132 if not errorlevel 1 echo Failed: errorlevel invalid check 2
2134 rem Supply same file identified as a directory
2135 call :setError 0
2136 copy file1 file1\ >NUL 2>&1
2137 if errorlevel 1 echo Passed: errorlevel invalid check 3
2138 if not errorlevel 1 echo Failed: errorlevel invalid check 3
2140 cd ..
2141 rd foobar2 /s /q
2143 echo ------------ Testing setlocal/endlocal ------------
2144 call :setError 0
2145 rem Note: setlocal EnableDelayedExpansion already tested in the variable delayed expansion test section
2146 mkdir foobar & cd foobar
2147 echo --- enable/disable extensions
2148 setlocal DisableEXTensions
2149 echo ErrLev: %ErrorLevel%
2150 endlocal
2151 echo ErrLev: %ErrorLevel%
2152 echo @echo off> tmp.cmd
2153 echo echo ErrLev: %%ErrorLevel%%>> tmp.cmd
2154 rem Enabled by default
2155 cmd /C tmp.cmd
2156 cmd /E:OfF /C tmp.cmd
2157 cmd /e:oN /C tmp.cmd
2159 rem FIXME: creating file before setting envvar value to prevent parsing-time evaluation (due to EnableDelayedExpansion not being implemented/available yet)
2160 echo --- setlocal with corresponding endlocal
2161 rem %CD% does not tork on NT4 so use the following workaround
2162 for /d %%i in (.) do set CURDIR=%%~dpnxi
2163 echo @echo off> test.cmd
2164 echo echo %%VAR%%>> test.cmd
2165 echo setlocal>> test.cmd
2166 echo set VAR=localval>> test.cmd
2167 echo md foobar2>> test.cmd
2168 echo cd foobar2>> test.cmd
2169 echo echo %%VAR%%>> test.cmd
2170 echo for /d %%%%i in (.) do echo %%%%~dpnxi>> test.cmd
2171 echo endlocal>> test.cmd
2172 echo echo %%VAR%%>> test.cmd
2173 echo for /d %%%%i in (.) do echo %%%%~dpnxi>> test.cmd
2174 set VAR=globalval
2175 call test.cmd
2176 echo %VAR%
2177 for /d %%i in (.) do echo %%~dpnxi
2178 cd /d %curdir%
2179 rd foobar2
2180 set VAR=
2181 echo --- setlocal with no corresponding endlocal
2182 echo @echo off> test.cmd
2183 echo echo %%VAR%%>> test.cmd
2184 echo setlocal>> test.cmd
2185 echo set VAR=localval>> test.cmd
2186 echo md foobar2>> test.cmd
2187 echo cd foobar2>> test.cmd
2188 echo echo %%VAR%%>> test.cmd
2189 echo for /d %%%%i in (.) do echo %%%%~dpnxi>> test.cmd
2190 set VAR=globalval
2191 rem %CD% does not tork on NT4 so use the following workaround
2192 for /d %%i in (.) do set CURDIR=%%~dpnxi
2193 call test.cmd
2194 echo %VAR%
2195 for /d %%i in (.) do echo %%~dpnxi
2196 cd /d %curdir%
2197 rd foobar2
2198 set VAR=
2199 echo --- setlocal within same batch program
2200 set var1=one
2201 set var2=
2202 set var3=
2203 rem %CD% does not tork on NT4 so use the following workaround
2204 for /d %%i in (.) do set CURDIR=%%~dpnxi
2205 setlocal
2206 set var2=two
2207 mkdir foobar2
2208 cd foobar2
2209 setlocal
2210 set var3=three
2211 if "%var1%"=="one" echo Var1 ok 1
2212 if "%var2%"=="two" echo Var2 ok 2
2213 if "%var3%"=="three" echo Var3 ok 3
2214 for /d %%i in (.) do set curdir2=%%~dpnxi
2215 if "%curdir2%"=="%curdir%\foobar2" echo Directory is ok 1
2216 endlocal
2217 if "%var1%"=="one" echo Var1 ok 1
2218 if "%var2%"=="two" echo Var2 ok 2
2219 if "%var3%"=="" echo Var3 ok 3
2220 for /d %%i in (.) do set curdir2=%%~dpnxi
2221 if "%curdir2%"=="%curdir%\foobar2" echo Directory is ok 2
2222 endlocal
2223 if "%var1%"=="one" echo Var1 ok 1
2224 if "%var2%"=="" echo Var2 ok 2
2225 if "%var3%"=="" echo Var3 ok 3
2226 for /d %%i in (.) do set curdir2=%%~dpnxi
2227 if "%curdir2%"=="%curdir%" echo Directory is ok 3
2228 rd foobar2 /s /q
2229 set var1=
2231 echo --- Mismatched set and end locals
2232 mkdir foodir2 2>nul
2233 mkdir foodir3 2>nul
2234 mkdir foodir4 2>nul
2235 rem %CD% does not tork on NT4 so use the following workaround
2236 for /d %%i in (.) do set curdir=%%~dpnxi
2238 echo @echo off> 2set1end.cmd
2239 echo echo %%VAR%%>> 2set1end.cmd
2240 echo setlocal>> 2set1end.cmd
2241 echo set VAR=2set1endvalue1>> 2set1end.cmd
2242 echo cd ..\foodir3>> 2set1end.cmd
2243 echo setlocal>> 2set1end.cmd
2244 echo set VAR=2set1endvalue2>> 2set1end.cmd
2245 echo cd ..\foodir4>> 2set1end.cmd
2246 echo endlocal>> 2set1end.cmd
2247 echo echo %%VAR%%>> 2set1end.cmd
2248 echo for /d %%%%i in (.) do echo %%%%~dpnxi>> 2set1end.cmd
2250 echo @echo off> 1set2end.cmd
2251 echo echo %%VAR%%>> 1set2end.cmd
2252 echo setlocal>> 1set2end.cmd
2253 echo set VAR=1set2endvalue1>> 1set2end.cmd
2254 echo cd ..\foodir3>> 1set2end.cmd
2255 echo endlocal>> 1set2end.cmd
2256 echo echo %%VAR%%>> 1set2end.cmd
2257 echo for /d %%%%i in (.) do echo %%%%~dpnxi>> 1set2end.cmd
2258 echo endlocal>> 1set2end.cmd
2259 echo echo %%VAR%%>> 1set2end.cmd
2260 echo for /d %%%%i in (.) do echo %%%%~dpnxi>> 1set2end.cmd
2262 echo --- Extra setlocal in called batch
2263 set VAR=value1
2264 rem -- setlocal1 == this batch, should never be used inside a called routine
2265 setlocal
2266 set var=value2
2267 cd foodir2
2268 call %curdir%\2set1end.cmd
2269 echo Finished:
2270 echo %VAR%
2271 for /d %%i in (.) do echo %%~dpnxi
2272 endlocal
2273 echo %VAR%
2274 for /d %%i in (.) do echo %%~dpnxi
2275 cd /d %curdir%
2277 echo --- Extra endlocal in called batch
2278 set VAR=value1
2279 rem -- setlocal1 == this batch, should never be used inside a called routine
2280 setlocal
2281 set var=value2
2282 cd foodir2
2283 call %curdir%\1set2end.cmd
2284 echo Finished:
2285 echo %VAR%
2286 for /d %%i in (.) do echo %%~dpnxi
2287 endlocal
2288 echo %VAR%
2289 for /d %%i in (.) do echo %%~dpnxi
2290 cd /d %curdir%
2292 echo --- endlocal in called function rather than batch pgm is ineffective
2293 @echo off
2294 set var=1
2295 set var2=1
2296 setlocal
2297 set var=2
2298 call :endlocalroutine
2299 echo %var%
2300 endlocal
2301 echo %var%
2302 goto :endlocalfinished
2303 :endlocalroutine
2304 echo %var%
2305 endlocal
2306 echo %var%
2307 setlocal
2308 set var2=2
2309 endlocal
2310 echo %var2%
2311 endlocal
2312 echo %var%
2313 echo %var2%
2314 goto :eof
2315 :endlocalfinished
2316 echo %var%
2318 set var=
2319 set var2=
2320 cd .. & rd /q/s foobar
2322 echo ------------ Testing Errorlevel ------------
2323 rem WARNING: Do *not* add tests using ErrorLevel after this section
2324 should_not_exist 2> nul > nul
2325 echo %ErrorLevel%
2326 rem nt 4.0 doesn't really support a way of setting errorlevel, so this is weak
2327 rem See http://www.robvanderwoude.com/exit.php
2328 call :setError 1
2329 echo %ErrorLevel%
2330 if errorlevel 2 echo errorlevel too high, bad
2331 if errorlevel 1 echo errorlevel just right, good
2332 if errorlevel 01 echo errorlevel with leading zero just right, good
2333 if errorlevel -1 echo errorlevel with negative number OK
2334 if errorlevel 0x1 echo hexa should not be recognized!
2335 if errorlevel 1a echo invalid error level recognized!
2336 call :setError 0
2337 echo abc%ErrorLevel%def
2338 if errorlevel 1 echo errorlevel nonzero, bad
2339 if not errorlevel 1 echo errorlevel zero, good
2340 if not errorlevel 0x1 echo hexa should not be recognized!
2341 if not errorlevel 1a echo invalid error level recognized!
2342 rem Now verify that setting a real variable hides its magic variable
2343 set errorlevel=7
2344 echo %ErrorLevel% should be 7
2345 if errorlevel 7 echo setting var worked too well, bad
2346 call :setError 3
2347 echo %ErrorLevel% should still be 7
2349 echo ------------ Testing GOTO ------------
2350 if a==a goto dest1
2351 :dest1
2352 echo goto with no leading space worked
2353 if b==b goto dest2
2354 :dest2
2355 echo goto with a leading space worked
2356 if c==c goto dest3
2357 :dest3
2358 echo goto with a leading tab worked
2359 if d==d goto dest4
2360 :dest4@space@
2361 echo goto with a following space worked
2363 echo ------------ Testing PATH ------------
2364 set backup_path=%path%
2365 set path=original
2366 path
2367 path try2
2368 path
2369 path=try3
2370 path
2371 set path=%backup_path%
2372 set backup_path=
2374 echo ------------ Testing combined CALLs/GOTOs ------------
2375 echo @echo off>foo.cmd
2376 echo goto :eof>>foot.cmd
2377 echo :eof>>foot.cmd
2378 echo echo world>>foo.cmd
2380 echo @echo off>foot.cmd
2381 echo echo cheball>>foot.cmd
2382 echo.>>foot.cmd
2383 echo call :bar>>foot.cmd
2384 echo if "%%1"=="deleteMe" (del foot.cmd)>>foot.cmd
2385 echo goto :eof>>foot.cmd
2386 echo.>>foot.cmd
2387 echo :bar>>foot.cmd
2388 echo echo barbare>>foot.cmd
2389 echo goto :eof>>foot.cmd
2391 call foo.cmd
2392 call foot
2393 call :bar
2394 del foo.cmd
2395 rem Script execution stops after the following line
2396 foot deleteMe
2397 call :foo
2398 call :foot
2399 goto :endFuns
2401 :foot
2402 echo foot
2404 :foo
2405 echo foo
2406 goto :eof
2408 :endFuns
2410 :bar
2411 echo bar
2412 call :foo
2414 :baz
2415 echo baz
2416 goto :eof
2418 echo Final message is not output since earlier 'foot' processing stops script execution
2419 echo Do NOT add any tests below this line
2421 echo ------------ Done, jumping to EOF -----------
2422 goto :eof
2423 rem Subroutine to set errorlevel and return
2424 rem in windows nt 4.0, this always sets errorlevel 1, since /b isn't supported
2425 :setError
2426 exit /B %1
2427 rem This line runs under cmd in windows NT 4, but not in more modern versions.