1 echo Tests
for cmd's builtin commands
4 echo ------------ Testing 'echo' [ON] ------------
6 echo 'singlequotedword
'
7 echo "doublequotedword"
20 echo word@space@@space@
24 echo@tab@word@tab@@space@
36 echo ------------ Testing 'echo' [OFF] ------------
38 echo 'singlequotedword
'
39 echo "doublequotedword"
52 echo word@space@@space@
56 echo@tab@word@tab@@space@
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 ------------
75 call :TestParm "a b c"
76 call :TestParm "a b"\c
77 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
93 echo ------------ Testing rem ------------
98 rem echo foo & echo bar
101 rem@tab@echo foo & echo bar
107 rem echo foo & echo bar
110 rem@tab@echo foo & echo bar
113 echo ------------ Testing redirection operators ------------
114 mkdir foobar & cd foobar
115 echo --- stdout redirection
124 echo foo@tab@1>@tab@foo
145 echo --- stdout appending
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
169 if 1==1 (echo foo2>bar) else echo baz2>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
175 if 1==0 (echo foo5>bar) else echo baz5>bar
177 if 1==0 (echo foo6) else echo baz6 1>bar
179 if 1==0 (echo foo7 1>bar) else echo baz7>bar
181 if 1==0 (echo foo8 1>bar) else echo baz8>bak
183 if 1==1 (echo foo>bar & echo baz)
190 (if 1==1 (echo A) else echo B) > C
192 (if 1==0 (echo A) else echo B) > 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
220 echo bak ^
&& echo baz
2> nul
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
240 echo ------------ Testing
'set' ------------
242 set FOOBAR
2> nul
> nul
246 echo %FOOBAR%FOOBAR
not defined
258 echo %FOOBAR%FOOBAR
not defined
260 set FOOBA
2> nul
> nul
270 echo %FOO%FOO
not defined
275 echo set "FOO=bar" should
not include the quotes in the variable value
288 echo ------------ Testing variable expansion
------------
290 echo ~dp0 should be directory containing batch file
306 echo %ERRORLEVEL%%ERRORLEVEL%
307 echo %ERRORLEVEL%ERRORLEVEL%
314 echo ------------ Testing variable substrings
------------
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
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
337 for %%i in
("F G" H
) do echo %%~di
339 for %%d in
("I J" K
) do echo %%~pd
341 for %%i in
("L M" N
) do echo %%~ni
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'
347 for %%i in
("U V" W
) do echo '%%~ai'
349 for %%i in
(foo
) do echo '%%~ai'
350 for %%i in
(foo
) do echo '%%~zi'
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'
356 rem Similar issues as above
357 for %%i in
("a b" c
) do echo '%%~zi'
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
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
371 for %%i in
("F G" H
) do call :echoFunD %%i
373 for %%d in
("I J" K
) do call :echoFunP %%d
375 for %%i in
("L M" N
) do call :echoFunN %%i
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
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
417 rem some NT4 workaround
440 echo ------------ Testing variable delayed expansion
------------
441 rem NT4 doesn't support this
442 echo --- default
mode (load
-time expansion
)
448 if %FOO% == bar
(echo bar
) else echo foo
454 if !FOO
! == bar
(echo bar
) else echo foo
457 echo --- runtime
(delayed
) expansion
mode
458 setlocal EnableDelayedExpansion
464 if %FOO% == bar
(echo bar
) else echo foo
470 if !FOO
! == bar
(echo bar
) else echo foo
473 setlocal DisableDelayedExpansion
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
486 cmd /V
:OfF /C 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
495 cd ..
& rd /s
/q foobar
497 echo foobar
not deleted
!
500 ) else echo foobar deleted
501 echo --- on success conditional and
502 call :setError 456 && echo foo3
> 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
516 echo ------------ Testing
cd ------------
519 echo blabla
> singleFile
521 echo Current
dir: %CD%
536 if not exist foobar
(cd ..
)
538 cd@tab@..@tab@@space@@tab@
540 if not exist foobar
(cd ..
)
554 cd "bar bak@space@
"@tab@@space@
565 echo ------------ Testing type ------------
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
591 rem NUL not special everywhere
594 if not exist foo (echo foo should have been created) else (
598 rem Empty file creation
609 echo ------------ Testing if/else ------------
610 echo --- if/else should work with blocks
612 echo if seems to work
614 echo if seems to be broken
617 echo else seems to be broken
619 echo else seems to work
622 echo if seems not to detect /c as parameter
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
772 if %1 LSS %2 echo %1 LSS %2
775 if %1 LEQ %2 echo %1 LEQ %2
778 if %1 EQU %2 echo %1 EQU %2
781 if %1 NEQ %2 echo %1 NEQ %2
784 if %1 GEQ %2 echo %1 GEQ %2
787 if %1 GTR %2 echo %1 GTR %2
790 :endIfCompOpsSubroutines
794 echo ------------ Testing 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
818 echo --- imbricated FORs
825 for %%i in (A B) do (
826 for %%j in (C D) do (
828 for %%i in (A B) do (
829 for %%j in (C D) do (
830 call :forTestFun2 %%i %%j ))
836 mkdir foobar & cd foobar
841 echo --- basic wildcards
842 for %%i in (ba*) do echo %%i
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
871 rem for /d %%i in (?a*) do echo %%i>> tmp
874 rem for /d %%i in (*) do echo %%i>> tmp
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"
897 for /l %%i in (1,1,%expectedresults%) do call :WriteLine if not "%%%%expectedresults.
%%i%%%%"=="" echo Found missing result: "%%%%expectedresults.
%%i%%%%"
903 rem %CD% does not tork on NT4 so use the following workaround
904 for /d %%i in (.) do set CURDIR=%%~dpnxi
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\.
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
925 for /R "%CURDIR%" %%i in (.) do call temp.bat %%i
926 call :ValidateExpected
929 echo File enumeration
930 set expectedresults=2
931 set expectedresults.1=%CURDIR%\baz\bazbaz
932 set expectedresults.2=%CURDIR%\bazbaz
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
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\.
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\.
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
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
996 for /R %%i in (baz* fred* jim) do call temp.bat %%i
997 call :ValidateExpected
1000 cd .. & rd /s/Q foobar
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%
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 (
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
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.
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
1133 for /f %%i in (foo) do echo %%i
1135 for /f %%i in (foo) do echo %%i
1137 for /f %%i in (foo) do echo %%i
1139 for /f %%i in (foo) do echo %%i
1141 for /f %%i in (foo) do echo %%i
1143 for /f %%i in (foo) do echo %%i
1146 for /f %%i in (foo) do echo %%i
1147 echo --------- multi-line with empty lines
1154 for /f %%b in (foo) do echo %%b
1155 echo --------- multiple files
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
1169 for /l %%i in (1,1,3) do echo Missing functionality - Broken%%i
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
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
1184 goto :ContinueFORFeol
1186 for /l %%i in (1,1,3) do echo Broken NT4 functionality%%i
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
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)
1219 echo ------------ Testing del /a ------------
1220 del /f/q *.test > nul
1223 echo not-r > not-r.test
1225 if not exist not-r.test echo not-r.test not found before delete, bad
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
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 ------------
1239 echo abc > file2.dat
1240 rem If /q doesn't work, cmd will prompt and the test case should hang
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
1247 echo ------------ Testing del /s ------------
1252 echo there > file2.dat
1253 echo bub > file3.dat
1254 echo bye > "file with spaces.dat
"
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
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
"
1269 echo ------------ Testing rename ------------
1270 mkdir foobar & cd foobar
1271 echo --- ren and rename are synonymous
1274 if exist foo echo foo should be renamed!
1275 if exist bar echo foo renamed to bar
1277 if exist bar echo bar should be renamed!
1278 if exist foo echo bar renamed to foo
1279 echo --- name collision
1289 echo --- rename read-only files
1293 if not exist file1 (
1295 echo read-only file renamed
1298 echo read-only file not renamed!
1300 echo --- rename directories
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
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
1329 move foo bar > nul 2>&1
1332 echo file move succeeded
1337 move /Y bar baz > nul 2>&1
1340 echo file move with overwrite succeeded
1343 echo file overwrite impossible!
1349 move baz bazro > nul 2>&1
1352 echo read-only files are moveable
1353 move bazro baz > nul 2>&1
1356 echo read-only file not moved!
1360 move baz rep > nul 2>&1
1363 echo file moved in subdirectory
1367 move rep\baz . > nul 2>&1
1368 move /Y baz baz > nul 2>&1
1370 echo moving a file to itself should be a no-op!
1372 echo moving a file to itself is a no-op
1374 echo ErrorLevel: %ErrorLevel%
1377 echo --- directory move
1381 move baz foo\bar > nul 2>&1
1383 if exist foo\bar\baz\baz2 (
1384 echo simple directory move succeeded
1389 move baz baz > nul 2>&1
1390 echo moving a directory to itself gives error; errlevel %ErrorLevel%
1391 echo ------ dir in dir move
1396 move foo bar > nul 2>&1
1404 cd .. & rd /s/q foobar
1406 echo ------------ Testing mkdir ------------
1408 echo --- md and mkdir are synonymous
1415 echo --- creating an already existing directory/file must fail
1424 echo --- multilevel path creation
1436 mkdir ..\..\..\foo\bar2
1439 mkdir ..\..\..\foo\bar2
1441 rmdir ..\..\..\foo\bar2
1449 echo --- trailing backslashes
1452 if exist foo (rmdir foo & echo dir created
1453 ) else ( echo dir not created )
1455 echo --- invalid chars
1457 echo mkdir ? gives errorlevel %ErrorLevel%
1460 echo mkdir ?\foo gives errorlevel %ErrorLevel%
1463 echo mkdir foo\? gives errorlevel %ErrorLevel%
1464 if exist foo (rmdir foo & echo ok, foo created
1465 ) else ( echo foo not created )
1468 echo mkdir foo\bar\? gives errorlevel %ErrorLevel%
1471 echo bad, foo not created
1475 echo ok, foo\bar created
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
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 ------------
1499 rem rd and rmdir are synonymous
1503 if not exist foobar
echo dir removed
1507 if not exist foobar
echo dir removed
1508 rem Removing nonexistent directory
1511 rem Removing single-level directories
1515 if exist foo
echo file
not removed
1521 if exist foo
echo non
-empty
dir not removed
1526 if exist foo
echo non
-empty
dir not removed
1533 echo recursive
rmdir succeeded
1540 echo foo
> foo
\bar
\brol
1541 rmdir /s
/Q foo
2>&1
1543 echo recursive
rmdir succeeded
1550 rem multiples directories at once
1551 mkdir foobaz
& cd foobaz
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
1567 echo --- pushing non
-existing
dir
1570 echo --- basic behaviour
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
1601 echo --- read
-only attribute
1602 rem Read-only files cannot be altered or deleted, unless forced
1610 echo Read
-only file
not deleted
1612 echo Should
not delete read
-only file
!
1616 echo Read
-only file forcibly deleted
1618 echo Should delete read
-only file with
del /F
!
1622 cd ..
& rd /s
/q foobar
1623 echo --- recursive behaviour
1624 mkdir foobar
\baz
& cd foobar
1630 attrib +R l
*vel?
/S
> nul
2>&1
1636 cd ..
& rd /s
/q foobar
1637 echo --- folders processing
1643 attrib +r baz
/s
/d
> nul
2>&1
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
1665 rem association set system-wide
1666 echo @
echo off
> tmp.
cmd
1667 echo echo +++>> tmp.
cmd
1668 echo assoc .foo
>> tmp.
cmd
1671 echo --- resetting association
1677 rem association removal set system-wide
1678 cmd /c tmp.
cmd > baz
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
1692 ftype footype
=foo_opencmd
1696 rem association set system-wide
1697 echo @
echo off
> tmp.
cmd
1698 echo echo +++>> tmp.
cmd
1699 echo ftype footype
>> tmp.
cmd
1702 echo --- resetting association
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
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)
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
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
1731 echo echo %%1 %%2 > foo.
cmd
1733 call foo.
cmd foo bar
1740 echo --- internal routines
1741 call :testRoutine :testRoutine
1742 goto :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
1760 echo --- with builtins
1763 if exist foo
(echo foo created
) else echo foo should
exist!
1765 set FOOBAZ_VAR
=foobaz
1766 call echo Should expand
%FOOBAZ_VAR%
1771 if 1==1 call del batfile
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
1778 call for %%i in (foo bar baz) do echo %%i 2> nul
1780 rem First look for programs in the path before trying a builtin
1781 echo echo non-builtin dir> dir.cmd
1784 rem The below line equates to call (, which does nothing, then the
1785 rem subsequent lines are executed.
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
1798 call call call echo passed
1799 cd .. & rd /s/q foobar
1801 echo ------------ Testing SHIFT ------------
1803 call :shiftFun p1 p2 p3 p4 p5
1807 echo '%1' '%2' '%3' '%4' '%5'
1809 echo '%1' '%2' '%3' '%4' '%5'
1811 echo '%1' '%2' '%3' '%4' '%5'
1813 echo '%1' '%2' '%3' '%4' '%5'
1815 echo '%1' '%2' '%3' '%4' '%5'
1817 echo '%1' '%2' '%3' '%4' '%5'
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
1826 if not exist foo.cmd (
1827 echo file correctly deleted
1829 echo file should be deleted!
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
1839 echo file not created!
1842 echo ---------- Testing copy
1845 rem Note echo adds 0x0d 0x0a on the end of the line in the file
1848 echo CCCCCCCCC> file3
1854 echo Passed: Found expected %1
1856 echo Failed: Did not find expected %1
1858 del /q "%1" >nul 2>&1
1860 if not "%1"=="" goto :CheckExist
1865 echo Passed: Did not find %1
1867 echo Failed: Unexpectedly found %1
1868 del /q "%1" >nul 2>&1
1871 if not "%1"=="" goto :CheckNotExist
1874 rem Note: No way to check file size on NT4 so skip the test
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%]
1884 if "%filesize%"=="%%~zi" (
1885 echo Skipping file size check on NT4
1887 echo Failed: file size check on %1 [%filesize% != %2]
1890 :ContinueFileSizeChecks
1893 if not "%1"=="" goto :CheckFileSize
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
1922 copy ..\file1 >nul 2>&1
1923 if errorlevel 1 echo Incorrect errorlevel
1924 call :CheckExist file1
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 -----------------------
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
1946 copy ..\file*.* >nul 2>&1
1947 if errorlevel 1 echo Incorrect errorlevel
1948 call :CheckExist file1 file2 file3
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
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)
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
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
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
2118 rem ---------------------------------------
2119 rem Error combinations
2120 rem ---------------------------------------
2121 rem Specify source directory but name is a file
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
\file
1
2128 rem Overwrite same file
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
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
2143 echo ------------ Testing
setlocal/endlocal ------------
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%
2151 echo ErrLev
: %ErrorLevel%
2152 echo @
echo off
> tmp.
cmd
2153 echo echo ErrLev
: %%ErrorLevel%%>> tmp.
cmd
2154 rem Enabled by default
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
2177 for /d
%%i in
(.
) do echo %%~dpnxi
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
2191 rem %CD% does not tork on NT4 so use the following workaround
2192 for /d
%%i in
(.
) do set CURDIR
=%%~dpnxi
2195 for /d
%%i in
(.
) do echo %%~dpnxi
2199 echo --- setlocal within same batch program
2203 rem %CD% does not tork on NT4 so use the following workaround
2204 for /d
%%i in
(.
) do set CURDIR
=%%~dpnxi
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
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
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
2231 echo --- Mismatched
set and
end locals
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 ..
\foodir
3>> 2set1end.
cmd
2243 echo setlocal>> 2set1end.
cmd
2244 echo set VAR
=2set1endvalue2
>> 2set1end.
cmd
2245 echo cd ..
\foodir
4>> 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 ..
\foodir
3>> 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
2264 rem -- setlocal1 == this batch, should never be used inside a called routine
2268 call %curdir%\
2set1end.
cmd
2271 for /d
%%i in
(.
) do echo %%~dpnxi
2274 for /d
%%i in
(.
) do echo %%~dpnxi
2277 echo --- Extra
endlocal in called batch
2279 rem -- setlocal1 == this batch, should never be used inside a called routine
2283 call %curdir%\
1set2end.
cmd
2286 for /d
%%i in
(.
) do echo %%~dpnxi
2289 for /d
%%i in
(.
) do echo %%~dpnxi
2292 echo --- endlocal in called function rather than batch pgm is ineffective
2298 call :endlocalroutine
2302 goto :endlocalfinished
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
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
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
!
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
2344 echo %ErrorLevel% should be
7
2345 if errorlevel 7 echo setting var worked too well
, bad
2347 echo %ErrorLevel% should still be
7
2349 echo ------------ Testing
GOTO ------------
2352 echo goto with no leading space worked
2355 echo goto with a leading space worked
2358 echo goto with a leading tab worked
2361 echo goto with a following space worked
2363 echo ------------ Testing
PATH ------------
2364 set backup_path
=%path%
2371 set path=%backup_path%
2374 echo ------------ Testing combined CALLs
/GOTOs
------------
2375 echo @
echo off
>foo.
cmd
2376 echo goto :eof>>foot.
cmd
2378 echo echo world
>>foo.
cmd
2380 echo @
echo off
>foot.
cmd
2381 echo echo cheball
>>foot.
cmd
2383 echo call :bar>>foot.
cmd
2384 echo if "%%1"=="deleteMe" (del foot.
cmd)>>foot.
cmd
2385 echo goto :eof>>foot.
cmd
2388 echo echo barbare
>>foot.
cmd
2389 echo goto :eof>>foot.
cmd
2395 rem Script execution stops after the following line
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
-----------
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
2427 rem This line runs under cmd in windows NT 4, but not in more modern versions.