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
626 echo Testing case sensitivity with and without /i option
627 if bar==BAR echo if does not default to case sensitivity
628 if not bar==BAR echo if seems to default to case sensitivity
629 if /i foo==FOO echo if /i seems to work
630 if /i not foo==FOO echo if /i seems to be broken
631 if /I foo==FOO echo if /I seems to work
632 if /I not foo==FOO echo if /I seems to be broken
633 echo Testing string comparisons
634 if abc == abc (echo equal) else echo non equal
635 if abc =="abc
" (echo equal) else echo non equal
636 if "abc
"== abc (echo equal) else echo non equal
637 if "abc
"== "abc
" (echo equal) else echo non equal
638 echo Testing tabs handling
639 if@tab@1==1 echo doom
640 if @tab@1==1 echo doom
641 if 1==1 (echo doom) else@tab@echo quake
642 if@tab@not @tab@1==@tab@0 @tab@echo lol
643 if 1==0@tab@(echo doom) else echo quake
644 if 1==0 (echo doom)@tab@else echo quake
645 if 1==0 (echo doom) else@tab@echo quake
647 echo ------------ Testing for ------------
649 for %%i in (A B C) do echo %%i
650 for %%i in (A B C) do echo %%I
651 for %%i in (A B C) do echo %%j
652 for %%i in (A B C) do call :forTestFun1 %%i
653 for %%i in (1,4,1) do echo %%i
654 for %%i in (A, B,C) do echo %%i
655 for %%i in (X) do echo %%i
656 for@tab@%%i in (X2) do echo %%i
657 for %%i in@tab@(X3) do echo %%i
658 for %%i in (@tab@ foo@tab@) do echo %%i
659 for@tab@ %%i in@tab@(@tab@M) do echo %%i
660 for %%i@tab@in (X)@tab@do@tab@echo %%i
661 for@tab@ %%j in@tab@(@tab@M, N, O@tab@) do echo %%j
662 for %%i in (`echo A B`) do echo %%i
663 for %%i in ('echo A B') do echo %%i
664 for %%i in ("echo A B
") do echo %%i
665 for %%i in ("A B
" C) do echo %%i
671 echo --- imbricated FORs
678 for %%i in (A B) do (
679 for %%j in (C D) do (
681 for %%i in (A B) do (
682 for %%j in (C D) do (
683 call :forTestFun2 %%i %%j ))
689 mkdir foobar & cd foobar
694 echo --- basic wildcards
695 for %%i in (ba*) do echo %%i
697 for /d %%i in (baz foo bar) do echo %%i 2>&1
698 rem Confirm we don't match files:
699 for /d %%i in (bazb*) do echo %%i 2>&1
700 for /d %%i in (bazb2*) do echo %%i 2>&1
701 rem Show we pass through non wildcards
702 for /d %%i in (PASSED) do echo %%i
703 for /d %%i in (xxx) do (
704 echo %%i - Should be xxx
705 echo Expected second line
707 rem Show we issue no messages on failures
708 for /d %%i in (FAILED?) do echo %%i 2>&1
709 for /d %%i in (FAILED?) do (
710 echo %%i - Unexpected!
711 echo FAILED Unexpected second line
713 for /d %%i in (FAILED*) do echo %%i 2>&1
714 for /d %%i in (FAILED*) do (
715 echo %%i - Unexpected!
716 echo FAILED Unexpected second line
718 rem FIXME can't test wildcard expansion here since it's listed in directory
719 rem order, and not in alphabetic order.
720 rem Proper testing would need a currently missing "sort" program implementation.
721 rem for /d %%i in (ba*) do echo %%i>> tmp
724 rem for /d %%i in (?a*) do echo %%i>> tmp
727 rem for /d %%i in (*) do echo %%i>> tmp
735 call :WriteLine set found=N
736 for /l %%i in (1,1,%expectedresults%) do (
737 call :WriteLine if "%%%%expectedresults.
%%i%%%%"=="%%%%1" set found=Y
738 call :WriteLine if "%%%%found%%%%"=="Y
" set expectedresults.%%i=
739 call :WriteLine if "%%%%found%%%%"=="Y
" goto :eof
741 call :WriteLine echo Got unexpected result: "%%%%1"
750 for /l %%i in (1,1,%expectedresults%) do call :WriteLine if not "%%%%expectedresults.
%%i%%%%"=="" echo Found missing result: "%%%%expectedresults.
%%i%%%%"
756 rem %CD% does not tork on NT4 so use the following workaround
757 for /d %%i in (.) do set CURDIR=%%~dpnxi
760 echo Plain directory enumeration
761 set expectedresults=4
762 set expectedresults.1=%CURDIR%\.
763 set expectedresults.2=%CURDIR%\bar\.
764 set expectedresults.3=%CURDIR%\baz\.
765 set expectedresults.4=%CURDIR%\foo\.
767 for /R %%i in (.) do call temp.bat %%i
768 call :ValidateExpected
770 echo Plain directory enumeration from provided root
771 set expectedresults=4
772 set expectedresults.1=%CURDIR%\.
773 set expectedresults.2=%CURDIR%\bar\.
774 set expectedresults.3=%CURDIR%\baz\.
775 set expectedresults.4=%CURDIR%\foo\.
776 if "%CD%"=="" goto :SkipBrokenNT4
778 for /R "%CURDIR%" %%i in (.) do call temp.bat %%i
779 call :ValidateExpected
782 echo File enumeration
783 set expectedresults=2
784 set expectedresults.1=%CURDIR%\baz\bazbaz
785 set expectedresults.2=%CURDIR%\bazbaz
787 for /R %%i in (baz*) do call temp.bat %%i
788 call :ValidateExpected
790 echo File enumeration from provided root
791 set expectedresults=2
792 set expectedresults.1=%CURDIR%\baz\bazbaz
793 set expectedresults.2=%CURDIR%\bazbaz
795 for /R %%i in (baz*) do call temp.bat %%i
796 call :ValidateExpected
798 echo Mixed enumeration
799 set expectedresults=6
800 set expectedresults.1=%CURDIR%\.
801 set expectedresults.2=%CURDIR%\bar\.
802 set expectedresults.3=%CURDIR%\baz\.
803 set expectedresults.4=%CURDIR%\baz\bazbaz
804 set expectedresults.5=%CURDIR%\bazbaz
805 set expectedresults.6=%CURDIR%\foo\.
807 for /R %%i in (. baz*) do call temp.bat %%i
808 call :ValidateExpected
810 echo Mixed enumeration from provided root
811 set expectedresults=6
812 set expectedresults.1=%CURDIR%\.
813 set expectedresults.2=%CURDIR%\bar\.
814 set expectedresults.3=%CURDIR%\baz\.
815 set expectedresults.4=%CURDIR%\baz\bazbaz
816 set expectedresults.5=%CURDIR%\bazbaz
817 set expectedresults.6=%CURDIR%\foo\.
819 for /R %%i in (. baz*) do call temp.bat %%i
820 call :ValidateExpected
822 echo With duplicates enumeration
823 set expectedresults=12
824 set expectedresults.1=%CURDIR%\bar\bazbaz
825 set expectedresults.2=%CURDIR%\bar\fred
826 set expectedresults.3=%CURDIR%\baz\bazbaz
827 set expectedresults.4=%CURDIR%\baz\bazbaz
828 set expectedresults.5=%CURDIR%\baz\bazbaz
829 set expectedresults.6=%CURDIR%\baz\fred
830 set expectedresults.7=%CURDIR%\bazbaz
831 set expectedresults.8=%CURDIR%\bazbaz
832 set expectedresults.9=%CURDIR%\bazbaz
833 set expectedresults.10=%CURDIR%\foo\bazbaz
834 set expectedresults.11=%CURDIR%\foo\fred
835 set expectedresults.12=%CURDIR%\fred
837 for /R %%i in (baz* bazbaz fred ba*) do call temp.bat %%i
838 call :ValidateExpected
840 echo Strip missing wildcards, keep unwildcarded names
841 set expectedresults=6
842 set expectedresults.1=%CURDIR%\bar\jim
843 set expectedresults.2=%CURDIR%\baz\bazbaz
844 set expectedresults.3=%CURDIR%\baz\jim
845 set expectedresults.4=%CURDIR%\bazbaz
846 set expectedresults.5=%CURDIR%\foo\jim
847 set expectedresults.6=%CURDIR%\jim
849 for /R %%i in (baz* fred* jim) do call temp.bat %%i
850 call :ValidateExpected
853 cd .. & rd /s/Q foobar
855 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
856 for /L %%i in (1,2,0) do echo %%i
857 for@tab@/L %%i in (1,2,0) do echo %%i
858 for /L %%i in (1,2,6) do echo %%i
859 for /l %%i in (1 ,2,6) do echo %%i
860 for /L %%i in (a,2,3) do echo %%i
861 for /L %%i in (1,2,-1) do echo %%i
862 for /L %%i in (-4,-1,-1) do echo %%i
863 for /L %%i in (1,-2,-2) do echo %%i
864 for /L %%i in (1,2,a) do echo %%i
865 echo ErrorLevel %ErrorLevel%
866 for /L %%i in (1,a,b) do echo %%i
867 echo ErrorLevel %ErrorLevel%
869 for /l %%i in (1,1,4) do echo %%i
870 for /l %%i in (1,2,4) do echo %%i
871 for /l %%i in (4,-1,1) do echo %%i
872 for /l %%i in (4,-2,1) do echo %%i
873 for /l %%i in (1,-1,4) do echo %%i
874 for /l %%i in (4,1,1) do echo %%i
875 for /L %%i in (a,2,b) do echo %%i
876 for /L %%i in (1,1,1) do echo %%i
877 for /L %%i in (1,-2,-1) do echo %%i
878 for /L %%i in (-1,-1,-1) do echo %%i
879 for /L %%i in (1,2, 3) do echo %%i
880 rem Test zero iteration skips the body of the for
881 for /L %%i in (2,2,1) do (
886 rem No output when using "set expr
" syntax, unless in interactive mode
887 rem Need to use "set envvar
=expr
" to use in a batch script
888 echo ------ individual operations
890 set /a var=1 +2 & echo %var%
891 set /a var=1 +-2 & echo %var%
892 set /a var=1 --2 & echo %var%
893 set /a var=2* 3 & echo %var%
894 set /a var=-2* -5 & echo %var%
895 set /a var=12/3 & echo %var%
896 set /a var=13/3 & echo %var%
897 set /a var=-13/3 & echo %var%
898 rem FIXME Divide by zero should return an error, but error messages cannot be tested with current infrastructure
899 set /a var=5 %% 5 & echo %var%
900 set /a var=5 %% 3 & echo %var%
901 set /a var=5 %% -3 & echo %var%
902 set /a var=-5 %% -3 & echo %var%
903 set /a var=1 ^<^< 0 & echo %var%
904 set /a var=1 ^<^< 2 & echo %var%
905 set /a var=1 ^<^< -2 & echo %var%
906 set /a var=-1 ^<^< -2 & echo %var%
907 set /a var=-1 ^<^< 2 & echo %var%
908 set /a var=9 ^>^> 0 & echo %var%
909 set /a var=9 ^>^> 2 & echo %var%
910 set /a var=9 ^>^> -2 & echo %var%
911 set /a var=-9 ^>^> -2 & echo %var%
912 set /a var=-9 ^>^> 2 & echo %var%
913 set /a var=5 ^& 0 & echo %var%
914 set /a var=5 ^& 1 & echo %var%
915 set /a var=5 ^& 3 & echo %var%
916 set /a var=5 ^& 4 & echo %var%
917 set /a var=5 ^& 1 & echo %var%
918 set /a var=5 ^| 0 & echo %var%
919 set /a var=5 ^| 1 & echo %var%
920 set /a var=5 ^| 3 & echo %var%
921 set /a var=5 ^| 4 & echo %var%
922 set /a var=5 ^| 1 & echo %var%
923 set /a var=5 ^^ 0 & echo %var%
924 set /a var=5 ^^ 1 & echo %var%
925 set /a var=5 ^^ 3 & echo %var%
926 set /a var=5 ^^ 4 & echo %var%
927 set /a var=5 ^^ 1 & echo %var%
928 echo ------ precedence and grouping
929 set /a var=4 + 2*3 & echo %var%
930 set /a var=(4+2)*3 & echo %var%
931 set /a var=4 * 3/5 & echo %var%
932 set /a var=(4 * 3)/5 & echo %var%
933 set /a var=4 * 5 %% 4 & echo %var%
934 set /a var=4 * (5 %% 4) & echo %var%
935 set /a var=3 %% (5 + 8 %% 3 ^^ 2) & echo %var%
936 set /a var=3 %% (5 + 8 %% 3 ^^ -2) & echo %var%
937 echo ------ octal and hexadecimal
938 set /a var=0xf + 3 & echo %var%
939 set /a var=0xF + 3 & echo %var%
940 set /a var=015 + 2 & echo %var%
941 set /a var=3, 8+3,0 & echo %var%
942 echo ------ variables
943 set /a var=foo=3, foo+1 & echo %var%
944 if defined foo (echo %foo%) else (
947 set /a var=foo=3, foo+=1 & echo %var%
948 set /a var=foo=3, bar=1, bar+=foo, bar & echo %var%
949 set /a var=foo*= foo & echo %var%
950 set /a var=whateverNonExistingVar & echo %var%
951 set /a var=whateverNonExistingVar + bar & echo %var%
952 set /a var=foo -= foo + 7 & echo %var%
953 set /a var=foo /= 3 + 2 & echo %var%
954 set /a var=foo=5, foo %%=2 & echo %var%
955 set /a var=foo ^<^<= 2 & echo %var%
956 set /a var=foo ^>^>= 2 & echo %var%
957 set /a var=foo ^&= 2 & echo %var%
958 set /a var=foo=5, foo ^|= 2 & echo %var%
959 set /a var=foo=5, foo ^^= 2 & echo %var%
960 set /a var=foo=19, foo %%= 4 + (bar %%= 7) & echo.
965 mkdir foobar & cd foobar
966 echo ------ string argument
967 for /F %%i in ("a b c
") do echo %%i
968 for /f %%i in ("a
") do echo %%i
969 for /f %%i in ("a
") do echo %%i
970 fOr /f %%i in (" a
") do echo %%i
971 for /f %%i in (" a
") do echo %%i
972 echo ------ fileset argument
973 echo --------- basic blank handling
975 for /f %%i in (foo) do echo %%i
977 for /f %%i in (foo) do echo %%i
979 for /f %%i in (foo) do echo %%i
981 for /f %%i in (foo) do echo %%i
983 for /f %%i in (foo) do echo %%i
985 for /f %%i in (foo) do echo %%i
988 for /f %%i in (foo) do echo %%i
989 echo --------- multi-line with empty lines
996 for /f %%b in (foo) do echo %%b
997 echo --------- multiple files
1001 for /f %%k in (foo bar) do echo %%k
1002 for /f %%k in (bar foo) do echo %%k
1003 rem echo ------ command argument
1004 rem Not implemented on NT4
1005 rem FIXME: Not testable right now in wine: not implemented and would need
1006 rem preliminary grep-like program implementation (e.g. like findstr or fc) even
1007 rem for a simple todo_wine test
1008 rem (for /f "usebackq
" %%i in (`echo z a b`) do echo %%i) || echo not supported
1009 rem (for /f usebackq %%i in (`echo z a b`) do echo %%i) || echo not supported
1010 echo ------ eol option
1011 for /f "eol
=@
" %%i in (" ad
") do echo %%i
1012 for /f "eol
=@
" %%i in (" z@y
") do echo %%i
1013 for /f "eol
=|
" %%i in ("a|d
") do echo %%i
1014 for /f "eol
=@
" %%i in ("@y
") do echo %%i > output_file
1015 if not exist output_file (echo no output) else (del output_file)
1016 for /f "eol
==" %%i in ("=y
") do echo %%i > output_file
1017 if not exist output_file (echo no output) else (del output_file)
1018 echo ------ delims option
1019 for /f "delims
=|
" %%i in ("a|d
") do echo %%i
1020 for /f "delims
=|
" %%i in ("a |d
") do echo %%i
1021 for /f "delims
=|
" %%i in ("a d|
") do echo %%i
1022 for /f "delims
=|
" %%i in ("a d|
") do echo %%i
1023 for /f "delims
==" %%i in ("C r
=d|
") do echo %%i
1024 for /f "delims
=" %%i in ("foo bar baz
") do echo %%i
1025 for /f "delims
=" %%i in ("c
:\foo bar baz\..
") do echo %%~fi
1026 echo ------ skip option
1030 for /f "skip
=2" %%i in (foo) do echo %%i
1031 for /f "skip
=3" %%i in (foo) do echo %%i > output_file
1032 if not exist output_file (echo no output) else (del output_file)
1033 for /f "skip
=4" %%i in (foo) do echo %%i > output_file
1034 if not exist output_file (echo no output) else (del output_file)
1038 echo ------------ Testing del /a ------------
1039 del /f/q *.test > nul
1042 echo not-r > not-r.test
1044 if not exist not-r.test echo not-r.test not found before delete, bad
1046 if not exist not-r.test echo not-r.test not found after delete, good
1048 if not exist r.test echo r.test not found before delete, bad
1049 if exist r.test echo r.test found before delete, good
1051 if not exist r.test echo r.test not found after delete, good
1052 if exist r.test echo r.test found after delete, bad
1054 echo ------------ Testing del /q ------------
1058 echo abc > file2.dat
1059 rem If /q doesn't work, cmd will prompt and the test case should hang
1061 for %%a in (1 2.dat) do if exist file%%a echo del /q * failed on file%%a
1062 for %%a in (1 2.dat) do if not exist file%%a echo del /q * succeeded on file%%a
1066 echo ------------ Testing del /s ------------
1071 echo there > file2.dat
1072 echo bub > file3.dat
1073 echo bye > "file with spaces.dat
"
1075 del /s file1.dat > nul
1076 del file2.dat /s > nul
1077 del "file3.dat
" /s > nul
1078 del "file with spaces.dat
" /s > nul
1080 for %%f in (1 2 3) do if exist file%%f.dat echo Del /s failed on file%%f
1081 for %%f in (1 2 3) do if exist file%%f.dat del file%%f.dat
1082 if exist "file with spaces.dat
" echo Del /s failed on "file with spaces.dat
"
1083 if exist "file with spaces.dat
" del "file with spaces.dat
"
1088 echo ------------ Testing rename ------------
1089 mkdir foobar & cd foobar
1090 echo --- ren and rename are synonymous
1093 if exist foo echo foo should be renamed!
1094 if exist bar echo foo renamed to bar
1096 if exist bar echo bar should be renamed!
1097 if exist foo echo bar renamed to foo
1098 echo --- name collision
1108 echo --- rename read-only files
1112 if not exist file1 (
1114 echo read-only file renamed
1117 echo read-only file not renamed!
1119 echo --- rename directories
1131 echo read-only dir renamed
1134 echo --- rename in other directory
1135 if not exist baz\abc (
1136 echo rename impossible in other directory
1137 if exist foo echo original file still present
1139 echo shouldn't rename in other directory!
1140 if not exist foo echo original file not present anymore
1142 cd .. & rd /s/q foobar
1144 echo ------------ Testing move ------------
1145 mkdir foobar & cd foobar
1148 move foo bar > nul 2>&1
1151 echo file move succeeded
1156 move /Y bar baz > nul 2>&1
1159 echo file move with overwrite succeeded
1162 echo file overwrite impossible!
1168 move baz bazro > nul 2>&1
1171 echo read-only files are moveable
1172 move bazro baz > nul 2>&1
1175 echo read-only file not moved!
1179 move baz rep > nul 2>&1
1182 echo file moved in subdirectory
1186 move rep\baz . > nul 2>&1
1187 move /Y baz baz > nul 2>&1
1189 echo moving a file to itself should be a no-op!
1191 echo moving a file to itself is a no-op
1193 echo ErrorLevel: %ErrorLevel%
1196 echo --- directory move
1200 move baz foo\bar > nul 2>&1
1202 if exist foo\bar\baz\baz2 (
1203 echo simple directory move succeeded
1208 move baz baz > nul 2>&1
1209 echo moving a directory to itself gives error; errlevel %ErrorLevel%
1210 echo ------ dir in dir move
1215 move foo bar > nul 2>&1
1223 cd .. & rd /s/q foobar
1225 echo ------------ Testing mkdir ------------
1227 echo --- md and mkdir are synonymous
1234 echo --- creating an already existing directory/file must fail
1243 echo --- multilevel path creation
1255 mkdir ..\..\..\foo\bar2
1258 mkdir ..\..\..\foo\bar2
1260 rmdir ..\..\..\foo\bar2
1268 echo --- trailing backslashes
1271 if exist foo (rmdir foo & echo dir created
1272 ) else ( echo dir not created )
1274 echo --- invalid chars
1276 echo mkdir ? gives errorlevel %ErrorLevel%
1279 echo mkdir ?\foo gives errorlevel %ErrorLevel%
1282 echo mkdir foo\? gives errorlevel %ErrorLevel%
1283 if exist foo (rmdir foo & echo ok, foo created
1284 ) else ( echo foo not created )
1287 echo mkdir foo\bar\? gives errorlevel %ErrorLevel%
1290 echo bad, foo not created
1294 echo ok, foo\bar created
1300 echo --- multiple directories at once
1301 mkdir foobaz & cd foobaz
1302 mkdir foo bar\baz foobar "bazbaz
" .\"zabzab"
1303 if exist foo
(echo foo created
) else echo foo
not created
!
1304 if exist bar
(echo bar created
) else echo bar
not created
!
1305 if exist foobar
(echo foobar created
) else echo foobar
not created
!
1306 if exist bar
\baz
(echo bar
\baz created
) else echo bar
\baz
not created
!
1307 if exist bazbaz
(echo bazbaz created
) else echo bazbaz
not created
!
1308 if exist zabzab
(echo zabzab created
) else echo zabzab
not created
!
1309 cd ..
& rd /s
/q foobaz
1312 echo mkdir foo\
* errorlevel %ErrorLevel%
1313 if exist foo
(rmdir foo
& echo ok
, foo created
1314 ) else ( echo bad
, foo
not created
)
1316 echo ------------ Testing
rmdir ------------
1318 rem rd and rmdir are synonymous
1322 if not exist foobar
echo dir removed
1326 if not exist foobar
echo dir removed
1327 rem Removing nonexistent directory
1330 rem Removing single-level directories
1334 if exist foo
echo file
not removed
1340 if exist foo
echo non
-empty
dir not removed
1345 if exist foo
echo non
-empty
dir not removed
1352 echo recursive
rmdir succeeded
1359 echo foo
> foo
\bar
\brol
1360 rmdir /s
/Q foo
2>&1
1362 echo recursive
rmdir succeeded
1369 rem multiples directories at once
1370 mkdir foobaz
& cd foobaz
1374 rd /s
/q foo bar foobar
1375 if not exist foo
(echo foo removed
) else echo foo
not removed
!
1376 if not exist bar
(echo bar removed
) else echo bar
not removed
!
1377 if not exist foobar
(echo foobar removed
) else echo foobar
not removed
!
1378 if not exist bar
\baz
(echo bar
\baz removed
) else echo bar
\baz
not removed
!
1379 cd ..
& rd /s
/q foobaz
1381 echo ------------ Testing pushd
/popd
------------
1383 echo --- popd is no
-op
when dir stack is empty
1386 echo --- pushing non
-existing
dir
1389 echo --- basic behaviour
1414 echo ------------ Testing
attrib ------------
1415 rem FIXME Add tests for archive, hidden and system attributes + mixed attributes modifications
1416 mkdir foobar
& cd foobar
1417 echo foo original contents
> foo
1420 echo --- read
-only attribute
1421 rem Read-only files cannot be altered or deleted, unless forced
1429 echo Read
-only file
not deleted
1431 echo Should
not delete read
-only file
!
1435 echo Read
-only file forcibly deleted
1437 echo Should delete read
-only file with
del /F
!
1441 cd ..
& rd /s
/q foobar
1442 echo --- recursive behaviour
1443 mkdir foobar
\baz
& cd foobar
1449 attrib +R l
*vel?
/S
> nul
2>&1
1455 cd ..
& rd /s
/q foobar
1456 echo --- folders processing
1462 attrib +r baz
/s
/d
> nul
2>&1
1468 rem Oddly windows allows file creation in a read-only directory...
1469 if exist baz\lala
(echo file created in read
-only
dir) else echo file
not created
1470 cd ..
& rd /s
/q foobar
1472 echo ------------ Testing assoc
------------
1473 rem FIXME Can't test error messages in the current test system, so we have to use some kludges
1474 rem FIXME Revise once || conditional execution is fixed
1475 mkdir foobar
& cd foobar
1476 echo --- setting association
1484 rem association set system-wide
1485 echo @
echo off
> tmp.
cmd
1486 echo echo +++>> tmp.
cmd
1487 echo assoc .foo
>> tmp.
cmd
1490 echo --- resetting association
1496 rem association removal set system-wide
1497 cmd /c tmp.
cmd > baz
1500 cd ..
& rd /s
/q foobar
1502 echo ------------ Testing ftype
------------
1503 rem FIXME Can't test error messages in the current test system, so we have to use some kludges
1504 rem FIXME Revise once || conditional execution is fixed
1505 mkdir foobar
& cd foobar
1506 echo --- setting association
1511 ftype footype
=foo_opencmd
1515 rem association set system-wide
1516 echo @
echo off
> tmp.
cmd
1517 echo echo +++>> tmp.
cmd
1518 echo ftype footype
>> tmp.
cmd
1521 echo --- resetting association
1524 rem Removing a file type association doesn't work on XP due to a bug, so a workaround is needed
1525 setlocal EnableDelayedExpansion
1526 set FOO
=original value
1529 for /F
%%i in
('type baz') do (set FOO
=buggyXP
)
1530 rem Resetting actually works on wine/NT4, but is reported as failing due to the peculiar test (and non-support for EnabledDelayedExpansion)
1531 rem FIXME Revisit once a grep-like program like ftype is implemented
1532 rem (e.g. to check baz's size using dir /b instead)
1535 rem cleanup registry
1536 echo REGEDIT4
> regCleanup.reg
1537 echo.
>> regCleanup.reg
1538 echo [-HKEY_CLASSES_ROOT
\footype
]>> regCleanup.reg
1539 regedit
/s regCleanup.reg
1542 cd ..
& rd /s
/q foobar
1544 echo ------------ Testing
CALL ------------
1545 mkdir foobar
& cd foobar
1546 echo --- external script
1547 echo echo foo
%%1> foo.
cmd
1550 echo echo %%1 %%2 > foo.
cmd
1552 call foo.
cmd foo bar
1559 echo --- internal routines
1560 call :testRoutine :testRoutine
1561 goto :endTestRoutine
1567 call :testRoutineArgs foo
1568 call :testRoutineArgs foo bar
1569 call :testRoutineArgs foo
""
1570 call :testRoutineArgs "" bar
1571 call :testRoutineArgs foo
''
1572 call :testRoutineArgs '' bar
1573 goto :endTestRoutineArgs
1579 echo --- with builtins
1582 if exist foo
(echo foo created
) else echo foo should
exist!
1584 set FOOBAZ_VAR
=foobaz
1585 call echo Should expand
%FOOBAZ_VAR%
1590 if 1==1 call del batfile
1592 if exist batfile
echo batfile shouldn
't exist
1593 rem ... but not for 'if' or 'for'
1594 call if 1==1 echo bar 2> nul
1597 call for %%i in (foo bar baz) do echo %%i 2> nul
1599 rem First look for programs in the path before trying a builtin
1600 echo echo non-builtin dir> dir.cmd
1603 rem The below line equates to call (, which does nothing, then the
1604 rem subsequent lines are executed.
1609 rem The below line equates to call if, which always fails, then the
1610 rem subsequent lines are executed. Note cmd.exe swallows all lines
1617 call call call echo passed
1618 cd .. & rd /s/q foobar
1620 echo ------------ Testing SHIFT ------------
1622 call :shiftFun p1 p2 p3 p4 p5
1626 echo '%1' '%2' '%3' '%4' '%5'
1628 echo '%1' '%2' '%3' '%4' '%5'
1630 echo '%1' '%2' '%3' '%4' '%5'
1632 echo '%1' '%2' '%3' '%4' '%5'
1634 echo '%1' '%2' '%3' '%4' '%5'
1636 echo '%1' '%2' '%3' '%4' '%5'
1640 echo ------------ Testing cmd invocation ------------
1641 rem FIXME: only a stub ATM
1642 echo --- a batch file can delete itself
1643 echo del foo.cmd>foo.cmd
1645 if not exist foo.cmd (
1646 echo file correctly deleted
1648 echo file should be deleted!
1651 echo --- a batch file can alter itself
1652 echo echo bar^>foo.cmd>foo.cmd
1653 cmd /q /c foo.cmd > NUL 2>&1
1658 echo file not created!
1661 echo ---------- Testing copy
1664 rem Note echo adds 0x0d 0x0a on the end of the line in the file
1667 echo CCCCCCCCC> file3
1673 echo Passed: Found expected %1
1675 echo Failed: Did not find expected %1
1677 del /q "%1" >nul 2>&1
1679 if not "%1"=="" goto :CheckExist
1684 echo Passed: Did not find %1
1686 echo Failed: Unexpectedly found %1
1687 del /q "%1" >nul 2>&1
1690 if not "%1"=="" goto :CheckNotExist
1693 rem Note: No way to check file size on NT4 so skip the test
1696 echo Failed: File missing when requested filesize check [%2]
1697 goto :ContinueFileSizeChecks
1699 for %%i in (%1) do set filesize=%%~zi
1700 if "%filesize%"=="%2" (
1701 echo Passed: file size check on %1 [%filesize%]
1703 if "%filesize%"=="%%~zi" (
1704 echo Skipping file size check on NT4
1706 echo Failed: file size check on %1 [%filesize% != %2]
1709 :ContinueFileSizeChecks
1712 if not "%1"=="" goto :CheckFileSize
1717 rem -----------------------
1718 rem Simple single file copy
1719 rem -----------------------
1720 rem Simple single file copy, normally used syntax
1721 copy file1 dummy.file >nul 2>&1
1722 if errorlevel 1 echo Incorrect errorlevel
1723 call :CheckExist dummy.file
1725 rem Simple single file copy, destination supplied as two forms of directory
1726 copy file1 dir1 >nul 2>&1
1727 if errorlevel 1 echo Incorrect errorlevel
1728 call :CheckExist dir1\file1
1730 copy file1 dir1\ >nul 2>&1
1731 if errorlevel 1 echo Incorrect errorlevel
1732 call :CheckExist dir1\file1
1734 rem Simple single file copy, destination supplied as fully qualified destination
1735 copy file1 dir1\file99 >nul 2>&1
1736 if errorlevel 1 echo Incorrect errorlevel
1737 call :CheckExist dir1\file99
1739 rem Simple single file copy, destination not supplied
1741 copy ..\file1 >nul 2>&1
1742 if errorlevel 1 echo Incorrect errorlevel
1743 call :CheckExist file1
1746 rem Simple single file copy, destination supplied as nonexistent directory
1747 copy file1 dir2\ >nul 2>&1
1748 if not errorlevel 1 echo Incorrect errorlevel
1749 call :CheckNotExist dir2 dir2\file1
1751 rem -----------------------
1753 rem -----------------------
1754 rem Simple single file copy, destination supplied as two forms of directory
1755 copy file? dir1 >nul 2>&1
1756 if errorlevel 1 echo Incorrect errorlevel
1757 call :CheckExist dir1\file1 dir1\file2 dir1\file3
1759 copy file* dir1\ >nul 2>&1
1760 if errorlevel 1 echo Incorrect errorlevel
1761 call :CheckExist dir1\file1 dir1\file2 dir1\file3
1763 rem Simple single file copy, destination not supplied
1765 copy ..\file*.* >nul 2>&1
1766 if errorlevel 1 echo Incorrect errorlevel
1767 call :CheckExist file1 file2 file3
1770 rem Simple wildcarded file copy, destination supplied as nonexistent directory
1771 copy file? dir2\ >nul 2>&1
1772 if not errorlevel 1 echo Incorrect errorlevel
1773 call :CheckNotExist dir2 dir2\file1 dir2\file2 dir2\file3
1775 rem ------------------------------------------------
1776 rem Confirm overwrite works (cannot test prompting!)
1777 rem ------------------------------------------------
1778 copy file1 testfile >nul 2>&1
1779 copy /y file2 testfile >nul 2>&1
1780 call :CheckExist testfile
1782 rem ------------------------------------------------
1783 rem Test concatenation
1784 rem ------------------------------------------------
1785 rem simple case, no wildcards
1786 copy file1+file2 testfile >nul 2>&1
1787 if errorlevel 1 echo Incorrect errorlevel
1788 call :CheckExist testfile
1790 rem simple case, wildcards, no concatenation
1791 copy file* testfile >nul 2>&1
1792 if errorlevel 1 echo Incorrect errorlevel
1793 call :CheckExist testfile
1795 rem simple case, wildcards, and concatenation
1797 copy file*+fred testfile >nul 2>&1
1798 if errorlevel 1 echo Incorrect errorlevel
1799 call :CheckExist testfile
1801 rem simple case, wildcards, and concatenation
1802 copy fred+file* testfile >nul 2>&1
1803 if errorlevel 1 echo Incorrect errorlevel
1804 call :CheckExist testfile
1806 rem Calculate destination name
1807 copy fred+file* dir1 >nul 2>&1
1808 if errorlevel 1 echo Incorrect errorlevel
1809 call :CheckExist dir1\fred
1811 rem Calculate destination name
1812 copy fred+file* dir1\ >nul 2>&1
1813 if errorlevel 1 echo Incorrect errorlevel
1814 call :CheckExist dir1\fred
1816 rem Calculate destination name (none supplied)
1818 copy ..\fred+..\file* >nul 2>&1
1819 if errorlevel 1 echo Incorrect errorlevel
1820 call :CheckExist fred
1822 copy ..\fr*+..\file1 >nul 2>&1
1823 if errorlevel 1 echo Incorrect errorlevel
1824 call :CheckExist fred
1827 rem ******************************************************************
1828 rem ASCII and BINARY tests
1829 rem Note: hard coded numbers deliberate because need to ensure whether
1830 rem an additional EOF has been added or not. There is no way to handle
1831 rem EOFs in batch, so assume if a single byte appears, its an EOF!
1832 rem ******************************************************************
1834 rem Confirm original sizes of file1,2,3
1835 call :CheckFileSize file1 5 file2 8 file3 11
1839 rem ----------------------------------------------
1840 rem Show concatenation defaults copy to ascii mode
1841 rem ----------------------------------------------
1842 rem Simple default copy source to destination (should not append EOF 5)
1843 copy ..\file1 file1_default >nul 2>&1
1844 call :CheckFileSize file1_default 5
1846 rem Simple binary copy source to destination (should not append EOF 5)
1847 copy /b ..\file1 file1_default2 >nul 2>&1
1848 call :CheckFileSize file1_default2 5
1850 rem Simple ascii copy source to destination (should append EOF 5+1, 8+1, 11+1)
1851 copy /a ..\file1 file1_plus_eof >nul 2>&1
1852 call :CheckFileSize file1_plus_eof 6
1853 copy /a ..\file2 file2_plus_eof >nul 2>&1
1854 call :CheckFileSize file2_plus_eof 9
1855 copy /a ..\file3 file3_plus_eof >nul 2>&1
1856 call :CheckFileSize file3_plus_eof 12
1858 rem Concat 2 files, ascii mode - (only one EOF on the end 5+8+1)
1859 copy /a ..\file1+..\file2 file12_plus_eof >nul 2>&1
1860 call :CheckFileSize file12_plus_eof 14
1862 rem Concat 2 files, binary mode - (no EOF on the end 5+8)
1863 copy /b ..\file1+..\file2 file12_no_eof >nul 2>&1
1864 call :CheckFileSize file12_no_eof 13
1866 rem Concat 2 files, default mode - (one EOF on the end 5+8+1)
1867 copy ..\file1+..\file2 file12_eof2 >nul 2>&1
1868 call :CheckFileSize file12_eof2 14
1870 rem --------------------------------------------------------------
1871 rem Show ascii source copy stops at first EOF, binary does the lot
1872 rem --------------------------------------------------------------
1873 copy file1_plus_eof /b file1_binary_srccopy /b >nul 2>&1
1874 call :CheckFileSize file1_binary_srccopy 6
1876 copy file1_plus_eof /a file1_ascii_srccopy /b >nul 2>&1
1877 call :CheckFileSize file1_ascii_srccopy 5
1879 rem --------------------------------------------------------------
1880 rem Show results of concatenating files (ending in EOFs) and /a /b
1881 rem --------------------------------------------------------------
1883 rem Default and ascii copy reads as ascii, stripping EOFs, so 6-1 + 9-1 + 12-1 + 1
1884 copy file1_plus_eof+file2_plus_eof+file3_plus_eof file123_default_copy >nul 2>&1
1885 call :CheckFileSize file123_default_copy 25
1886 copy /a file1_plus_eof+file2_plus_eof+file3_plus_eof file123_ascii_copy >nul 2>&1
1887 call :CheckFileSize file123_ascii_copy 25
1889 rem In binary mode, we get 3 eofs, so 6 + 9 + 12 = 27
1890 copy /b file1_plus_eof + file2_plus_eof + file3_plus_eof file123_binary_copy >nul 2>&1
1891 call :CheckFileSize file123_binary_copy 27
1893 rem We can select which we want the eofs from by postfixing it with /a or /b
1894 rem so here have first and third with eof, second as ascii 6 + 9-1 + 12
1895 copy file1_plus_eof /b + file2_plus_eof /a + file3_plus_eof /b file123_mixed_copy1 >nul 2>&1
1896 call :CheckFileSize file123_mixed_copy1 26
1898 rem By postfixing the destination with /a, we ask for an ascii destination which appends EOF
1899 rem so here have first and third with eof, second as ascii 6 + 9-1 + 12 + extra EOF
1900 rem Note the delta between this and the previous one also shows that the destination
1901 rem ascii/binary is inherited from the last /a or /b on the line
1902 copy file1_plus_eof /b + file2_plus_eof /a + file3_plus_eof /b file123_mixed_copy2 /a >nul 2>&1
1903 call :CheckFileSize file123_mixed_copy2 27
1905 rem so here have second with eof, first and third as ascii 6-1 + 9 + 12-1
1906 rem Note the delta between the next two also shows that the destination ascii/binary is
1907 rem inherited from the last /a or /b on the line, so the first has an extra EOF
1908 copy file1_plus_eof /a + file2_plus_eof /b + file3_plus_eof /a file123_mixed_copy3 >nul 2>&1
1909 call :CheckFileSize file123_mixed_copy3 26
1910 copy file1_plus_eof /a + file2_plus_eof /b + file3_plus_eof /a file123_mixed_copy4 /b >nul 2>&1
1911 call :CheckFileSize file123_mixed_copy4 25
1913 rem -------------------------------------------------------------------------------------------
1914 rem This shows when concatenating, an ascii destination always adds on an EOF but when we
1915 rem are not concatenating, its a direct copy regardless of destination if being read as binary
1916 rem -------------------------------------------------------------------------------------------
1918 rem All 3 have eof's
, plus an extra
= 6 + 9 + 12 + eof
1919 copy /b file1_plus_eof
+ file2_plus_eof
+ file3_plus_eof file123_mixed_copy5
/a
>nul
2>&1
1920 call :CheckFileSize file123_mixed_copy5
28
1922 rem All 2 have eof's, plus an extra = 6 + 12 + eof
1923 copy /b file1_plus_eof
+ file3_plus_eof file123_mixed_copy6
/a
>nul
2>&1
1924 call :CheckFileSize file123_mixed_copy6
19
1926 rem One file has EOF, but doesn't get an extra one, i.e. 6
1927 copy /b file1_plus_eof file123_mixed_copy7
/a
>nul
2>&1
1928 call :CheckFileSize file123_mixed_copy7
6
1930 rem Syntax means concatenate so ascii destination kicks in
1931 copy /b file1_plus_eof
* file123_mixed_copy8
/a
>nul
2>&1
1932 call :CheckFileSize file123_mixed_copy8
7
1937 rem ---------------------------------------
1938 rem Error combinations
1939 rem ---------------------------------------
1940 rem Specify source directory but name is a file
1942 copy file1\ dir1\
>NUL
2>&1
1943 if errorlevel 1 echo Passed
: errorlevel invalid check
1
1944 if not errorlevel 1 echo Failed
: errorlevel invalid check
1
1945 call :CheckNotExist dir1
\file
1
1947 rem Overwrite same file
1949 copy file1 file1
>NUL
2>&1
1950 if errorlevel 1 echo Passed
: errorlevel invalid check
2
1951 if not errorlevel 1 echo Failed
: errorlevel invalid check
2
1953 rem Supply same file identified as a directory
1955 copy file1 file1\
>NUL
2>&1
1956 if errorlevel 1 echo Passed
: errorlevel invalid check
3
1957 if not errorlevel 1 echo Failed
: errorlevel invalid check
3
1962 echo ------------ Testing
setlocal/endlocal ------------
1964 rem Note: setlocal EnableDelayedExpansion already tested in the variable delayed expansion test section
1965 mkdir foobar
& cd foobar
1966 echo --- enable
/disable extensions
1967 setlocal DisableEXTensions
1968 echo ErrLev
: %ErrorLevel%
1970 echo ErrLev
: %ErrorLevel%
1971 echo @
echo off
> tmp.
cmd
1972 echo echo ErrLev
: %%ErrorLevel%%>> tmp.
cmd
1973 rem Enabled by default
1975 cmd /E
:OfF /C tmp.
cmd
1976 cmd /e
:oN /C tmp.
cmd
1978 rem FIXME: creating file before setting envvar value to prevent parsing-time evaluation (due to EnableDelayedExpansion not being implemented/available yet)
1979 echo --- setlocal with corresponding
endlocal
1980 rem %CD% does not tork on NT4 so use the following workaround
1981 for /d
%%i in
(.
) do set CURDIR
=%%~dpnxi
1982 echo @
echo off
> test.
cmd
1983 echo echo %%VAR%%>> test.
cmd
1984 echo setlocal>> test.
cmd
1985 echo set VAR
=localval
>> test.
cmd
1986 echo md foobar2
>> test.
cmd
1987 echo cd foobar2
>> test.
cmd
1988 echo echo %%VAR%%>> test.
cmd
1989 echo for /d
%%%%i in
(.
) do echo %%%%~dpnxi
>> test.
cmd
1990 echo endlocal>> test.
cmd
1991 echo echo %%VAR%%>> test.
cmd
1992 echo for /d
%%%%i in
(.
) do echo %%%%~dpnxi
>> test.
cmd
1996 for /d
%%i in
(.
) do echo %%~dpnxi
2000 echo --- setlocal with no corresponding
endlocal
2001 echo @
echo off
> test.
cmd
2002 echo echo %%VAR%%>> test.
cmd
2003 echo setlocal>> test.
cmd
2004 echo set VAR
=localval
>> test.
cmd
2005 echo md foobar2
>> test.
cmd
2006 echo cd foobar2
>> test.
cmd
2007 echo echo %%VAR%%>> test.
cmd
2008 echo for /d
%%%%i in
(.
) do echo %%%%~dpnxi
>> test.
cmd
2010 rem %CD% does not tork on NT4 so use the following workaround
2011 for /d
%%i in
(.
) do set CURDIR
=%%~dpnxi
2014 for /d
%%i in
(.
) do echo %%~dpnxi
2018 echo --- setlocal within same batch program
2022 rem %CD% does not tork on NT4 so use the following workaround
2023 for /d
%%i in
(.
) do set CURDIR
=%%~dpnxi
2030 if "%var1%"=="one" echo Var1 ok
1
2031 if "%var2%"=="two" echo Var2 ok
2
2032 if "%var3%"=="three" echo Var3 ok
3
2033 for /d
%%i in
(.
) do set curdir2
=%%~dpnxi
2034 if "%curdir2%"=="%curdir%\foobar2" echo Directory is ok
1
2036 if "%var1%"=="one" echo Var1 ok
1
2037 if "%var2%"=="two" echo Var2 ok
2
2038 if "%var3%"=="" echo Var3 ok
3
2039 for /d
%%i in
(.
) do set curdir2
=%%~dpnxi
2040 if "%curdir2%"=="%curdir%\foobar2" echo Directory is ok
2
2042 if "%var1%"=="one" echo Var1 ok
1
2043 if "%var2%"=="" echo Var2 ok
2
2044 if "%var3%"=="" echo Var3 ok
3
2045 for /d
%%i in
(.
) do set curdir2
=%%~dpnxi
2046 if "%curdir2%"=="%curdir%" echo Directory is ok
3
2050 echo --- Mismatched
set and
end locals
2054 rem %CD% does not tork on NT4 so use the following workaround
2055 for /d
%%i in
(.
) do set curdir
=%%~dpnxi
2057 echo @
echo off
> 2set1end.
cmd
2058 echo echo %%VAR%%>> 2set1end.
cmd
2059 echo setlocal>> 2set1end.
cmd
2060 echo set VAR
=2set1endvalue1
>> 2set1end.
cmd
2061 echo cd ..
\foodir
3>> 2set1end.
cmd
2062 echo setlocal>> 2set1end.
cmd
2063 echo set VAR
=2set1endvalue2
>> 2set1end.
cmd
2064 echo cd ..
\foodir
4>> 2set1end.
cmd
2065 echo endlocal>> 2set1end.
cmd
2066 echo echo %%VAR%%>> 2set1end.
cmd
2067 echo for /d
%%%%i in
(.
) do echo %%%%~dpnxi
>> 2set1end.
cmd
2069 echo @
echo off
> 1set2end.
cmd
2070 echo echo %%VAR%%>> 1set2end.
cmd
2071 echo setlocal>> 1set2end.
cmd
2072 echo set VAR
=1set2endvalue1
>> 1set2end.
cmd
2073 echo cd ..
\foodir
3>> 1set2end.
cmd
2074 echo endlocal>> 1set2end.
cmd
2075 echo echo %%VAR%%>> 1set2end.
cmd
2076 echo for /d
%%%%i in
(.
) do echo %%%%~dpnxi
>> 1set2end.
cmd
2077 echo endlocal>> 1set2end.
cmd
2078 echo echo %%VAR%%>> 1set2end.
cmd
2079 echo for /d
%%%%i in
(.
) do echo %%%%~dpnxi
>> 1set2end.
cmd
2081 echo --- Extra
setlocal in called batch
2083 rem -- setlocal1 == this batch, should never be used inside a called routine
2087 call %curdir%\
2set1end.
cmd
2090 for /d
%%i in
(.
) do echo %%~dpnxi
2093 for /d
%%i in
(.
) do echo %%~dpnxi
2096 echo --- Extra
endlocal in called batch
2098 rem -- setlocal1 == this batch, should never be used inside a called routine
2102 call %curdir%\
1set2end.
cmd
2105 for /d
%%i in
(.
) do echo %%~dpnxi
2108 for /d
%%i in
(.
) do echo %%~dpnxi
2111 echo --- endlocal in called function rather than batch pgm is ineffective
2117 call :endlocalroutine
2121 goto :endlocalfinished
2139 cd ..
& rd /q
/s foobar
2141 echo ------------ Testing
Errorlevel ------------
2142 rem WARNING: Do *not* add tests using ErrorLevel after this section
2143 should_not_exist
2> nul
> nul
2145 rem nt 4.0 doesn't really support a way of setting errorlevel, so this is weak
2146 rem See http://www.robvanderwoude.com/exit.php
2149 if errorlevel 2 echo errorlevel too high
, bad
2150 if errorlevel 1 echo errorlevel just right
, good
2151 if errorlevel 01 echo errorlevel with leading zero just right
, good
2152 if errorlevel -1 echo errorlevel with negative number OK
2153 if errorlevel 0x1 echo hexa should
not be recognized
!
2154 if errorlevel 1a
echo invalid error level recognized
!
2156 echo abc
%ErrorLevel%def
2157 if errorlevel 1 echo errorlevel nonzero
, bad
2158 if not errorlevel 1 echo errorlevel zero
, good
2159 if not errorlevel 0x1 echo hexa should
not be recognized
!
2160 if not errorlevel 1a
echo invalid error level recognized
!
2161 rem Now verify that setting a real variable hides its magic variable
2163 echo %ErrorLevel% should be
7
2164 if errorlevel 7 echo setting var worked too well
, bad
2166 echo %ErrorLevel% should still be
7
2168 echo ------------ Testing
GOTO ------------
2171 echo goto with no leading space worked
2174 echo goto with a leading space worked
2177 echo goto with a leading tab worked
2180 echo goto with a following space worked
2182 echo ------------ Testing
PATH ------------
2183 set backup_path
=%path%
2190 set path=%backup_path%
2193 echo ------------ Testing combined CALLs
/GOTOs
------------
2194 echo @
echo off
>foo.
cmd
2195 echo goto :eof>>foot.
cmd
2197 echo echo world
>>foo.
cmd
2199 echo @
echo off
>foot.
cmd
2200 echo echo cheball
>>foot.
cmd
2202 echo call :bar>>foot.
cmd
2203 echo if "%%1"=="deleteMe" (del foot.
cmd)>>foot.
cmd
2204 echo goto :eof>>foot.
cmd
2207 echo echo barbare
>>foot.
cmd
2208 echo goto :eof>>foot.
cmd
2214 rem Script execution stops after the following line
2237 echo Final message is
not output since earlier
'foot' processing stops script execution
2238 echo Do NOT add any tests below this line
2240 echo ------------ Done
, jumping to EOF
-----------
2242 rem Subroutine to set errorlevel and return
2243 rem in windows nt 4.0, this always sets errorlevel 1, since /b isn't supported
2246 rem This line runs under cmd in windows NT 4, but not in more modern versions.