写入/读取计数到txt文件

时间:2010-06-06 21:55:49

标签: windows batch-file

我需要一个将计数编号写入txt文件的批处理文件。

下次运行批处理文件时,它应该从txt文件中读取当前计数,并添加1进行计数并将此新值保存在txt文件中。 (txt文件中没有其他内容) 当count>> 5时,它应该从1再次开始

实施例: Count.bat运行一次:

count.txt没有计数,因此Count.bat将值1保存在count.txt

Count.bat运行2次:

Count.bat从count.txt读取1并将新值2保存到count.txt

当count.bat运行6次时,它应该通过在count.txt中保存值1来重新开始

我认为这很容易,但我不习惯批量命令

所以希望有人可以帮助我。

4 个答案:

答案 0 :(得分:0)

我知道这不是您要求的,但您可能想要创建单独的文件:

@echo off

if not exists count.1 goto l1
if not exists count.2 goto l2
if not exists count.3 goto l3
if not exists count.4 goto l4
if not exists count.5 goto l5

del count.*

rem -- fall trhough -- and create first count-file

:l1
echo . > count.1
goto end

:l2
echo . > count.2
goto end

:l3
echo . > count.3
goto end

:l4
echo . > count.4
goto end

:l5
echo . > count.5

rem -- fall through -- goto end

:end

答案 1 :(得分:0)

从文件temp.txt开始,其中一行只包含一个没有括号的一(1)。

   for /f "eol=# tokens=* delims=," %%i in (temp.txt) do (
     set /A Count = %%i + 1
     If '%Count%' == '6' (set /A Count = 1)
   )
   echo %Count% > temp.txt

答案 2 :(得分:0)

if not exist count.txt (
    echo 1 > count.txt
    exit
)
< count.txt set /p count=
if %count% equ 6 (
    echo 1 > count.txt
    exit
)
set /a count=count+1
echo %count% > count.txt

说实话;我只写了这个答案,因为这个问题是6岁。

答案 3 :(得分:0)

使用模数更容易:

@echo off & setlocal
(<count.txt set /p count=) 2>nul
set /a count=count %% 5 + 1
>count.txt echo %count%
type count.txt