1:: 2:: Diverse tests for the CMD echoer and parser. 3:: 4@echo off 5setlocal enableextensions 6 7 8:: 9:: Simple tests for the CMD echoer. 10:: 11echo --------- Testing CMD echoer --------- 12@echo on 13 14if 1==1 echo j1|(echo j2) else echo j3 15 16(echo a 1>&2|echo a 1>&2) 2>&1 17 18echo 1 19@echo 2 20@@echo 3 21@@@echo 4 22 23:echo x1 24::echo x2 25@:echo y1 26@::echo y2 27@:::echo y3 28 29@@:echo z1 30@@::echo z2 31 32foobar 33echo 34foobar parameter 35echo parameter 36 37toto>NUL 38toto> NUL 39toto >NUL 40toto > NUL 41 42toto>NUL 2>&1 43toto> NUL 2>&1 44toto >NUL 2>&1 45toto > NUL 2>&1 46 47a&b 48a& b 49a &b 50a & b 51 52a||b 53a|| b 54a ||b 55a || b 56 57a&&b 58a&& b 59a &&b 60a && b 61 62:: a|b 63:: a| b 64:: a |b 65:: a | b 66 67if 1==1 (echo lol) else (echo boom) 68 69if 1==1 ( 70echo lol 71) else ( 72echo boom 73) 74 75for /l %%l in (1,1,5) do (echo %%l) 76 77for /l %%l in (1,1,5) do ( 78echo %%l 79) 80 81for /l %%l in (1,1,5) do (@@@echo %%l) 82 83if 1==1 @echo hi 84if 1==1 (@echo heh) 85if 1==0 (@echo lol) else @echo better 86if 1==0 (@echo lol) else (@echo better2) 87 88(a) 89(a b) 90 91:: An empty parenthesized block is considered to be an error. 92:: () 93 94 95:: 96:: Tests for delayed expansion. 97:: 98 99@echo off 100setlocal enableextensions 101setlocal enabledelayedexpansion 102 103echo --------- Testing Delayed Expansion --------- 104 105:: Checking exclamation point escaping 106set ENDV= ^(an open-source operating system^) 107echo This is ReactOS^^!%ENDV% 108echo Hello! 109echo Hello!! 110echo Hello!!! 111echo Hello^^^! "^!" 112 113:: The following tests are adapted from 114:: https://ss64.com/nt/delayedexpansion.html 115 116echo "Hello^World" 117echo "Hello^World!" 118 119:: Checking expansion 120set "_var=first" 121set "_var=second" & Echo %_var% !_var! 122 123:: Checking expansion and replacement 124set var1=Hello ABC how are you 125set var2=ABC 126set result=!var1:%var2%=Developer! 127echo [!result!] 128 129:: Test of FOR-loop 130set COUNT=0 131for /l %%v in (1,1,4) do ( 132 set /A COUNT=!COUNT! + 1 133 echo [!COUNT!] 134) 135echo Total = %COUNT% 136 137 138set "com[0]=lol0" 139set "com[1]=lol2" 140set "com[2]=lol4" 141set "com[3]=lol6" 142set "com[4]=lol8" 143set "com[5]=lol10" 144 145for /l %%k in (1,1,5) do ( 146 echo(!com[%%k]! 147 if /I "%%k" equ "5" echo OHLALA 148) 149 150 151:: 152:: Re-enable the command echoer 153:: 154@echo on 155 156setlocal disabledelayedexpansion 157 158echo %~DP0 159 160set test=abc 161set abc=def 162 163echo % 164echo %% 165echo %%% 166echo %%%% 167 168echo %test% 169echo %test%% 170echo %%test% 171echo %%test%% 172echo %%%test%%% 173 174echo !test! 175echo !!test!! 176 177endlocal 178setlocal enabledelayedexpansion 179 180:: 181:: Regular and Delayed variables 182:: 183 184echo ! 185echo !! 186echo !!! 187echo !!!! 188echo !a! 189echo !!a!! 190 191set a=b 192:: Will display b 193echo !!a!! 194set b=c 195:: Will still display b 196echo !!a!! 197 198echo %test% 199echo %test%% 200echo %%test% 201echo %%test%% 202echo %%%test%%% 203 204echo %!test!% 205echo !%test%! 206echo !!test!! 207:: That other one is the same as the previous one. 208echo !^!test^!! 209echo !^^!test^^!! 210echo !test! 211echo !test!! 212echo !!test! 213echo !!test!! 214echo !!!test!!! 215 216 217set proj=XYZ 218echo !%proj%_folder! 219echo !!proj!_folder! 220 221set %proj%_folder=\\server\folder\ 222echo !%proj%_folder! 223echo !!proj!_folder! 224 225 226:: 227:: Delayed variables in blocks 228:: 229 230if 1==1 ( 231 set "pc=T" 232 echo pc == !pc! 233 234 set i=3 235 set "!pc!!i!=5" 236 237 echo other pc == !pc! and !pc!!i! == !!pc!!i!! 238 echo other pc == !pc! and !pc!!i! == !^!pc^!^!i^!! 239 echo other pc == !pc! and !pc!!i! == ^!!pc!!i!^! 240 echo other pc == !pc! and !pc!!i! == ^!^!pc^!^!i^!^! 241 set "trol=!pc!!i!" 242 echo the var was !trol! 243 244 set "!pc!N=!i!" 245 echo updated !pc!N == !!pc!N! 246 echo updated !pc!N == !^!pc^!N! 247 echo updated !pc!N == ^!!pc!N^! 248 echo updated !pc!N == ^!^!pc^!N^! 249 set "trol=!pc!N" 250 echo updated !pc!N == !trol! 251) 252 253 254 255:: Parsing FOR, IF and REM commands. 256:: 257:: Note: wrong syntaxes for FOR, IF, REM or parsing operators 258:: are considered syntactic errors and batch running stops. 259:: For all other commands, execution continues. 260:: 261 262@echo on 263 264echo --------- Parsing FOR, IF and REM commands --------- 265 266fOr@space@@space@@space@@tab@@tab@ /d@space@@space@@tab@ %%d iN (*)@space@@space@@tab@ do eCHo %%d 267:for /asdf 268:: for /d %%d in (*) do echo %%d 269:: for /d %%d in (*) do echo %%~fd 270for /d %%d in (*) do echo %%~ed 271for /d %%d in (*) do echo %~d0: 272fOr@tab@@space@@space@@space@@space@@space@@space@@space@@space@@tab@/l@space@@tab@ %%c@space@@space@@tab@ iN@tab@ (1,1,5)@space@@space@@tab@ Do@space@@space@@tab@@space@@tab@ echo@tab@@space@@space@@space@@space@@tab@@tab@%%c 273 274 275iF@space@@space@@tab@ 457@space@@tab@@space@@tab@neQ@space@@space@@tab@ 458@space@@tab@ (@space@@tab@ echo@space@@tab@ %~d0 ) eLSe (@space@@space@@tab@ echo $~d0@space@@tab@ ) 276 277::: if 457 nea 458 (echo %~d0) eLSe (echo $~dp) 278:if 457 nea 458 (echo yo) else (echo ya) 279:if 457 leq 458 (rem/? /d) 280:: if 457 leq 458 ( if 457 nea 458 (echo hi) else (echo yo) ) 281 282iF@space@@space@@tab@ "2147483647"@space@@space@@tab@ gEq@space@@tab@@space@@tab@ "2147483648"@space@@tab@ (Echo Larger)@space@@space@@tab@ Else@space@@tab@ (:Echo Smaller) 283ecHO sMaLlEr) 284 285iF@space@@space@@tab@eRrOrlevel 0 echo hi! 286 287 288:: rem@tab@@space@@tab@ /df@space@@space@@tab@ /d 289 290rEM /v@space@@tab@/d 291REm@space@@space@@space@@tab@@tab@ /d 292rEm REM2 /d 293reM@space@@tab@ /d@space@@tab@ >@space@@tab@ NUL 294:reM /?/d 295 296:: These commands, even commented out via the parser comment colon, 297:: cause parsing errors even when being commented out. 298:: Replace the two '%' by a single one to see the effects. 299:: 300:echo %~f0 %%~dp %~p1 301:echo %%~d0 $~dp 302:echo %%~dp: 303:echo %%~b0 304 305 306 307:: 308:: Parsing random commands 309:: 310 311echo --------- Parsing random commands --------- 312 313:: Parsing these commands should fail (when being un-commmented). 314:@a ( b & | 315: a ( b & | 316 317:: If goto fails, batch stops. (Done OK in ROS' cmd) 318:goto /asdf whatever 319 320dir > NUL &(b) 321 322 323setlocal enabledelayedexpansion 324 325echo %~dp0 326 327set SOMEVAR=C:\ReAcToS 328rem %SOMEVAR% | 329 330Set "_var=first" 331Set "_var=second" & Rem %_var% !_var! 332Set "_var=third" & Echo %_var% !_var! 333 334endlocal 335 336 337 338:: 339:: Parsing line continuations, either from parenthesized blocks 340:: or via the escape caret. 341:: It may be informative to manually run this test under CMD 342:: with cmd!fDumpTokens and cmd!fDumpParse flags enabled. 343:: 344 345echo --------- Parsing line continuations --------- 346 347( 348a & b 349c 350d 351) 352:: 353:: Parsed as: 354:: '('[ '&'[a, CRLF[b, CRLF[c,d] ] ] ] 355:: 356 357 358( 359a && b 360c 361d 362) 363:: 364:: Parsed as: 365:: '('[ CRLF['&&'[a,b], CRLF[c,d]] ] 366:: 367 368 369(a & b) 370 371( 372a & b 373) 374 375 376( 377a & b 378c & d 379) 380 381 382(a & ^ 383b 384c & d 385) 386:: 387:: Parsed as: 388:: '('[ '&'[a, CRLF[b, '&'[c,d] ] ] 389:: 390 391 392( 393a & b 394c 395d 396) 397:: 398:: Parsed as: 399:: '('[ '&'[a, CRLF[b, CRLF[c,d] ] ] ] 400:: 401 402 403( 404a && b 405c 406d 407) 408:: 409:: Parsed as: 410:: '('[ CRLF['&&'[a,b], CRLF[c,d]] ] 411:: 412 413 414( 415a || b 416c 417d 418) 419:: 420:: Parsed as: 421:: '('[ CRLF['||'[a,b], CRLF[c,d]] ] 422:: 423 424 425( 426a 427b & c 428d 429) 430:: 431:: Parsed as: 432:: '('[ CRLF[a, '&'[b, CRLF[c,d]]] ] 433:: 434 435 436( 437a 438b && c 439d 440) 441:: 442:: Parsed as: 443:: '('[ CRLF[a, CRLF['&&'[b,c], d]] ] 444:: 445 446 447( 448a 449b 450c && d 451) 452:: 453:: Parsed as: 454:: '('[ CRLF[a, CRLF[b, '&&'[c,d]]] ] 455:: 456 457 458( 459a 460b 461c & d 462) 463:: 464:: Parsed as: 465:: '('[ CRLF[a, CRLF[b, '&'[c,d]]] ] 466:: 467 468 469( 470a 471b 472 473c 474d 475) 476:: 477:: Parsed as: 478:: '('[ CRLF[a, CRLF[b, CRLF[c,d] ] ] ] 479:: 480 481 482 483REM foo^ 484bar^ 485baz trol^ 486 487 488if 1==1 (echo a) else (echo b) 489 490if 1==1 (echo a 491) else (echo b) 492 493if 1==1 (echo a) else ( 494echo b) 495 496if 1==1 ( 497echo a 498) else ( 499echo b 500) 501 502 503REM if 1==1 (^ 504REM echo a 505REM ) ^ 506REM else 507REM (^ 508REM echo b 509REM ) 510 511 512REM if 1==1 (^ 513REM echo a 514REM ) ^ 515REM else^ 516REM (^ 517REM echo b 518REM ) 519 520 521REM if 1==1 (^ 522REM echo a 523REM ) ^ 524REM else^ 525REM ( 526REM echo b 527REM ) 528 529 530if 1==1 (^ 531echo a 532) 533 534 535if 1==1 (^ 536@echo a 537) 538 539 540 541(foo ^ 542bar 543) 544 545(foo ^ 546&& bar 547baz 548) 549 550(foo ^ 551 && bar 552baz 553) 554 555REM (foo^ 556REM && bar 557REM baz 558REM ) 559 560(foo^ 561bar 562baz 563) 564 565 566(foo &^ 567& bar 568baz 569) 570 571 572(^ 573foo 574bar 575) 576 577 578 579( 580 581 582foo ^ 583bar 584) 585 586 587 588( 589 590 591foo ^ 592&& bar 593baz 594) 595 596 597 598( 599 600 601foo ^ 602 && bar 603baz 604) 605 606 607 608 609REM ( 610REM 611REM 612REM foo^ 613REM && bar 614REM baz 615REM ) 616 617 618 619( 620 621 622foo^ 623bar 624baz 625) 626 627 628 629( 630 631 632foo &^ 633& bar 634baz 635) 636 637 638 639( 640 641 642^ 643foo 644bar 645) 646 647 648 649:: 650:: Tests for Character Escape and Line Continuation 651:: 652 653(^"!pc::^=^!^") 654 655(^ 656"!pc::^=^!^") 657 658( 659^"!pc::^=^!^") 660 661 662(^"!pc::^=^ 663% New line % 664!^") 665 666REM @ 2>&1 ( ( dir) ) && lol 667 668 669echo & ^ 670&&lol 671 672 673REM echo & ^ 674REM ^ 675REM &&lol 676 677 678trol ^ 679&&lol 680 681 682trol^ 683&&lol 684 685 686REM (echo hi)^ 687REM &&lol 688 689 690rem trol(^ 691line 692@rem trol2""^ 693line2 694:trol3^ 695line3 696 697echo trol^ 698line 699 700:echo trol^ 701line 702 703 704 705@echo off 706 707:: 708:: Finished! 709:: 710echo --------- Finished -------------- 711goto :EOF 712