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
1602 cd .. & rd /s/q foobar
1604 echo ------------ Testing SHIFT ------------
1606 call :shiftFun p1 p2 p3 p4 p5
1610 echo '%1' '%2' '%3' '%4' '%5'
1612 echo '%1' '%2' '%3' '%4' '%5'
1614 echo '%1' '%2' '%3' '%4' '%5'
1616 echo '%1' '%2' '%3' '%4' '%5'
1618 echo '%1' '%2' '%3' '%4' '%5'
1620 echo '%1' '%2' '%3' '%4' '%5'
1624 echo ------------ Testing cmd invocation ------------
1625 rem FIXME: only a stub ATM
1626 echo --- a batch file can delete itself
1627 echo del foo.cmd>foo.cmd
1629 if not exist foo.cmd (
1630 echo file correctly deleted
1632 echo file should be deleted!
1635 echo --- a batch file can alter itself
1636 echo echo bar^>foo.cmd>foo.cmd
1637 cmd /q /c foo.cmd > NUL 2>&1
1642 echo file not created!
1645 echo ---------- Testing copy
1648 rem Note echo adds 0x0d 0x0a on the end of the line in the file
1651 echo CCCCCCCCC> file3
1657 echo Passed: Found expected %1
1659 echo Failed: Did not find expected %1
1661 del /q "%1" >nul 2>&1
1663 if not "%1"=="" goto :CheckExist
1668 echo Passed: Did not find %1
1670 echo Failed: Unexpectedly found %1
1671 del /q "%1" >nul 2>&1
1674 if not "%1"=="" goto :CheckNotExist
1677 rem Note: No way to check file size on NT4 so skip the test
1680 echo Failed: File missing when requested filesize check [%2]
1681 goto :ContinueFileSizeChecks
1683 for %%i in (%1) do set filesize=%%~zi
1684 if "%filesize%"=="%2" (
1685 echo Passed: file size check on %1 [%filesize%]
1687 if "%filesize%"=="%%~zi" (
1688 echo Skipping file size check on NT4
1690 echo Failed: file size check on %1 [%filesize% != %2]
1693 :ContinueFileSizeChecks
1696 if not "%1"=="" goto :CheckFileSize
1701 rem -----------------------
1702 rem Simple single file copy
1703 rem -----------------------
1704 rem Simple single file copy, normally used syntax
1705 copy file1 dummy.file >nul 2>&1
1706 if errorlevel 1 echo Incorrect errorlevel
1707 call :CheckExist dummy.file
1709 rem Simple single file copy, destination supplied as two forms of directory
1710 copy file1 dir1 >nul 2>&1
1711 if errorlevel 1 echo Incorrect errorlevel
1712 call :CheckExist dir1\file1
1714 copy file1 dir1\ >nul 2>&1
1715 if errorlevel 1 echo Incorrect errorlevel
1716 call :CheckExist dir1\file1
1718 rem Simple single file copy, destination supplied as fully qualified destination
1719 copy file1 dir1\file99 >nul 2>&1
1720 if errorlevel 1 echo Incorrect errorlevel
1721 call :CheckExist dir1\file99
1723 rem Simple single file copy, destination not supplied
1725 copy ..\file1 >nul 2>&1
1726 if errorlevel 1 echo Incorrect errorlevel
1727 call :CheckExist file1
1730 rem Simple single file copy, destination supplied as non existant directory
1731 copy file1 dir2\ >nul 2>&1
1732 if not errorlevel 1 echo Incorrect errorlevel
1733 call :CheckNotExist dir2 dir2\file1
1735 rem -----------------------
1737 rem -----------------------
1738 rem Simple single file copy, destination supplied as two forms of directory
1739 copy file? dir1 >nul 2>&1
1740 if errorlevel 1 echo Incorrect errorlevel
1741 call :CheckExist dir1\file1 dir1\file2 dir1\file3
1743 copy file* dir1\ >nul 2>&1
1744 if errorlevel 1 echo Incorrect errorlevel
1745 call :CheckExist dir1\file1 dir1\file2 dir1\file3
1747 rem Simple single file copy, destination not supplied
1749 copy ..\file*.* >nul 2>&1
1750 if errorlevel 1 echo Incorrect errorlevel
1751 call :CheckExist file1 file2 file3
1754 rem Simple wildcarded file copy, destination supplied as non existant directory
1755 copy file? dir2\ >nul 2>&1
1756 if not errorlevel 1 echo Incorrect errorlevel
1757 call :CheckNotExist dir2 dir2\file1 dir2\file2 dir2\file3
1759 rem ------------------------------------------------
1760 rem Confirm overwrite works (cannot test prompting!)
1761 rem ------------------------------------------------
1762 copy file1 testfile >nul 2>&1
1763 copy /y file2 testfile >nul 2>&1
1764 call :CheckExist testfile
1766 rem ------------------------------------------------
1767 rem Test concatenation
1768 rem ------------------------------------------------
1769 rem simple case, no wildcards
1770 copy file1+file2 testfile >nul 2>&1
1771 if errorlevel 1 echo Incorrect errorlevel
1772 call :CheckExist testfile
1774 rem simple case, wildcards, no concatenation
1775 copy file* testfile >nul 2>&1
1776 if errorlevel 1 echo Incorrect errorlevel
1777 call :CheckExist testfile
1779 rem simple case, wildcards, and concatenation
1781 copy file*+fred testfile >nul 2>&1
1782 if errorlevel 1 echo Incorrect errorlevel
1783 call :CheckExist testfile
1785 rem simple case, wildcards, and concatenation
1786 copy fred+file* testfile >nul 2>&1
1787 if errorlevel 1 echo Incorrect errorlevel
1788 call :CheckExist testfile
1790 rem Calculate destination name
1791 copy fred+file* dir1 >nul 2>&1
1792 if errorlevel 1 echo Incorrect errorlevel
1793 call :CheckExist dir1\fred
1795 rem Calculate destination name
1796 copy fred+file* dir1\ >nul 2>&1
1797 if errorlevel 1 echo Incorrect errorlevel
1798 call :CheckExist dir1\fred
1800 rem Calculate destination name (none supplied)
1802 copy ..\fred+..\file* >nul 2>&1
1803 if errorlevel 1 echo Incorrect errorlevel
1804 call :CheckExist fred
1806 copy ..\fr*+..\file1 >nul 2>&1
1807 if errorlevel 1 echo Incorrect errorlevel
1808 call :CheckExist fred
1811 rem ******************************************************************
1812 rem ASCII and BINARY tests
1813 rem Note: hard coded numbers deliberate because need to ensure whether
1814 rem an additional EOF has been added or not. There is no way to handle
1815 rem EOFs in batch, so assume if a single byte appears, its an EOF!
1816 rem ******************************************************************
1818 rem Confirm original sizes of file1,2,3
1819 call :CheckFileSize file1 5 file2 8 file3 11
1823 rem ----------------------------------------------
1824 rem Show concatenation defaults copy to ascii mode
1825 rem ----------------------------------------------
1826 rem Simple default copy source to destination (should not append EOF 5)
1827 copy ..\file1 file1_default >nul 2>&1
1828 call :CheckFileSize file1_default 5
1830 rem Simple binary copy source to destination (should not append EOF 5)
1831 copy /b ..\file1 file1_default2 >nul 2>&1
1832 call :CheckFileSize file1_default2 5
1834 rem Simple ascii copy source to destination (should append EOF 5+1, 8+1, 11+1)
1835 copy /a ..\file1 file1_plus_eof >nul 2>&1
1836 call :CheckFileSize file1_plus_eof 6
1837 copy /a ..\file2 file2_plus_eof >nul 2>&1
1838 call :CheckFileSize file2_plus_eof 9
1839 copy /a ..\file3 file3_plus_eof >nul 2>&1
1840 call :CheckFileSize file3_plus_eof 12
1842 rem Concat 2 files, ascii mode - (only one EOF on the end 5+8+1)
1843 copy /a ..\file1+..\file2 file12_plus_eof >nul 2>&1
1844 call :CheckFileSize file12_plus_eof 14
1846 rem Concat 2 files, binary mode - (no EOF on the end 5+8)
1847 copy /b ..\file1+..\file2 file12_no_eof >nul 2>&1
1848 call :CheckFileSize file12_no_eof 13
1850 rem Concat 2 files, default mode - (one EOF on the end 5+8+1)
1851 copy ..\file1+..\file2 file12_eof2 >nul 2>&1
1852 call :CheckFileSize file12_eof2 14
1854 rem --------------------------------------------------------------
1855 rem Show ascii source copy stops at first EOF, binary does the lot
1856 rem --------------------------------------------------------------
1857 copy file1_plus_eof /b file1_binary_srccopy /b >nul 2>&1
1858 call :CheckFileSize file1_binary_srccopy 6
1860 copy file1_plus_eof /a file1_ascii_srccopy /b >nul 2>&1
1861 call :CheckFileSize file1_ascii_srccopy 5
1863 rem --------------------------------------------------------------
1864 rem Show results of concatenating files (ending in EOFs) and /a /b
1865 rem --------------------------------------------------------------
1867 rem Default and ascii copy reads as ascii, stripping EOFs, so 6-1 + 9-1 + 12-1 + 1
1868 copy file1_plus_eof+file2_plus_eof+file3_plus_eof file123_default_copy >nul 2>&1
1869 call :CheckFileSize file123_default_copy 25
1870 copy /a file1_plus_eof+file2_plus_eof+file3_plus_eof file123_ascii_copy >nul 2>&1
1871 call :CheckFileSize file123_ascii_copy 25
1873 rem In binary mode, we get 3 eofs, so 6 + 9 + 12 = 27
1874 copy /b file1_plus_eof + file2_plus_eof + file3_plus_eof file123_binary_copy >nul 2>&1
1875 call :CheckFileSize file123_binary_copy 27
1877 rem We can select which we want the eofs from by postfixing it with /a or /b
1878 rem so here have first and third with eof, second as ascii 6 + 9-1 + 12
1879 copy file1_plus_eof /b + file2_plus_eof /a + file3_plus_eof /b file123_mixed_copy1 >nul 2>&1
1880 call :CheckFileSize file123_mixed_copy1 26
1882 rem By postfixing the destination with /a, we ask for an ascii destination which appends EOF
1883 rem so here have first and third with eof, second as ascii 6 + 9-1 + 12 + extra EOF
1884 rem Note the delta between this and the previous one also shows that the destination
1885 rem ascii/binary is inherited from the last /a or /b on the line
1886 copy file1_plus_eof /b + file2_plus_eof /a + file3_plus_eof /b file123_mixed_copy2 /a >nul 2>&1
1887 call :CheckFileSize file123_mixed_copy2 27
1889 rem so here have second with eof, first and third as ascii 6-1 + 9 + 12-1
1890 rem Note the delta between the next two also shows that the destination ascii/binary is
1891 rem inherited from the last /a or /b on the line, so the first has an extra EOF
1892 copy file1_plus_eof /a + file2_plus_eof /b + file3_plus_eof /a file123_mixed_copy3 >nul 2>&1
1893 call :CheckFileSize file123_mixed_copy3 26
1894 copy file1_plus_eof /a + file2_plus_eof /b + file3_plus_eof /a file123_mixed_copy4 /b >nul 2>&1
1895 call :CheckFileSize file123_mixed_copy4 25
1897 rem -------------------------------------------------------------------------------------------
1898 rem This shows when concatenating, an ascii destination always adds on an EOF but when we
1899 rem are not concatenating, its a direct copy regardless of destination if being read as binary
1900 rem -------------------------------------------------------------------------------------------
1902 rem All 3 have eof's, plus an extra = 6 + 9 + 12 + eof
1903 copy /b file1_plus_eof + file2_plus_eof + file3_plus_eof file123_mixed_copy5 /a >nul 2>&1
1904 call :CheckFileSize file123_mixed_copy5 28
1906 rem All 2 have eof's, plus an extra = 6 + 12 + eof
1907 copy /b file1_plus_eof + file3_plus_eof file123_mixed_copy6 /a >nul 2>&1
1908 call :CheckFileSize file123_mixed_copy6 19
1910 rem One file has EOF, but doesnt get an extra one, ie 6
1911 copy /b file1_plus_eof file123_mixed_copy7 /a >nul 2>&1
1912 call :CheckFileSize file123_mixed_copy7 6
1917 rem ---------------------------------------
1918 rem Error combinations
1919 rem ---------------------------------------
1920 rem Specify source directory but name is a file
1922 copy file1\ dir1\ >NUL 2>&1
1923 if errorlevel 1 echo Passed: errorlevel invalid check 1
1924 if not errorlevel 1 echo Failed: errorlevel invalid check 1
1925 call :CheckNotExist dir1\file1
1927 rem Overwrite same file
1929 copy file1 file1 >NUL 2>&1
1930 if errorlevel 1 echo Passed: errorlevel invalid check 2
1931 if not errorlevel 1 echo Failed: errorlevel invalid check 2
1933 rem Supply same file identified as a directory
1935 copy file1 file1\ >NUL 2>&1
1936 if errorlevel 1 echo Passed: errorlevel invalid check 3
1937 if not errorlevel 1 echo Failed: errorlevel invalid check 3
1942 echo ------------ Testing setlocal/endlocal ------------
1944 rem Note: setlocal EnableDelayedExpansion already tested in the variable delayed expansion test section
1945 mkdir foobar & cd foobar
1946 echo --- enable/disable extensions
1947 setlocal DisableEXTensions
1948 echo ErrLev: %ErrorLevel%
1950 echo ErrLev: %ErrorLevel%
1951 echo @echo off> tmp.cmd
1952 echo echo ErrLev: %%ErrorLevel%%>> tmp.cmd
1953 rem Enabled by default
1955 cmd /E:OfF /C tmp.cmd
1956 cmd /e:oN /C tmp.cmd
1958 rem FIXME: creating file before setting envvar value to prevent parsing-time evaluation (due to EnableDelayedExpansion not being implemented/available yet)
1959 echo --- setlocal with corresponding endlocal
1960 rem %CD% does not tork on NT4 so use the following workaround
1961 for /d %%i in (.) do set CURDIR=%%~dpnxi
1962 echo @echo off> test.cmd
1963 echo echo %%VAR%%>> test.cmd
1964 echo setlocal>> test.cmd
1965 echo set VAR=localval>> test.cmd
1966 echo md foobar2>> test.cmd
1967 echo cd foobar2>> test.cmd
1968 echo echo %%VAR%%>> test.cmd
1969 echo for /d %%%%i in (.) do echo %%%%~dpnxi>> test.cmd
1970 echo endlocal>> test.cmd
1971 echo echo %%VAR%%>> test.cmd
1972 echo for /d %%%%i in (.) do echo %%%%~dpnxi>> test.cmd
1976 for /d %%i in (.) do echo %%~dpnxi
1980 echo --- setlocal with no corresponding endlocal
1981 echo @echo off> test.cmd
1982 echo echo %%VAR%%>> test.cmd
1983 echo setlocal>> test.cmd
1984 echo set VAR=localval>> test.cmd
1985 echo md foobar2>> test.cmd
1986 echo cd foobar2>> test.cmd
1987 echo echo %%VAR%%>> test.cmd
1988 echo for /d %%%%i in (.) do echo %%%%~dpnxi>> test.cmd
1990 rem %CD% does not tork on NT4 so use the following workaround
1991 for /d %%i in (.) do set CURDIR=%%~dpnxi
1994 for /d %%i in (.) do echo %%~dpnxi
1998 echo --- setlocal within same batch program
2002 rem %CD% does not tork on NT4 so use the following workaround
2003 for /d %%i in (.) do set CURDIR=%%~dpnxi
2010 if "%var1%"=="one" echo Var1 ok 1
2011 if "%var2%"=="two" echo Var2 ok 2
2012 if "%var3%"=="three" echo Var3 ok 3
2013 for /d %%i in (.) do set curdir2=%%~dpnxi
2014 if "%curdir2%"=="%curdir%\foobar2" echo Directory is ok 1
2016 if "%var1%"=="one" echo Var1 ok 1
2017 if "%var2%"=="two" echo Var2 ok 2
2018 if "%var3%"=="" echo Var3 ok 3
2019 for /d %%i in (.) do set curdir2=%%~dpnxi
2020 if "%curdir2%"=="%curdir%\foobar2" echo Directory is ok 2
2022 if "%var1%"=="one" echo Var1 ok 1
2023 if "%var2%"=="" echo Var2 ok 2
2024 if "%var3%"=="" echo Var3 ok 3
2025 for /d %%i in (.) do set curdir2=%%~dpnxi
2026 if "%curdir2%"=="%curdir%" echo Directory is ok 3
2030 echo --- Mismatched set and end locals
2034 rem %CD% does not tork on NT4 so use the following workaround
2035 for /d %%i in (.) do set curdir=%%~dpnxi
2037 echo @echo off> 2set1end.cmd
2038 echo echo %%VAR%%>> 2set1end.cmd
2039 echo setlocal>> 2set1end.cmd
2040 echo set VAR=2set1endvalue1>> 2set1end.cmd
2041 echo cd ..\foodir3>> 2set1end.cmd
2042 echo setlocal>> 2set1end.cmd
2043 echo set VAR=2set1endvalue2>> 2set1end.cmd
2044 echo cd ..\foodir4>> 2set1end.cmd
2045 echo endlocal>> 2set1end.cmd
2046 echo echo %%VAR%%>> 2set1end.cmd
2047 echo for /d %%%%i in (.) do echo %%%%~dpnxi>> 2set1end.cmd
2049 echo @echo off> 1set2end.cmd
2050 echo echo %%VAR%%>> 1set2end.cmd
2051 echo setlocal>> 1set2end.cmd
2052 echo set VAR=1set2endvalue1>> 1set2end.cmd
2053 echo cd ..\foodir3>> 1set2end.cmd
2054 echo endlocal>> 1set2end.cmd
2055 echo echo %%VAR%%>> 1set2end.cmd
2056 echo for /d %%%%i in (.) do echo %%%%~dpnxi>> 1set2end.cmd
2057 echo endlocal>> 1set2end.cmd
2058 echo echo %%VAR%%>> 1set2end.cmd
2059 echo for /d %%%%i in (.) do echo %%%%~dpnxi>> 1set2end.cmd
2061 echo --- Extra setlocal in called batch
2063 rem -- setlocal1 == this batch, should never be used inside a called routine
2067 call %curdir%\2set1end.cmd
2070 for /d %%i in (.) do echo %%~dpnxi
2073 for /d %%i in (.) do echo %%~dpnxi
2076 echo --- Extra endlocal in called batch
2078 rem -- setlocal1 == this batch, should never be used inside a called routine
2082 call %curdir%\1set2end.cmd
2085 for /d %%i in (.) do echo %%~dpnxi
2088 for /d %%i in (.) do echo %%~dpnxi
2091 echo --- endlocal in called function rather than batch pgm is ineffective
2097 call :endlocalroutine
2101 goto :endlocalfinished
2119 cd .. & rd /q/s foobar
2121 echo ------------ Testing Errorlevel ------------
2122 rem WARNING: Do *not* add tests using ErrorLevel after this section
2123 should_not_exist 2> nul > nul
2125 rem nt 4.0 doesn't really support a way of setting errorlevel, so this is weak
2126 rem See http://www.robvanderwoude.com/exit.php
2129 if errorlevel 2 echo errorlevel too high, bad
2130 if errorlevel 1 echo errorlevel just right, good
2131 if errorlevel 01 echo errorlevel with leading zero just right, good
2132 if errorlevel -1 echo errorlevel with negative number OK
2133 if errorlevel 0x1 echo hexa should not be recognized!
2134 if errorlevel 1a echo invalid error level recognized!
2136 echo abc%ErrorLevel%def
2137 if errorlevel 1 echo errorlevel nonzero, bad
2138 if not errorlevel 1 echo errorlevel zero, good
2139 if not errorlevel 0x1 echo hexa should not be recognized!
2140 if not errorlevel 1a echo invalid error level recognized!
2141 rem Now verify that setting a real variable hides its magic variable
2143 echo %ErrorLevel% should be 7
2144 if errorlevel 7 echo setting var worked too well, bad
2146 echo %ErrorLevel% should still be 7
2148 echo ------------ Testing GOTO ------------
2151 echo goto with no leading space worked
2154 echo goto with a leading space worked
2157 echo goto with a leading tab worked
2160 echo goto with a following space worked
2162 echo ------------ Testing PATH ------------
2163 set backup_path=%path%
2170 set path=%backup_path%
2173 echo ------------ Testing combined CALLs/GOTOs ------------
2174 echo @echo off>foo.cmd
2175 echo goto :eof>>foot.cmd
2177 echo echo world>>foo.cmd
2179 echo @echo off>foot.cmd
2180 echo echo cheball>>foot.cmd
2182 echo call :bar>>foot.cmd
2183 echo if "%%1"=="deleteMe" (del foot.cmd)>>foot.cmd
2184 echo goto :eof>>foot.cmd
2187 echo echo barbare>>foot.cmd
2188 echo goto :eof>>foot.cmd
2194 rem Script execution stops after the following line
2217 echo Final message is not output since earlier 'foot' processing stops script execution
2218 echo Do NOT add any tests below this line
2220 echo ------------ Done, jumping to EOF -----------
2222 rem Subroutine to set errorlevel and return
2223 rem in windows nt 4.0, this always sets errorlevel 1, since /b isn't supported
2226 rem This line runs under cmd in windows NT 4, but not in more modern versions.