Windows Batch Variables Won't Set

前端 未结 2 990
广开言路
广开言路 2020-11-22 08:04

I think I ran into a bug in Window\'s batch scripting.

I cannot set variables that are in an expanded if statement.

Here is an isolated part of my script:

2条回答
  •  不要未来只要你来
    2020-11-22 08:30

    You missed something ;-)

    cmd expands variables when commands are parsed, not when they are run. It so happens that an if or for statement with a block ( ... ) (or actually any block) counds as a single command in that case. So when you set variables inside a block and try using them in the same block there are no variables anymore – they were replaced by the values the variables had before the block even executed.

    Stick a

    setlocal enabledelayedexpansion
    

    at the start of your batch file and use !zip! instead of %zip%. See help set for a detailed discussion of the problem.

提交回复
热议问题