批處理文件“and if”語句 (Batch-file "and if" statement)


問題描述

批處理文件“and if”語句 (Batch‑file "and if" statement)

我完全不知道如何為我的批處理文件製作“and if”語句,我希望我能在這裡得到一些幫助。

我正在嘗試製作的行:

if %a%==X/O and if %b%==X/O goto done

參考解法

方法 1:

In batch, if statements are quite simple, but unfortunately don't have the same amount of functionality of many other coding languages such as JavaScript.

In batch, to write an if statement, there are only 4 parts. Of course at the beginning you have to specify that it is a if statement, then you add the two things you will be comparing, and the operator. Once that is done, you specify the function you want the program to do if the condition is true. For example:

if "%type%" == "5" goto end

Also, one last thing to note. If your condition is false, the batch file will simply go to the next line. So you might want to add something like this:

:start
set num1=1
set num2=2
set num3=3
set /a totalnum=%num1%+%num2%+%num3%
if "%totalnum%" == "100" goto end
goto start
:end
exit

方法 2:

Batch doesn't have support for ands or ors in if logic. You can simulate an and by nesting your conditions.

if %a%==X/O (
    if %b%==X/O (
        goto done
    )
)

Similarly, you can simulate an or by having two separate checks.

if %a%==X/O goto done
if %b%==X/O goto done

(by conecallJacob WardSomethingDark)

參考文件

  1. Batch‑file "and if" statement (CC BY‑SA 2.5/3.0/4.0)

#batch-file






相關問題

如何在 Windows 批處理腳本或 Perl 中將文件移動到回收站? (How can I move files to the Recycle Bin in a Windows batch script or Perl?)

psftp 批處理腳本不起作用 (psftp batch script won't work)

用於確定 Windows 和 Office 版本的快速命令或批處理腳本 (quick command or batch script to determine Windows and Office version)

查找 pdfs/doc/docx/etc 的批處理文件 (Batch file to look for pdfs/doc/docx/etc)

批處理文件“and if”語句 (Batch-file "and if" statement)

打開具有特定顏色和標題的 CMD (Open CMD with specific color and title)

使用 excel 2010 更新批處理文件中的變量 (use excel 2010 to update variables in batch file)

讓這批列出文件名,其路徑為文件 gtr 超過一定大小。如何修改以僅列出沒有路徑和擴展名的文件名? (Got this batch to list filenames with path for files gtr than some size.. How to modify to list only filenames without path and extension?)

從變量中刪除括號(CLI-Windows) (Remove Brackets from Variable (CLI-Windows))

僅在不到一天的文件中搜索字符串 (Search for a string only in files less than a day old)

如何從批處理文件本身中找到已編譯的 bat (.exe) 目錄? (How can you find a compiled bat (.exe) directory from within the batch file itself?)

Windows 批處理腳本:Findstr 輸出:過濾子字符串 (Windows batch script : Findstr output : Filter substring)







留言討論