批处理文件 - 如何从文件复制文本,然后将其更新为新值
每次打开批处理文件时,我都希望它读取当前存储在文本文件中的信息,然后将其删除的存储信息应用于计算新整数。
我试图弄清楚如何从文本文件中复制数字,以变量存储,然后更新到该文本文件中的新整数,例如将1添加到值中。 我一直在网上筛选信息,一切似乎都指向不同的方向。
这是我从这样挖掘出来的测试代码:
set file="Test.txt"
set /a _Counter =< %file%
echo:%_Counter%
set /a "_Update=%_Counter%+1"
echo:%_Update% >%file%
timeout /t 10
出于某种原因,当我尝试获取计数器的信息时,它不会从文本文件中提取任何数据通过批处理文件。
F:\Users\Test\Documents\JumbledDirectory> set /a _Counter = Directory\Test.txt 0<F:\Users\Test\Documents\Jumbled
我看到的最常见的答案是在此处看到的内容
set /p _Counter=< Test.txt
echo %_Counter%
: Windows batch命令命令从文本文件读取第一行,
但是在执行此操作之后,我要么最终
echo:%_Counter%
变得完全空白,要么每次默认为0。
任何帮助都将不胜感激,因为我很遗憾地试图找到如何在6个小时内找到该简单功能。
Each time I open the batch file, I would like it to read the information currently stored in the text file, and then apply that stored information it pulled to calculating a new integer.
I'm trying to figure out how to get a number copied from a text file, stored as a variable, and then updated to a new integer in that text file, say adding 1 to the value.
I've been sifting through information online, and everything seems to point in a different direction.
Here is a test code I've gotten from digging thus-far:
set file="Test.txt"
set /a _Counter =< %file%
echo:%_Counter%
set /a "_Update=%_Counter%+1"
echo:%_Update% >%file%
timeout /t 10
For some reason when I try to get the information for the counter, it doesn't pull any data from the text file, and I'm left with this line output by the batch file.
F:\Users\Test\Documents\JumbledDirectory> set /a _Counter = Directory\Test.txt 0<F:\Users\Test\Documents\Jumbled
The most common answer I've seen is to use something along the lines of
set /p _Counter=< Test.txt
echo %_Counter%
As seen here: Windows batch command(s) to read first line from text file
But upon doing this I've either ended up with
echo:%_Counter%
being completely blank, or it defaults to 0 each time.
Any help would be appreciated as I've sadly been trying to find how to get this simple function for around 6 hours now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您使用点单击和giggle的方法执行批处理时,如果找到语法 - 错误或脚本运行到完成,则批处理窗口将关闭。您 can 在语句之后放置
暂停
在错误中回家,但最好打开'命令提示符'并从那里运行批处理,以便窗口保持打开状态,并显示任何(错误)消息。错误消息将是关于缺少操作数的。
当我跟踪使用其自己的数据集回答的每个问题时,我更改了文件名。
使用
设置“ var = value”
设置字符串值 - 这避免了落后空间引起的问题。不要分配终端\
,space或“
- 从元素构建路径名 - 违反直觉,它可能会使过程更容易。set> set/a < /code>不希望从任何地方进行输入,它只是执行计算,而不是必须删除
%< /code>。
,如果您使用的是delayeredexpansion
set> set/p 期望输入,所以我用它来读取该文件> set/a 无视空间,但是
set
和set/p
将在=
之前包括在分配的variablename中,_Counter
&amp;_counter
是不同的变量。When you use the point-click-and-giggle method of executing a batch, the batch window will close if a syntax-error is found or the script runs to completion. You can put a
pause
after statements and home in on the error, but better to open a 'command prompt' and run your batch from there so that the window remains open and any (error) messages will be displayed.The error message would be about missing operand.
I've changed the filename as I track each question I respond to with its own set of data.
Use
set "var=value"
for setting string values - this avoids problems caused by trailing spaces. Don't assign a terminal\
, Space or"
- build pathnames from the elements - counterintuitively, it is likely to make the process easier.set /a
does not expect input from anywhere, it simply performs the calculation and assigns the result. Quotes are not necessary so I've removed them.%
are also not required in aset /a
but can be required if you are usingdelayedexpansion
.set /p
expects input, so I've used that to read the file. Note thatset /a
disregards spaces, butset
andset /p
will include the space before the=
in the variablename assigned, and_Counter
&_Counter
are different variables.因此
So having the batch in the same directory as the textfile this will work: