Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 批处理文件在var中读取并写回_Batch File - Fatal编程技术网

Batch file 批处理文件在var中读取并写回

Batch file 批处理文件在var中读取并写回,batch-file,Batch File,每次关闭或重新启动计算机时,我都要数数。因此,我相信我可以通过将批处理文件添加到“开始”菜单来实现这一点。所以每次你打开电脑,它都会运行。 当它运行时,它应该 open c:\count.txt read in the value on that text file add 1 to it write the value to the text file exit. 但是我没有太多地使用批处理文件,也不知道如何从文本文件中读取数字 这是MS-DOS时代的解决方案。c:\count.txt文件大

每次关闭或重新启动计算机时,我都要数数。因此,我相信我可以通过将批处理文件添加到“开始”菜单来实现这一点。所以每次你打开电脑,它都会运行。 当它运行时,它应该

open c:\count.txt
read in the value on that text file
add 1 to it
write the value to the text file
exit.

但是我没有太多地使用批处理文件,也不知道如何从文本文件中读取数字

这是MS-DOS时代的解决方案。c:\count.txt文件大小对于任何实际数字来说都不是问题

要重置计数器,请删除c:\count.txt

@echo off
>>c:\count.txt echo 1
echo Count is up to:
find /c "1" <c:\count.txt
@echo关闭
>>c:\count.txt echo 1
回波计数最多为:
根据您的想法查找/c“1”(更新文件中的计数器):

rem open c:\count.txt
rem读取该文本文件上的值
set/p count=c:\count.txt echo。%count%
快速眼动退出。
出口

注意:请注意,您要选择一个用户有写访问权的路径。(
C:\
没有特权不能工作)

或者,您可以使用
REG
SETX
将此类数据存储在注册表中。我需要对电脑的影响最小,干扰REG是有风险的,或者我认为是这样。好的。我个人更喜欢它写驱动根路径。我不明白这一点,这将读取count.txt并回显第一行,对吗?不确定>>c:\count行的作用或查找结果。请尝试几次,然后检查文件。这应该让它更清楚。哈哈,不优雅,但功能@GlenMorse:批处理的每次运行都会在文件中添加一行
1
find
计算
1
s(
/c
)是“set/p”和“set/a”的好例子。我喜欢这样,它对我来说既快又容易理解:D我有一个问题,那就是如果1。count.txt文件被删除了吗?它是否会再次创建该文件。它会给出一个错误导致它是一个空白文件吗?它还会增加1。。我想我可以测试一下。只是重新启动一台计算机,或者重新启动这台计算机是我不想做的事情,因为它上面运行着应用程序。但如果你不能回答这些问题,我会的。如果count.txt不存在,
set/p
将给出一个错误。然而,
set/a
将正确地“添加”一个@谢谢你的编辑:)
rem open c:\count.txt
rem read in the value on that text file
set /p count=<c:\count.txt
rem add 1 to it
set /a count+=1
rem write the value to the text file
>c:\count.txt echo.%count%
rem exit.
exit