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-01 21:15
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Can you write an automatic batch command that executes after the computer is started a specified number of times? View 3,636 Replies 15
Original Poster Posted 2006-05-11 17:43 ·  中国 广东 广州 电信
新手上路
Credits 14
Posts 5
Joined 2006-05-11 17:28
20-year member
UID 55336
Gender Male
Status Offline
For example, the computer records that after starting 50 times, it will automatically format the hard disk, haha
Floor 2 Posted 2006-05-11 22:57 ·  中国 四川 成都 联通
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
Wow, there are always people asking this kind of question on the union~~
Floor 3 Posted 2006-05-12 12:59 ·  中国 广东 广州 电信
新手上路
Credits 14
Posts 5
Joined 2006-05-11 17:28
20-year member
UID 55336
Gender Male
Status Offline
Ah, is there anyone else asking?
Floor 4 Posted 2006-05-12 14:12 ·  中国 北京 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
Hehe, actually this is very easy to achieve on systems like 2000/XP/2003. But I haven't thought about formatting. However, it's possible to set what command to execute after restarting or powering on the machine a specified number of times.
Floor 5 Posted 2006-05-12 15:09 ·  中国 北京 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
@echo off
set i=1
if not exist c:\test.txt goto A

:C
if exist c:\test.txt goto D

:A
echo ^0>test.txt
goto C

:D
for /f %%i in (c:\test.txt) do set /a k=%%i+%i%
if "%k%"=="50" (goto B) else echo %k% 1>c:\test.txt&goto :eof

:B
echo SUCCESSFUL .... ....
This is the program you want to execute

Roughly written, it seems it can be more concise, but I can't guarantee whether people will run this batch script after entering the system. In short, it runs once and decreases by one. Hehe, just put this in the startup.

Test environment WINDOWS XP & WINDOWS 2000
Floor 6 Posted 2006-05-12 15:44 ·  中国 广东 广州 电信
新手上路
Credits 14
Posts 5
Joined 2006-05-11 17:28
20-year member
UID 55336
Gender Male
Status Offline
Ha, thanks first. I'll input it into the batch processing tomorrow and use it. I'll ask you if there's any problem.
Floor 7 Posted 2006-05-12 15:47 ·  中国 广东 广州 电信
新手上路
Credits 14
Posts 5
Joined 2006-05-11 17:28
20-year member
UID 55336
Gender Male
Status Offline
By the way, it should be okay to use this program under DOS 6.22, right?
Floor 8 Posted 2006-05-12 16:09 ·  中国 北京 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
What I said is already very clear, and I also wrote down the test environment. DOS 6.22 is not acceptable............
Floor 9 Posted 2006-05-12 16:22 ·  中国 广东 广州 白云区 电信
金牌会员
★★★★
D◎$ Fαп
Credits 4,562
Posts 1,883
Joined 2004-01-19 00:00
22-year member
UID 15812
Gender Male
From 广东广州
Status Offline
Hehe, it seems that there are more and more posts about using DOS to cause trouble in the forum now.
----====≡≡≡≡ 我的至爱,永远是MSDOS!≡≡≡≡====----
Floor 10 Posted 2006-05-12 16:36 ·  中国 广东 广州 电信
新手上路
Credits 14
Posts 5
Joined 2006-05-11 17:28
20-year member
UID 55336
Gender Male
Status Offline
Of course, it's to restrict the user. Why cause destruction for no reason!
Floor 11 Posted 2006-05-12 19:23 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
Hehe, the method of using formatting to restrict usage is quite clever, but as long as there's interest, why not start from causing damage?

@echo off
set /p num=<num.txt || echo 1 > num.txt
if %num% equ 50 goto something
set /a num = %num% + 1
echo %num% > num.txt
echo This is the %num%th run...
pause
goto :eof

:something
echo 50 runs are up... & pause

The above demonstration script runs under XP SP2. If you want to write one that runs under DOS, it's not impossible, but the amount of code is relatively larger, so it's skipped. Here are some hints:

@echo off
dir *.cxs
if errorlevel 1 echo ^&*HUIT^&%T^&%& > 1.cxs
if exist 50.cxs goto something
ren 48.cxs 49.cxs
ren 47.cxs 48.cxs
.
.
.
.
ren 1.cxs 2.cxs
goto :exit

:something
echo 50 runs have been made.

:exit
Floor 12 Posted 2006-05-13 17:16 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re zwzhb:

Actually, the batch script that triggers an event based on the number of startups is already close to the detection mechanism of a virus. But in fact, it's very simple to implement, and there are many solutions even under DOS. So, it was the first module I completed when I was researching batch pseudo-viruses in the early days.

Just for the purpose of controlling the spread speed of dangerous technologies, I can only provide a general idea, not the source code. In fact, it's very hard to find my past source code. The premise of judging whether to trigger an event based on the number of startups is to measure the number of startups. There are various algorithms to obtain and detect this number.

For example, the solution of bagpipe and Brother 3742668 involving saving the number of startups to a variable and incrementing it one by one. Under DOS, because it's complicated to add and subtract environment variables, this method can be slightly modified. Convert the number of startups into the number of lines in a temporary file (detectable with find) or the total number of bytes (usually 3 times the number of times) for saving and detection.

Or, you can also use the stack method in Brother 3742668's second solution to save the number of startups in the file name. When actually implementing it, you don't need to rename and stack line by line. A simple command line loop can complete this task.

Re bagpipe:

I feel that your code affects the simplicity and readability of the program because of taking care of the sequentiality in the program structure.

For example, if not exist c:\test.txt goto A and if exist c:\test.txt goto D have code redundancy. And the four label segments A, B, C, D and the corresponding goto also make the program structure complicated. After careful consideration, I think these labels can all be omitted because the existence judgment of c:\test.txt can be completed at the same time by slightly modifying the for statement, and other jumps and branches can also be simplified.

In addition, brother uses the variable i to save the incremented number 1, which should have special considerations, but I can't guess it.

Finally, I remember that brother once posted a signature picture of "bagpipe musical note letters". Can you give me a copy for collection? If you are interested, please send it directly to my email. Thank you very much!

Re 3742668:

Is it more convenient to directly write the set /a sentence into the temporary batch file for saving? For example:


if exist _bootnum.cmd (call _bootnum.cmd) else set bootnum=1
echo set /a bootnum=%bootnum%+1>_bootnum.cmd
if bootnum equ 50 goto .....


To Wengier:

This topic can be transferred to "Batch Processing Room" for further discussion. Please help transfer it a little.

[ Last edited by willsort on 2006-5-13 at 17:23 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 13 Posted 2006-05-13 19:24 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
Re regarding the sort:
After reading your post, I feel that your style still leans towards DOS, while mine undoubtedly leans towards CMD.
I believe many people will have such an idea: try not to generate temporary files when writing batch scripts. Although this is actually not a big problem and can be ignored compared to each system startup and disk swapping, there are still many friends who like to use for to operate the output handle. And before I wrote this batch script under WINDOWS, I naturally catered to the public, focusing on not generating temporary files. In this example, there are actually several ways to not generate temporary files. Personally, I think the simplest and most convenient one is to use the file streams of the NTFS format. So at the beginning, I planned to write the startup count into a non-mainstream part of %0 of the file itself, but halfway through writing, I thought that not everyone uses NTFS formatted partitions. For "compatibility", I still wrote it into a file.
Of course, if you want to not generate temporary files, there are actually quite a few methods. You can use for, find, and event log commands, etc.
I'm replying to this post mainly to understand:
Or you can also use the stack method in brother 3742668's second solution to save the startup count in the file name. When actually implementing it, you don't need to rename and stack line by line, and you can complete this task using a simple command line loop.

I wonder if you can give the idea of the command line loop under DOS. Since I don't have a DOS environment here, I can't know what parameters each command has under DOS, so I have only been able to mention ideas about batch processing under DOS and haven't dared to be overconfident and make things up. Please post the necessary commands and the parameters needed, and as for the specific code, it's better to write it yourself to be more profound.
Floor 14 Posted 2006-05-13 23:36 ·  中国 上海 电信
中级用户
★★
大师兄
Credits 377
Posts 99
Joined 2005-08-26 07:37
20-year member
UID 41945
Status Offline
I really admire the board master willsort in terms of DOS batch processing. The thinking is quite meticulous, the script versatility is also relatively good, and there are some tricks that I have never heard of before, which is really eye-opening : )

[ Last edited by tigerpower on 2006-5-13 at 23:38 ]
Floor 15 Posted 2006-05-14 19:44 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re 3742668:

The determination of the number of startups, no matter which algorithm is adopted, requires setting a "marker", and this marker will not be lost after shutting down. So the only option left for us seems to be disk files. If we don't want to use temporary files, the system file or the batch file itself may be a place to hide the "marker", but accessing it is relatively complicated.

The "command line loop" I mentioned actually refers to passing the traversal object to itself through command line parameters, and then using the shift+goto loop. When I later wrote incrementing or traversal loops under DOS, I basically mostly used this method because it is more flexible than for. Even compared with CMD's FOR, it has the advantage of being able to refer to multiple traversal objects before and after at a certain moment. This method can also be implemented under CMD, and it will be simpler because of the characteristics of CALL.

For examples under DOS, you can refer to:

14512-Rename 100 sequential files
http://www.cn-dos.net/forum/viewthread.php?tid=14512

[ Last edited by willsort on 2006-5-14 at 20:24 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Forum Jump: