China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-03 01:01
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Problems with counting View 2,259 Replies 22
Floor 16 Posted 2007-01-07 05:52 ·  中国 辽宁 本溪 联通
银牌会员
★★★
Credits 1,212
Posts 464
Joined 2006-12-13 21:11
19-year member
UID 73417
Gender Male
Status Offline
The principle of this VBS I wrote is like this.
Read the tmp.txt file. If it doesn't exist, it means it's the first run, then create the tmp.txt file and input "1" in it.
If it exists, read the "1" inside, then add 1, rewrite this file. And display the number of times it runs. (Display according to the number in tmp.txt)

BAT is something I'm completely incompetent with. :(
Write my thoughts here for reference only.
Floor 17 Posted 2007-01-07 05:53 ·  中国 北京 联通
银牌会员
★★★
努力做坏人
Credits 1,185
Posts 438
Joined 2006-08-28 12:00
19-year member
UID 61449
From 北京
Status Offline
Actually, this method is not good. If exiting the current CMD SHELL is invalid and the defined variable A is invalid, it is still seeking the best solution

@echo off
if not defined a (set a=1) else (set/a a=%a%+1)
echo P has run %a% times


[ Last edited by 9527 on 2007-1-7 at 05:54 AM ]
我今后在论坛的目标就是做个超级坏人!!!
Floor 18 Posted 2007-01-07 06:24 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
To calculate the number of runs without using the registry or temporary files, not only batch processing can't do it, but also other languages can't. Because the number of uses always needs to be stored in a place that is not easy to lose data, so that it can be read out next time. -- BTW, putting this number of uses in the batch file itself can also be regarded as using a temporary file, but it's just a disguised use.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 19 Posted 2007-01-07 08:15 ·  中国 甘肃 甘南藏族自治州 合作市 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Originally posted by namejm at 2007-1-7 06:24:
  If you calculate the number of runs without using the registry or temporary files, not only batch processing can't do it, but also other languages are impossible to do, because you always have to put the number of uses in a place that is not easy to lose...

Agreed
Floor 20 Posted 2007-01-07 10:07 ·  中国 北京 中国科学院研究生院
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
Since everyone says so, then it will end temporarily. Hehe
Floor 21 Posted 2007-01-07 13:11 ·  中国 河南 许昌 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
There is a shortcoming in the code on my 6th floor: the batch file name cannot contain spaces.

Improvement to allow spaces:

@echo off
for /f "tokens=1,2* delims=:" %%i in ('findstr /nv "`" "%~nx0"') do set /a N=%%i-1
for /f "skip=%N% delims=" %%i in ('type "%~nx0"') do (
for %%n in (%%i) do set /a %%n+=1)
set /a :+=1
echo.&echo. The %:% run. &echo.
set/p=: <nul>>"%~nx0"
pause
exit


The file name cannot contain special symbols!
The compiled batch file must have an empty line at the last line for a newline
Test passed on XP SP2

[ Last edited by zh159 on 2007-1-7 at 12:15 AM ]
Floor 22 Posted 2007-01-07 13:42 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
Didn't pay attention and there's already a good method, heh heh

[ Last edited by 26933062 on 2007-1-7 at 12:48 AM ]
Floor 23 Posted 2007-01-08 01:53 ·  中国 北京 中国科学院研究生院
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
Okay, very good

Is it possible to handle file names with spaces by adding 'type "%~nx0"'?
Forum Jump: