问题
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