Set variable equal to a number in a text file Batch

回眸只為那壹抹淺笑 提交于 2019-12-25 01:49:38

问题


I have a batch file (tickets.bat) and each time you create a different ticket, by setting multiple variables and echoing the variables to a text file, I want the ticket number to go up by 1. I tried to do this by creating a number.txt file and each time you go through the loop and create a new ticket, it sets %pnumber% to the text file, and then sets %number% to %pnumber% plus 1. But %pnumber% keeps setting to 0, even when the number.txt file contains the number 1 and doesn't change. This is what I have:

:start
cls
echo Enter Ticket Info Here:
set /p name="Name:"
echo Press Enter to Show Ticket Preview
set /a pnumber=C:\Batch\ticket\number.txt
set /a number=%pnumber%+1
echo %number% > "C:\Batch\ticket\number.txt"
echo %name% Ticket Number %number%
pause
goto start

It looks like my problem is that %pnumber% always sets to 0. Unless I'm missing something else. Basically, I need it to always increase %number% by 1, even if you close the Batch job and open it up again, so that's why I went to using a .txt file. Is it not seeing the number in the text file or something? It's one number on one line. Thank you.


回答1:


Try Like this :

@echo Off
:start
cls
echo Enter Ticket Info Here:
set /p name="Name:"
echo Press Enter to Show Ticket Preview
set /p pnumber=<C:\Batch\ticket\number.txt
set /a number=%pnumber%+1
echo %number% >C:\Batch\ticket\number.txt
echo %name% Ticket Number %number%
pause
goto start


来源:https://stackoverflow.com/questions/27094750/set-variable-equal-to-a-number-in-a-text-file-batch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!