[置頂]

書單: 安迪葛洛夫:Only paranoid survives.

Feb 11, 2009

[Batch Script Programming] Execute 2 commands in IF expression

You can execute 2 or more commands in one IF expression:

  • You can use "&" to execute 2 or more command if a IF expression
  • @echo off

    set A=12
    set B=24

    IF %A% EQU 12           echo "hawaii" & echo "los angles" & echo "tapiei"

    The result is
    "hawaii"
    "los angles"
    "tapiei"

  • If you use set command right before "&", don't leave space. Or the space will become part of the value.

  • For example, following code will search \sources\boot.wim in each drive (from C to Z). If found, it will set variable RP the drive.

      FOR %%d in (C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z) DO IF EXIST %%d:\sources\boot.wim set RP=%%d& GOTO END_SEARCH_RP

      However, if you leave space befire "&", like this:

      FOR %%d in (C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z) DO IF EXIST %%d:\sources\boot.wim set RP=%%d & GOTO END_SEARCH_RP

      If "E" is the drive, the above code will set RP as "E ".

No comments: