【问题标题】:Write/read count to txt file写入/读取计数到 txt 文件
【发布时间】:2010-06-06 21:55:49
【问题描述】:

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

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

示例: Count.bat 运行 1 次:

count.txt 没有计数,所以 Count.bat 将值 1 保存在 count.txt 中

Count.bat 运行 2 次:

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

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

我认为这很容易做到,但我不习惯批处理命令

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

【问题讨论】:

    标签: windows batch-file


    【解决方案1】:

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

    @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
    

    【讨论】:

      【解决方案2】:

      从文件 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
      

      【讨论】:

        【解决方案3】:

        模数更容易:

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

        【讨论】:

          【解决方案4】:
          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 岁了。

          【讨论】:

          • 现在可以了,它甚至将 6 重置为 1。
          猜你喜欢
          • 2015-06-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-05-29
          • 1970-01-01
          • 1970-01-01
          • 2021-02-06
          • 2021-07-15
          相关资源
          最近更新 更多