在If语句中更改变量值

时间:2018-11-27 20:01:51

标签: windows batch-file

我正在尝试使用一些用户输入定义的变量来决定是否进入循环并执行复制和重命名,然后删除原始文件,因为不再需要它。

set /p multiTune="Does your tune file need to be shared with multiple 
Element sequences? (y/n) "
Echo Multi Tune is %multiTune%
if "%multiTune%"=="y" (set /p tuneCount="How many sequences will need to share your tune file? ")
Echo Tune count is %tuneCount%
pause
if "%multiTune%"=="y" (SET /p tuneName="Enter the file letter/number combination for the R quant of your tune file. ") else (@ECHO The user specified there is no need for a second tune.)
Echo Tune Name is %tuneName%
pause
if "%multiTune%"=="y" (SET /a tuneCount+=1) else(set /a tuneCount-=tuneCount)
Echo Tune count is now %tuneCount%
pause
:loop 
if "%tuneCount%"=="0" goto exitloop
Set /p seqNumber="Enter the number for one of the sequences." 
copy %tuneName%.D.pdf "S%seqNumber%-TUN1_%tuneName%.D.pdf" 
echo %tuneName%.D.pdf renamed to S%seqNumber%-TUN1_%tuneName%.D.pdf
pause
Set /a tuneCount-=1
if "%tuneCount%"=="1" DEL "%tuneName%.D.pdf"
if "%tuneCount%"==1 goto exitloop
Echo %tuneCount%
pause
goto loop
:exitloop

所有回声和暂停仅用于测试目的,以确保变量中的值正确。 批处理文件可以正常运行,其中的变量包含正确的字符串和值,直到该行为止:

if "%multiTune%"=="y" (SET /a tuneCount+=1) else(set /a tuneCount-=tuneCount)

该文件说有什么意外,并在此刻关闭,因此我没有机会弄清循环部分是否有效。 +1的点是这样,它进入循环并执行命令,直到达到1为止;如果等于0,则跳过循环。

我读了一堆关于setlocal延迟扩展的信息,并在变量周围使用!而不是%。我不确定如何实现此功能,或者根本不适用于我的问题。我知道可能有一种更简单的方法来执行if语句,但是我是新手,这是我最容易理解的方法,因为我一直在不断尝试和尝试错误学习,而您所看到的一切都是一天学习的结果。

任何帮助将不胜感激。我试图尽可能详细地说明我要做什么,但是如果您有任何疑问,我会尽力回答。

1 个答案:

答案 0 :(得分:1)

我真的认为您正在使事情变得如此非常复杂 ...

已为您提供了一段经过修改的代码(注意:我没有碰到loop子例程成为主题):

@echo off

choice /m "Does your tune file need to be shared with multiple element sequences? (y/n) " /C:yn /N
rem Echo Multi Tune is %errorlevel%
rem If errorlevel equals to 1 user input is "Y", it is 2 it is "N". (I commented the "echo" command as it changes the errorlevel value).

if errorlevel 2 goto question_N
if errorlevel 1 goto question_Y

:question_Y
set /p tuneCount="How many sequences will need to share your tune file? "
set /p tuneName="Enter the file letter/number combination for the R quant of your tune file. "
SET /a "tuneCount+=1"
goto loop

:question_N
set /p tuneCount="How many sequences will need to share your tune file? "
ECHO The user specified there is no need for a second tune.
set /a "tuneCount-=tuneCount"
goto loop

:loop
rem [Code you provided above]

我希望您对此感到满意,对其进行测试并能正常工作!

相关问题