2010-06-06 386 views
0

我需要一個將計數編號寫入txt文件的批處理文件。寫入/讀取計數到txt文件

下次運行批處理文件時,它應該從txt文件讀取當前計數並添加1進行計數並將此新值保存到txt文件中。 (閒來無事在txt文件) 當計數> 5,應該從1開始再次

例子: Count.bat運行1次:

count.txt沒有計數,以便Count.bat節省在count.txt值1

Count.bat運行2時間:

Count.bat從count.txt讀取1和保存新的值2至count.txt

當count.bat運行了6次,它應該sta在count.txt

我認爲這僅僅是很容易做到節約價值1室溫結束了,但我'不使用批處理命令

所以希望這裏有人可以幫助我。

回答

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 
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 
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歲。

+0

......雖然這是行不通的。 – Stephan 2016-08-19 05:49:09

+0

現在,它甚至將6重置爲1。 – 2016-08-19 13:53:07

0
與模

簡單:

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