วันเสาร์ที่ 5 กรกฎาคม พ.ศ. 2557

How to use if - else structure in a batch file?

http://stackoverflow.com/questions/11081735/how-to-use-if-else-structure-in-a-batch-file

IF %F%==1 IF %C%==1(
    ::copying the file c to d
    copy "%sourceFile%" "%destinationFile%"
    )
ELSE IF %F%==1 IF %C%==0(
    ::moving the file c to d
    move "%sourceFile%" "%destinationFile%"
    )

ELSE IF %F%==0 IF %C%==1(
    ::copying a directory c from d, /s:  boş olanlar hariç, /e:boş olanlar dahil
    xcopy "%sourceCopyDirectory%" "%destinationCopyDirectory%" /s/e
    )
ELSE IF %F%==0 IF %C%==0(
    ::moving a directory
    xcopy /E "%sourceMoveDirectory%" "%destinationMoveDirectory%"
    rd /s /q "%sourceMoveDirectory%"
    )

   @echo off

    set one=1
    set two=2

    REM Example 1

    IF %one%_%two%==1_1 (
       echo Example 1 fails
    ) ELSE IF %one%_%two%==1_2 (
       echo Example 1 works correctly
    ) ELSE (
        echo Example 1 fails
    )

    REM Example 2

    set test1result=0
    set test2result=0

    if %one%==1 if %two%==1 set test1result=1
    if %one%==1 if %two%==2 set test2result=1

    IF %test1result%==1 (
       echo Example 2 fails
    ) ELSE IF %test2result%==1 (
       echo Example 2 works correctly
    ) ELSE (
        echo Example 2 fails
    )

    REM Example 3

    if %one%==1 if %two%==1 (
       echo Example 3 fails
       goto :endoftests
    )
    if %one%==1 if %two%==2 (
       echo Example 3 works correctly
       goto :endoftests
    )
    echo Example 3 fails
    )
    :endoftests

ไม่มีความคิดเห็น:

แสดงความคิดเห็น