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 be recognized) else (echo string/hexa compare ok)
736 if "0xA1" EQU 161 (echo hexa should be 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 for /F %%i in ("a b c") do echo %%i
1115 for /f %%i in ("a ") do echo %%i
1116 for /f %%i in ("a") do echo %%i
1117 fOr /f %%i in (" a") do echo %%i
1118 for /f %%i in (" a ") do echo %%i
1119 echo ------ fileset argument
1120 echo --------- basic blank handling
1122 for /f %%i in (foo) do echo %%i
1124 for /f %%i in (foo) do echo %%i
1126 for /f %%i in (foo) do echo %%i
1128 for /f %%i in (foo) do echo %%i
1130 for /f %%i in (foo) do echo %%i
1132 for /f %%i in (foo) do echo %%i
1135 for /f %%i in (foo) do echo %%i
1136 echo --------- multi-line with empty lines
1143 for /f %%b in (foo) do echo %%b
1144 echo --------- multiple files
1148 for /f %%k in (foo bar) do echo %%k
1149 for /f %%k in (bar foo) do echo %%k
1150 echo ------ command argument
1151 rem Not implemented on NT4, need to skip it as no way to get output otherwise
1152 if "%CD%"=="" goto :SkipFORFcmdNT4
1153 for /f %%i in ('echo.Passed1') do echo %%i
1154 for /f "usebackq" %%i in (`echo.Passed2`) do echo %%i
1155 for /f usebackq %%i in (`echo.Passed3`) do echo %%i
1158 for /l %%i in (1,1,3) do echo Missing functionality - Broken%%i
1160 rem FIXME: Rest not testable right now in wine: not implemented and would need
1161 rem preliminary grep-like program implementation (e.g. like findstr or fc) even
1162 rem for a simple todo_wine test
1163 rem (for /f "usebackq" %%i in (`echo z a b`) do echo %%i) || echo not supported
1164 rem (for /f usebackq %%i in (`echo z a b`) do echo %%i) || echo not supported
1165 echo ------ eol option
1166 if "%CD%"=="" goto :SkipFORFeolNT4
1168 echo and Line two>>foo
1169 echo Line three>>foo
1170 for /f "eol=L" %%i in (foo) do echo %%i
1171 for /f "eol=a" %%i in (foo) do echo %%i
1173 goto :ContinueFORFeol
1175 for /l %%i in (1,1,3) do echo Broken NT4 functionality%%i
1177 for /f "eol=@" %%i in (" ad") do echo %%i
1178 for /f "eol=@" %%i in (" z@y") do echo %%i
1179 for /f "eol=|" %%i in ("a|d") do echo %%i
1180 for /f "eol=@" %%i in ("@y") do echo %%i > output_file
1181 if not exist output_file (echo no output) else (del output_file)
1182 for /f "eol==" %%i in ("=y") do echo %%i > output_file
1183 if not exist output_file (echo no output) else (del output_file)
1184 echo ------ delims option
1185 for /f "delims=|" %%i in ("a|d") do echo %%i
1186 for /f "delims=|" %%i in ("a |d") do echo %%i
1187 for /f "delims=|" %%i in ("a d|") do echo %%i
1188 for /f "delims=| " %%i in ("a d|") do echo %%i
1189 for /f "delims==" %%i in ("C r=d|") do echo %%i
1190 for /f "delims=" %%i in ("foo bar baz") do echo %%i
1191 for /f "delims=" %%i in ("c:\foo bar baz\..") do echo %%~fi
1192 echo ------ skip option
1196 for /f "skip=2" %%i in (foo) do echo %%i
1197 for /f "skip=3" %%i in (foo) do echo %%i > output_file
1198 if not exist output_file (echo no output) else (del output_file)
1199 for /f "skip=4" %%i in (foo) do echo %%i > output_file
1200 if not exist output_file (echo no output) else (del output_file)
1201 for /f "skip=02" %%i in (foo) do echo %%i
1202 for /f "skip=0x2" %%i in (foo) do echo %%i
1203 for /f "skip=1" %%i in ("skipme") do echo %%i > output_file
1204 if not exist output_file (echo no output) else (del output_file)
1208 echo ------------ Testing del /a ------------
1209 del /f/q *.test > nul
1212 echo not-r > not-r.test
1214 if not exist not-r.test echo not-r.test not found before delete, bad
1216 if not exist not-r.test echo not-r.test not found after delete, good
1218 if not exist r.test echo r.test not found before delete, bad
1219 if exist r.test echo r.test found before delete, good
1221 if not exist r.test echo r.test not found after delete, good
1222 if exist r.test echo r.test found after delete, bad
1224 echo ------------ Testing del /q ------------
1228 echo abc > file2.dat
1229 rem If /q doesn't work, cmd will prompt and the test case should hang
1231 for %%a in (1 2.dat) do if exist file%%a echo del /q * failed on file%%a
1232 for %%a in (1 2.dat) do if not exist file%%a echo del /q * succeeded on file%%a
1236 echo ------------ Testing del /s ------------
1241 echo there > file2.dat
1242 echo bub > file3.dat
1243 echo bye > "file with spaces.dat"
1245 del /s file1.dat > nul
1246 del file2.dat /s > nul
1247 del "file3.dat" /s > nul
1248 del "file with spaces.dat" /s > nul
1250 for %%f in (1 2 3) do if exist file%%f.dat echo Del /s failed on file%%f
1251 for %%f in (1 2 3) do if exist file%%f.dat del file%%f.dat
1252 if exist "file with spaces.dat" echo Del /s failed on "file with spaces.dat"
1253 if exist "file with spaces.dat" del "file with spaces.dat"
1258 echo ------------ Testing rename ------------
1259 mkdir foobar & cd foobar
1260 echo --- ren and rename are synonymous
1263 if exist foo echo foo should be renamed!
1264 if exist bar echo foo renamed to bar
1266 if exist bar echo bar should be renamed!
1267 if exist foo echo bar renamed to foo
1268 echo --- name collision
1278 echo --- rename read-only files
1282 if not exist file1 (
1284 echo read-only file renamed
1287 echo read-only file not renamed!
1289 echo --- rename directories
1301 echo read-only dir renamed
1304 echo --- rename in other directory
1305 if not exist baz\abc (
1306 echo rename impossible in other directory
1307 if exist foo echo original file still present
1309 echo shouldn't rename in other directory!
1310 if not exist foo echo original file not present anymore
1312 cd .. & rd /s/q foobar
1314 echo ------------ Testing move ------------
1315 mkdir foobar & cd foobar
1318 move foo bar > nul 2>&1
1321 echo file move succeeded
1326 move /Y bar baz > nul 2>&1
1329 echo file move with overwrite succeeded
1332 echo file overwrite impossible!
1338 move baz bazro > nul 2>&1
1341 echo read-only files are moveable
1342 move bazro baz > nul 2>&1
1345 echo read-only file not moved!
1349 move baz rep > nul 2>&1
1352 echo file moved in subdirectory
1356 move rep\baz . > nul 2>&1
1357 move /Y baz baz > nul 2>&1
1359 echo moving a file to itself should be a no-op!
1361 echo moving a file to itself is a no-op
1363 echo ErrorLevel: %ErrorLevel%
1366 echo --- directory move
1370 move baz foo\bar > nul 2>&1
1372 if exist foo\bar\baz\baz2 (
1373 echo simple directory move succeeded
1378 move baz baz > nul 2>&1
1379 echo moving a directory to itself gives error; errlevel %ErrorLevel%
1380 echo ------ dir in dir move
1385 move foo bar > nul 2>&1
1393 cd .. & rd /s/q foobar
1395 echo ------------ Testing mkdir ------------
1397 echo --- md and mkdir are synonymous
1404 echo --- creating an already existing directory/file must fail
1413 echo --- multilevel path creation
1425 mkdir ..\..\..\foo\bar2
1428 mkdir ..\..\..\foo\bar2
1430 rmdir ..\..\..\foo\bar2
1438 echo --- trailing backslashes
1441 if exist foo (rmdir foo & echo dir created
1442 ) else ( echo dir not created )
1444 echo --- invalid chars
1446 echo mkdir ? gives errorlevel %ErrorLevel%
1449 echo mkdir ?\foo gives errorlevel %ErrorLevel%
1452 echo mkdir foo\? gives errorlevel %ErrorLevel%
1453 if exist foo (rmdir foo & echo ok, foo created
1454 ) else ( echo foo not created )
1457 echo mkdir foo\bar\? gives errorlevel %ErrorLevel%
1460 echo bad, foo not created
1464 echo ok, foo\bar created
1470 echo --- multiple directories at once
1471 mkdir foobaz & cd foobaz
1472 mkdir foo bar\baz foobar "bazbaz" .\"zabzab"
1473 if exist foo (echo foo created) else echo foo not created!
1474 if exist bar (echo bar created) else echo bar not created!
1475 if exist foobar (echo foobar created) else echo foobar not created!
1476 if exist bar\baz (echo bar\baz created) else echo bar\baz not created!
1477 if exist bazbaz (echo bazbaz created) else echo bazbaz not created!
1478 if exist zabzab (echo zabzab created) else echo zabzab not created!
1479 cd .. & rd /s/q foobaz
1482 echo mkdir foo\* errorlevel %ErrorLevel%
1483 if exist foo (rmdir foo & echo ok, foo created
1484 ) else ( echo bad, foo not created )
1486 echo ------------ Testing rmdir ------------
1488 rem rd and rmdir are synonymous
1492 if not exist foobar echo dir removed
1496 if not exist foobar echo dir removed
1497 rem Removing nonexistent directory
1500 rem Removing single-level directories
1504 if exist foo echo file not removed
1510 if exist foo echo non-empty dir not removed
1515 if exist foo echo non-empty dir not removed
1522 echo recursive rmdir succeeded
1529 echo foo > foo\bar\brol
1530 rmdir /s /Q foo 2>&1
1532 echo recursive rmdir succeeded
1539 rem multiples directories at once
1540 mkdir foobaz & cd foobaz
1544 rd /s/q foo bar foobar
1545 if not exist foo (echo foo removed) else echo foo not removed!
1546 if not exist bar (echo bar removed) else echo bar not removed!
1547 if not exist foobar (echo foobar removed) else echo foobar not removed!
1548 if not exist bar\baz (echo bar\baz removed) else echo bar\baz not removed!
1549 cd .. & rd /s/q foobaz
1551 echo ------------ Testing pushd/popd ------------
1553 echo --- popd is no-op when dir stack is empty
1556 echo --- pushing non-existing dir
1559 echo --- basic behaviour
1584 echo ------------ Testing attrib ------------
1585 rem FIXME Add tests for archive, hidden and system attributes + mixed attributes modifications
1586 mkdir foobar & cd foobar
1587 echo foo original contents> foo
1590 echo --- read-only attribute
1591 rem Read-only files cannot be altered or deleted, unless forced
1599 echo Read-only file not deleted
1601 echo Should not delete read-only file!
1605 echo Read-only file forcibly deleted
1607 echo Should delete read-only file with del /F!
1611 cd .. & rd /s/q foobar
1612 echo --- recursive behaviour
1613 mkdir foobar\baz & cd foobar
1619 attrib +R l*vel? /S > nul 2>&1
1625 cd .. & rd /s/q foobar
1626 echo --- folders processing
1632 attrib +r baz /s /d > nul 2>&1
1638 rem Oddly windows allows file creation in a read-only directory...
1639 if exist baz\lala (echo file created in read-only dir) else echo file not created
1640 cd .. & rd /s/q foobar
1642 echo ------------ Testing assoc ------------
1643 rem FIXME Can't test error messages in the current test system, so we have to use some kludges
1644 rem FIXME Revise once || conditional execution is fixed
1645 mkdir foobar & cd foobar
1646 echo --- setting association
1654 rem association set system-wide
1655 echo @echo off> tmp.cmd
1656 echo echo +++>> tmp.cmd
1657 echo assoc .foo>> tmp.cmd
1660 echo --- resetting association
1666 rem association removal set system-wide
1667 cmd /c tmp.cmd > baz
1670 cd .. & rd /s/q foobar
1672 echo ------------ Testing ftype ------------
1673 rem FIXME Can't test error messages in the current test system, so we have to use some kludges
1674 rem FIXME Revise once || conditional execution is fixed
1675 mkdir foobar & cd foobar
1676 echo --- setting association
1681 ftype footype=foo_opencmd
1685 rem association set system-wide
1686 echo @echo off> tmp.cmd
1687 echo echo +++>> tmp.cmd
1688 echo ftype footype>> tmp.cmd
1691 echo --- resetting association
1694 rem Removing a file type association doesn't work on XP due to a bug, so a workaround is needed
1695 setlocal EnableDelayedExpansion
1696 set FOO=original value
1699 for /F %%i in ('type baz') do (set FOO=buggyXP)
1700 rem Resetting actually works on wine/NT4, but is reported as failing due to the peculiar test (and non-support for EnabledDelayedExpansion)
1701 rem FIXME Revisit once a grep-like program like ftype is implemented
1702 rem (e.g. to check baz's size using dir /b instead)
1705 rem cleanup registry
1706 echo REGEDIT4> regCleanup.reg
1707 echo.>> regCleanup.reg
1708 echo [-HKEY_CLASSES_ROOT\footype]>> regCleanup.reg
1709 regedit /s regCleanup.reg
1712 cd .. & rd /s/q foobar
1714 echo ------------ Testing CALL ------------
1715 mkdir foobar & cd foobar
1716 echo --- external script
1717 echo echo foo %%1> foo.cmd
1720 echo echo %%1 %%2 > foo.cmd
1722 call foo.cmd foo bar
1729 echo --- internal routines
1730 call :testRoutine :testRoutine
1731 goto :endTestRoutine
1737 call :testRoutineArgs foo
1738 call :testRoutineArgs foo bar
1739 call :testRoutineArgs foo ""
1740 call :testRoutineArgs "" bar
1741 call :testRoutineArgs foo ''
1742 call :testRoutineArgs '' bar
1743 goto :endTestRoutineArgs
1749 echo --- with builtins
1752 if exist foo (echo foo created) else echo foo should exist!
1754 set FOOBAZ_VAR=foobaz
1755 call echo Should expand %FOOBAZ_VAR%
1760 if 1==1 call del batfile
1762 if exist batfile echo batfile shouldn't exist
1763 rem ... but not for 'if' or 'for'
1764 call if 1==1 echo bar 2> nul
1767 call for %%i in (foo bar baz) do echo %%i 2> nul
1769 rem First look for programs in the path before trying a builtin
1770 echo echo non-builtin dir> dir.cmd
1773 rem The below line equates to call (, which does nothing, then the
1774 rem subsequent lines are executed.
1779 rem The below line equates to call if, which always fails, then the
1780 rem subsequent lines are executed. Note cmd.exe swallows all lines
1787 call call call echo passed
1788 cd .. & rd /s/q foobar
1790 echo ------------ Testing SHIFT ------------
1792 call :shiftFun p1 p2 p3 p4 p5
1796 echo '%1' '%2' '%3' '%4' '%5'
1798 echo '%1' '%2' '%3' '%4' '%5'
1800 echo '%1' '%2' '%3' '%4' '%5'
1802 echo '%1' '%2' '%3' '%4' '%5'
1804 echo '%1' '%2' '%3' '%4' '%5'
1806 echo '%1' '%2' '%3' '%4' '%5'
1810 echo ------------ Testing cmd invocation ------------
1811 rem FIXME: only a stub ATM
1812 echo --- a batch file can delete itself
1813 echo del foo.cmd>foo.cmd
1815 if not exist foo.cmd (
1816 echo file correctly deleted
1818 echo file should be deleted!
1821 echo --- a batch file can alter itself
1822 echo echo bar^>foo.cmd>foo.cmd
1823 cmd /q /c foo.cmd > NUL 2>&1
1828 echo file not created!
1831 echo ---------- Testing copy
1834 rem Note echo adds 0x0d 0x0a on the end of the line in the file
1837 echo CCCCCCCCC> file3
1843 echo Passed: Found expected %1
1845 echo Failed: Did not find expected %1
1847 del /q "%1" >nul 2>&1
1849 if not "%1"=="" goto :CheckExist
1854 echo Passed: Did not find %1
1856 echo Failed: Unexpectedly found %1
1857 del /q "%1" >nul 2>&1
1860 if not "%1"=="" goto :CheckNotExist
1863 rem Note: No way to check file size on NT4 so skip the test
1866 echo Failed: File missing when requested filesize check [%2]
1867 goto :ContinueFileSizeChecks
1869 for %%i in (%1) do set filesize=%%~zi
1870 if "%filesize%"=="%2" (
1871 echo Passed: file size check on %1 [%filesize%]
1873 if "%filesize%"=="%%~zi" (
1874 echo Skipping file size check on NT4
1876 echo Failed: file size check on %1 [%filesize% != %2]
1879 :ContinueFileSizeChecks
1882 if not "%1"=="" goto :CheckFileSize
1887 rem -----------------------
1888 rem Simple single file copy
1889 rem -----------------------
1890 rem Simple single file copy, normally used syntax
1891 copy file1 dummy.file >nul 2>&1
1892 if errorlevel 1 echo Incorrect errorlevel
1893 call :CheckExist dummy.file
1895 rem Simple single file copy, destination supplied as two forms of directory
1896 copy file1 dir1 >nul 2>&1
1897 if errorlevel 1 echo Incorrect errorlevel
1898 call :CheckExist dir1\file1
1900 copy file1 dir1\ >nul 2>&1
1901 if errorlevel 1 echo Incorrect errorlevel
1902 call :CheckExist dir1\file1
1904 rem Simple single file copy, destination supplied as fully qualified destination
1905 copy file1 dir1\file99 >nul 2>&1
1906 if errorlevel 1 echo Incorrect errorlevel
1907 call :CheckExist dir1\file99
1909 rem Simple single file copy, destination not supplied
1911 copy ..\file1 >nul 2>&1
1912 if errorlevel 1 echo Incorrect errorlevel
1913 call :CheckExist file1
1916 rem Simple single file copy, destination supplied as nonexistent directory
1917 copy file1 dir2\ >nul 2>&1
1918 if not errorlevel 1 echo Incorrect errorlevel
1919 call :CheckNotExist dir2 dir2\file1
1921 rem -----------------------
1923 rem -----------------------
1924 rem Simple single file copy, destination supplied as two forms of directory
1925 copy file? dir1 >nul 2>&1
1926 if errorlevel 1 echo Incorrect errorlevel
1927 call :CheckExist dir1\file1 dir1\file2 dir1\file3
1929 copy file* dir1\ >nul 2>&1
1930 if errorlevel 1 echo Incorrect errorlevel
1931 call :CheckExist dir1\file1 dir1\file2 dir1\file3
1933 rem Simple single file copy, destination not supplied
1935 copy ..\file*.* >nul 2>&1
1936 if errorlevel 1 echo Incorrect errorlevel
1937 call :CheckExist file1 file2 file3
1940 rem Simple wildcarded file copy, destination supplied as nonexistent directory
1941 copy file? dir2\ >nul 2>&1
1942 if not errorlevel 1 echo Incorrect errorlevel
1943 call :CheckNotExist dir2 dir2\file1 dir2\file2 dir2\file3
1945 rem ------------------------------------------------
1946 rem Confirm overwrite works (cannot test prompting!)
1947 rem ------------------------------------------------
1948 copy file1 testfile >nul 2>&1
1949 copy /y file2 testfile >nul 2>&1
1950 call :CheckExist testfile
1952 rem ------------------------------------------------
1953 rem Test concatenation
1954 rem ------------------------------------------------
1955 rem simple case, no wildcards
1956 copy file1+file2 testfile >nul 2>&1
1957 if errorlevel 1 echo Incorrect errorlevel
1958 call :CheckExist testfile
1960 rem simple case, wildcards, no concatenation
1961 copy file* testfile >nul 2>&1
1962 if errorlevel 1 echo Incorrect errorlevel
1963 call :CheckExist testfile
1965 rem simple case, wildcards, and concatenation
1967 copy file*+fred testfile >nul 2>&1
1968 if errorlevel 1 echo Incorrect errorlevel
1969 call :CheckExist testfile
1971 rem simple case, wildcards, and concatenation
1972 copy fred+file* testfile >nul 2>&1
1973 if errorlevel 1 echo Incorrect errorlevel
1974 call :CheckExist testfile
1976 rem Calculate destination name
1977 copy fred+file* dir1 >nul 2>&1
1978 if errorlevel 1 echo Incorrect errorlevel
1979 call :CheckExist dir1\fred
1981 rem Calculate destination name
1982 copy fred+file* dir1\ >nul 2>&1
1983 if errorlevel 1 echo Incorrect errorlevel
1984 call :CheckExist dir1\fred
1986 rem Calculate destination name (none supplied)
1988 copy ..\fred+..\file* >nul 2>&1
1989 if errorlevel 1 echo Incorrect errorlevel
1990 call :CheckExist fred
1992 copy ..\fr*+..\file1 >nul 2>&1
1993 if errorlevel 1 echo Incorrect errorlevel
1994 call :CheckExist fred
1997 rem ******************************************************************
1998 rem ASCII and BINARY tests
1999 rem Note: hard coded numbers deliberate because need to ensure whether
2000 rem an additional EOF has been added or not. There is no way to handle
2001 rem EOFs in batch, so assume if a single byte appears, its an EOF!
2002 rem ******************************************************************
2004 rem Confirm original sizes of file1,2,3
2005 call :CheckFileSize file1 5 file2 8 file3 11
2009 rem ----------------------------------------------
2010 rem Show concatenation defaults copy to ascii mode
2011 rem ----------------------------------------------
2012 rem Simple default copy source to destination (should not append EOF 5)
2013 copy ..\file1 file1_default >nul 2>&1
2014 call :CheckFileSize file1_default 5
2016 rem Simple binary copy source to destination (should not append EOF 5)
2017 copy /b ..\file1 file1_default2 >nul 2>&1
2018 call :CheckFileSize file1_default2 5
2020 rem Simple ascii copy source to destination (should append EOF 5+1, 8+1, 11+1)
2021 copy /a ..\file1 file1_plus_eof >nul 2>&1
2022 call :CheckFileSize file1_plus_eof 6
2023 copy /a ..\file2 file2_plus_eof >nul 2>&1
2024 call :CheckFileSize file2_plus_eof 9
2025 copy /a ..\file3 file3_plus_eof >nul 2>&1
2026 call :CheckFileSize file3_plus_eof 12
2028 rem Concat 2 files, ascii mode - (only one EOF on the end 5+8+1)
2029 copy /a ..\file1+..\file2 file12_plus_eof >nul 2>&1
2030 call :CheckFileSize file12_plus_eof 14
2032 rem Concat 2 files, binary mode - (no EOF on the end 5+8)
2033 copy /b ..\file1+..\file2 file12_no_eof >nul 2>&1
2034 call :CheckFileSize file12_no_eof 13
2036 rem Concat 2 files, default mode - (one EOF on the end 5+8+1)
2037 copy ..\file1+..\file2 file12_eof2 >nul 2>&1
2038 call :CheckFileSize file12_eof2 14
2040 rem --------------------------------------------------------------
2041 rem Show ascii source copy stops at first EOF, binary does the lot
2042 rem --------------------------------------------------------------
2043 copy file1_plus_eof /b file1_binary_srccopy /b >nul 2>&1
2044 call :CheckFileSize file1_binary_srccopy 6
2046 copy file1_plus_eof /a file1_ascii_srccopy /b >nul 2>&1
2047 call :CheckFileSize file1_ascii_srccopy 5
2049 rem --------------------------------------------------------------
2050 rem Show results of concatenating files (ending in EOFs) and /a /b
2051 rem --------------------------------------------------------------
2053 rem Default and ascii copy reads as ascii, stripping EOFs, so 6-1 + 9-1 + 12-1 + 1
2054 copy file1_plus_eof+file2_plus_eof+file3_plus_eof file123_default_copy >nul 2>&1
2055 call :CheckFileSize file123_default_copy 25
2056 copy /a file1_plus_eof+file2_plus_eof+file3_plus_eof file123_ascii_copy >nul 2>&1
2057 call :CheckFileSize file123_ascii_copy 25
2059 rem In binary mode, we get 3 eofs, so 6 + 9 + 12 = 27
2060 copy /b file1_plus_eof + file2_plus_eof + file3_plus_eof file123_binary_copy >nul 2>&1
2061 call :CheckFileSize file123_binary_copy 27
2063 rem We can select which we want the eofs from by postfixing it with /a or /b
2064 rem so here have first and third with eof, second as ascii 6 + 9-1 + 12
2065 copy file1_plus_eof /b + file2_plus_eof /a + file3_plus_eof /b file123_mixed_copy1 >nul 2>&1
2066 call :CheckFileSize file123_mixed_copy1 26
2068 rem By postfixing the destination with /a, we ask for an ascii destination which appends EOF
2069 rem so here have first and third with eof, second as ascii 6 + 9-1 + 12 + extra EOF
2070 rem Note the delta between this and the previous one also shows that the destination
2071 rem ascii/binary is inherited from the last /a or /b on the line
2072 copy file1_plus_eof /b + file2_plus_eof /a + file3_plus_eof /b file123_mixed_copy2 /a >nul 2>&1
2073 call :CheckFileSize file123_mixed_copy2 27
2075 rem so here have second with eof, first and third as ascii 6-1 + 9 + 12-1
2076 rem Note the delta between the next two also shows that the destination ascii/binary is
2077 rem inherited from the last /a or /b on the line, so the first has an extra EOF
2078 copy file1_plus_eof /a + file2_plus_eof /b + file3_plus_eof /a file123_mixed_copy3 >nul 2>&1
2079 call :CheckFileSize file123_mixed_copy3 26
2080 copy file1_plus_eof /a + file2_plus_eof /b + file3_plus_eof /a file123_mixed_copy4 /b >nul 2>&1
2081 call :CheckFileSize file123_mixed_copy4 25
2083 rem -------------------------------------------------------------------------------------------
2084 rem This shows when concatenating, an ascii destination always adds on an EOF but when we
2085 rem are not concatenating, its a direct copy regardless of destination if being read as binary
2086 rem -------------------------------------------------------------------------------------------
2088 rem All 3 have eof's, plus an extra = 6 + 9 + 12 + eof
2089 copy /b file1_plus_eof + file2_plus_eof + file3_plus_eof file123_mixed_copy5 /a >nul 2>&1
2090 call :CheckFileSize file123_mixed_copy5 28
2092 rem All 2 have eof's, plus an extra = 6 + 12 + eof
2093 copy /b file1_plus_eof + file3_plus_eof file123_mixed_copy6 /a >nul 2>&1
2094 call :CheckFileSize file123_mixed_copy6 19
2096 rem One file has EOF, but doesn't get an extra one, i.e. 6
2097 copy /b file1_plus_eof file123_mixed_copy7 /a >nul 2>&1
2098 call :CheckFileSize file123_mixed_copy7 6
2100 rem Syntax means concatenate so ascii destination kicks in
2101 copy /b file1_plus_eof* file123_mixed_copy8 /a >nul 2>&1
2102 call :CheckFileSize file123_mixed_copy8 7
2107 rem ---------------------------------------
2108 rem Error combinations
2109 rem ---------------------------------------
2110 rem Specify source directory but name is a file
2112 copy file1\ dir1\ >NUL 2>&1
2113 if errorlevel 1 echo Passed: errorlevel invalid check 1
2114 if not errorlevel 1 echo Failed: errorlevel invalid check 1
2115 call :CheckNotExist dir1\file1
2117 rem Overwrite same file
2119 copy file1 file1 >NUL 2>&1
2120 if errorlevel 1 echo Passed: errorlevel invalid check 2
2121 if not errorlevel 1 echo Failed: errorlevel invalid check 2
2123 rem Supply same file identified as a directory
2125 copy file1 file1\ >NUL 2>&1
2126 if errorlevel 1 echo Passed: errorlevel invalid check 3
2127 if not errorlevel 1 echo Failed: errorlevel invalid check 3
2132 echo ------------ Testing setlocal/endlocal ------------
2134 rem Note: setlocal EnableDelayedExpansion already tested in the variable delayed expansion test section
2135 mkdir foobar & cd foobar
2136 echo --- enable/disable extensions
2137 setlocal DisableEXTensions
2138 echo ErrLev: %ErrorLevel%
2140 echo ErrLev: %ErrorLevel%
2141 echo @echo off> tmp.cmd
2142 echo echo ErrLev: %%ErrorLevel%%>> tmp.cmd
2143 rem Enabled by default
2145 cmd /E:OfF /C tmp.cmd
2146 cmd /e:oN /C tmp.cmd
2148 rem FIXME: creating file before setting envvar value to prevent parsing-time evaluation (due to EnableDelayedExpansion not being implemented/available yet)
2149 echo --- setlocal with corresponding endlocal
2150 rem %CD% does not tork on NT4 so use the following workaround
2151 for /d %%i in (.) do set CURDIR=%%~dpnxi
2152 echo @echo off> test.cmd
2153 echo echo %%VAR%%>> test.cmd
2154 echo setlocal>> test.cmd
2155 echo set VAR=localval>> test.cmd
2156 echo md foobar2>> test.cmd
2157 echo cd foobar2>> test.cmd
2158 echo echo %%VAR%%>> test.cmd
2159 echo for /d %%%%i in (.) do echo %%%%~dpnxi>> test.cmd
2160 echo endlocal>> test.cmd
2161 echo echo %%VAR%%>> test.cmd
2162 echo for /d %%%%i in (.) do echo %%%%~dpnxi>> test.cmd
2166 for /d %%i in (.) do echo %%~dpnxi
2170 echo --- setlocal with no corresponding endlocal
2171 echo @echo off> test.cmd
2172 echo echo %%VAR%%>> test.cmd
2173 echo setlocal>> test.cmd
2174 echo set VAR=localval>> test.cmd
2175 echo md foobar2>> test.cmd
2176 echo cd foobar2>> test.cmd
2177 echo echo %%VAR%%>> test.cmd
2178 echo for /d %%%%i in (.) do echo %%%%~dpnxi>> test.cmd
2180 rem %CD% does not tork on NT4 so use the following workaround
2181 for /d %%i in (.) do set CURDIR=%%~dpnxi
2184 for /d %%i in (.) do echo %%~dpnxi
2188 echo --- setlocal within same batch program
2192 rem %CD% does not tork on NT4 so use the following workaround
2193 for /d %%i in (.) do set CURDIR=%%~dpnxi
2200 if "%var1%"=="one" echo Var1 ok 1
2201 if "%var2%"=="two" echo Var2 ok 2
2202 if "%var3%"=="three" echo Var3 ok 3
2203 for /d %%i in (.) do set curdir2=%%~dpnxi
2204 if "%curdir2%"=="%curdir%\foobar2" echo Directory is ok 1
2206 if "%var1%"=="one" echo Var1 ok 1
2207 if "%var2%"=="two" echo Var2 ok 2
2208 if "%var3%"=="" echo Var3 ok 3
2209 for /d %%i in (.) do set curdir2=%%~dpnxi
2210 if "%curdir2%"=="%curdir%\foobar2" echo Directory is ok 2
2212 if "%var1%"=="one" echo Var1 ok 1
2213 if "%var2%"=="" echo Var2 ok 2
2214 if "%var3%"=="" echo Var3 ok 3
2215 for /d %%i in (.) do set curdir2=%%~dpnxi
2216 if "%curdir2%"=="%curdir%" echo Directory is ok 3
2220 echo --- Mismatched set and end locals
2224 rem %CD% does not tork on NT4 so use the following workaround
2225 for /d %%i in (.) do set curdir=%%~dpnxi
2227 echo @echo off> 2set1end.cmd
2228 echo echo %%VAR%%>> 2set1end.cmd
2229 echo setlocal>> 2set1end.cmd
2230 echo set VAR=2set1endvalue1>> 2set1end.cmd
2231 echo cd ..\foodir3>> 2set1end.cmd
2232 echo setlocal>> 2set1end.cmd
2233 echo set VAR=2set1endvalue2>> 2set1end.cmd
2234 echo cd ..\foodir4>> 2set1end.cmd
2235 echo endlocal>> 2set1end.cmd
2236 echo echo %%VAR%%>> 2set1end.cmd
2237 echo for /d %%%%i in (.) do echo %%%%~dpnxi>> 2set1end.cmd
2239 echo @echo off> 1set2end.cmd
2240 echo echo %%VAR%%>> 1set2end.cmd
2241 echo setlocal>> 1set2end.cmd
2242 echo set VAR=1set2endvalue1>> 1set2end.cmd
2243 echo cd ..\foodir3>> 1set2end.cmd
2244 echo endlocal>> 1set2end.cmd
2245 echo echo %%VAR%%>> 1set2end.cmd
2246 echo for /d %%%%i in (.) do echo %%%%~dpnxi>> 1set2end.cmd
2247 echo endlocal>> 1set2end.cmd
2248 echo echo %%VAR%%>> 1set2end.cmd
2249 echo for /d %%%%i in (.) do echo %%%%~dpnxi>> 1set2end.cmd
2251 echo --- Extra setlocal in called batch
2253 rem -- setlocal1 == this batch, should never be used inside a called routine
2257 call %curdir%\2set1end.cmd
2260 for /d %%i in (.) do echo %%~dpnxi
2263 for /d %%i in (.) do echo %%~dpnxi
2266 echo --- Extra endlocal in called batch
2268 rem -- setlocal1 == this batch, should never be used inside a called routine
2272 call %curdir%\1set2end.cmd
2275 for /d %%i in (.) do echo %%~dpnxi
2278 for /d %%i in (.) do echo %%~dpnxi
2281 echo --- endlocal in called function rather than batch pgm is ineffective
2287 call :endlocalroutine
2291 goto :endlocalfinished
2309 cd .. & rd /q/s foobar
2311 echo ------------ Testing Errorlevel ------------
2312 rem WARNING: Do *not* add tests using ErrorLevel after this section
2313 should_not_exist 2> nul > nul
2315 rem nt 4.0 doesn't really support a way of setting errorlevel, so this is weak
2316 rem See http://www.robvanderwoude.com/exit.php
2319 if errorlevel 2 echo errorlevel too high, bad
2320 if errorlevel 1 echo errorlevel just right, good
2321 if errorlevel 01 echo errorlevel with leading zero just right, good
2322 if errorlevel -1 echo errorlevel with negative number OK
2323 if errorlevel 0x1 echo hexa should not be recognized!
2324 if errorlevel 1a echo invalid error level recognized!
2326 echo abc%ErrorLevel%def
2327 if errorlevel 1 echo errorlevel nonzero, bad
2328 if not errorlevel 1 echo errorlevel zero, good
2329 if not errorlevel 0x1 echo hexa should not be recognized!
2330 if not errorlevel 1a echo invalid error level recognized!
2331 rem Now verify that setting a real variable hides its magic variable
2333 echo %ErrorLevel% should be 7
2334 if errorlevel 7 echo setting var worked too well, bad
2336 echo %ErrorLevel% should still be 7
2338 echo ------------ Testing GOTO ------------
2341 echo goto with no leading space worked
2344 echo goto with a leading space worked
2347 echo goto with a leading tab worked
2350 echo goto with a following space worked
2352 echo ------------ Testing PATH ------------
2353 set backup_path=%path%
2360 set path=%backup_path%
2363 echo ------------ Testing combined CALLs/GOTOs ------------
2364 echo @echo off>foo.cmd
2365 echo goto :eof>>foot.cmd
2367 echo echo world>>foo.cmd
2369 echo @echo off>foot.cmd
2370 echo echo cheball>>foot.cmd
2372 echo call :bar>>foot.cmd
2373 echo if "%%1"=="deleteMe" (del foot.cmd)>>foot.cmd
2374 echo goto :eof>>foot.cmd
2377 echo echo barbare>>foot.cmd
2378 echo goto :eof>>foot.cmd
2384 rem Script execution stops after the following line
2407 echo Final message is not output since earlier 'foot' processing stops script execution
2408 echo Do NOT add any tests below this line
2410 echo ------------ Done, jumping to EOF -----------
2412 rem Subroutine to set errorlevel and return
2413 rem in windows nt 4.0, this always sets errorlevel 1, since /b isn't supported
2416 rem This line runs under cmd in windows NT 4, but not in more modern versions.