将变量设置为等于文本文件批处理中的数字 [英] Set variable equal to a number in a text file Batch

查看:58
本文介绍了将变量设置为等于文本文件批处理中的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理文件( tickets.bat ),每次创建不同的票证时,通过设置多个变量并将这些变量回显到文本文件中,我希望票证号增加我尝试通过创建一个 number.txt 文件来实现此目的,并且每次您遍历循环并创建新票证时,都会将%pnumber%设置为文本文件,然后将%number%设置为%pnumber%加1.但是,即使 number.txt 文件包含数字1,并且保持不变.这就是我所拥有的:

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

看来我的问题是%pnumber%始终设置为0.除非我缺少其他内容.基本上,我需要它始终将%number%增加1,即使您关闭了Batch作业并再次将其打开,所以这就是为什么我要使用 .txt 文件.在文本文件中看不到数字吗?它是一行上的一个数字.谢谢.

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.

推荐答案

像这样尝试:

@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

这篇关于将变量设置为等于文本文件批处理中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆