Can I have an IF block in DOS batch file?

前端 未结 5 2147
梦谈多话
梦谈多话 2020-12-05 03:41

In a DOS batch file we can only have 1 line if statement body? I think I found somewhere that I could use () for an if block just like the {} used

5条回答
  •  执念已碎
    2020-12-05 04:26

    Instead of this goto mess, try using the ampersand & or double ampersand && (conditional to errorlevel 0) as command separators.

    I fixed a script snippet with this trick, to summarize, I have three batch files, one which calls the other two after having found which letters the external backup drives have been assigned. I leave the first file on the primary external drive so the calls to its backup routine worked fine, but the calls to the second one required an active drive change. The code below shows how I fixed it:

    for %%b in (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 "%%b:\Backup.cmd" %%b: & CALL "%%b:\Backup.cmd"
    )
    

提交回复
热议问题