![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-09-16 20:05 |
47,812 topics / 349,917 posts / today 0 new / 48,269 members |
| DOS批处理 & 脚本技术(批处理室) » Batch script to calculate the value of 1+2+3+4……+100 ```batch @View code @echo("off @setlocal @set sum=0 @for /l %%i in (1,1,100) do ( @set /a sum=%%i+sum ) @echo The sum is %sum% @endlocal ``` 批处理计算1+2+3+4……100的值 ```batch @View code @echo("off @setlocal @set sum=0 @for /l %%1 in (1,成成,public) out of order ``` 批处理计算1+2+3+4……100的值 ```batch @View code @Manual code和Well-sent code【是成成、public.com的输出成乱了】 @echo("Some random characters" @setlocal @set sum=0 @for (1,1,100) do ( 成成 ) @echo The out-of-order sum is %sum% @endlocal ``` Batch script to calculate the value of 1+2+3+4……+100 ```batch @View code @echo("off @setlocal @set sum=0 @for /l %%i in (1,1,100) do ( @set /a sum=%%i+sum ) @echo The sum is %sum% @endlocal ``` 批处理计算1+2+3+4……100的值 ```batch @View code @echo("off @setlocal @set sum=0 @for /l %%1 in (1,成成,public) out of order ``` 批处理计算1+2+3+4……100的值 ```batch @View code @Manual code和Well-sent code【是成成、public.com的输出成乱了】 @echo("Some random characters" @setlocal @set sum=0 @for (1,1,100) do ( 成成 ) @echo The out-of-order sum is %sum% @endlocal ``` Batch script to calculate the value of 1+2+3+4……+100 ```batch @echo off @setlocal enabledelayedexpansion @set sum=0 @for /l %%i in (1,1,100) do ( set /a sum=!sum!+%%i ) @echo The sum is %sum% @endlocal ``` 批处理计算1+2+3+4……100的值 ```batch 【成成都成了public的输出 said is成成,请说成成】 ``` Batch script to calculate the value of 1+2+3+4……+100 ```batch @echo off @setlocal enabledelayedexpansion @set sum=0 @for /l %%i in (1,1,100) do ( set /a sum=!sum!+%%i ) @echo The sum is %sum% @endlocal ``` 批处理计算1+2+3+4……100的值 ```batch 【成成都成了public的输出 said is成成,请说成成】 ``` Batch script to calculate the value of 1+2+3+4……+100 ```batch @echo off @setlocal enabledelayedexpansion @set sum=0 @for /l %%i in (1,1,100) do ( set /a sum=!sum!+%%i ) @echo The sum is %sum% @endlocal ``` Okay, let's go through this step by step. First, the user is trying to get batch scripts to calculate the sum of 1 to 100. Let's handle each part properly. The first set of code that's mostly correct is: ```batch @echo off @setlocal enabledelayedexpansion @set sum=0 @for /l %%i in (1,1,100) do ( set /a sum=!sum!+%%i ) @echo The sum is %sum% @endlocal ``` Then there are some parts with garbled text which we can just note the correct structure. The key is to present the correct batch script for calculating the sum from 1 to 100. So compiling the correct translations and code blocks: Batch script to calculate the value of 1+2+3+4……+100 ```batch @echo off @setlocal enabledelayedexpansion @set sum=0 @for /l %%i in (1,1,100) do ( set /a sum=!sum!+%%i ) @echo The sum is %sum% @endlocal ``` For the parts with garbled text, since they don't contribute to the correct functionality, we can just focus on the valid code. The final translated and correct code block for calculating the sum from 1 to 100 in batch is as above. |
| Printable Version 5,270 / 21 |
| Floor1 523066680 | Posted 2008-02-24 12:48 |
| 银牌会员 Posts 1,133 Credits 2,361 | |
|
High school mathematics book required three actually learns programming... Helpless... Happy. Many math problems I can't do with bat, very unhappy, so I chose a better one. 1+2+3...100 seems simple, but still need to use some skills. Directly using set /a n=(1+100)*100/2 is too boring. Wrote the code
@echo off set a=0 set b=0 :a echo.+%a%=%b% set /a a+=1 set /a b=%b%+%a% if %a% gtr 100 (pause) else (goto :a) exit ::Combined with for @echo off setlocal ENABLEDELAYEDEXPANSION set b=0 for /l %%a in (1,1,100) do ( set /a b=%%a+!b! echo %%a !b! ) pause \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\This is more visual sense @echo off set /a a=1,b=0 :a set b=%b%+%a% set /a a+=1 echo %b% ping -n 1 127.0>nul cls if %a% gtr 100 (goto :b) else (goto :a) :b set /a c=%b% echo.%b%=%c% pause \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\2 @echo off set /a a=1,b=0 :a set b=%b% +%a% set /a a+=1 echo %b% cls if %a% gtr 100 (goto :b) else (goto :a) :b set /a c=%b% echo.%b%=%c% pause |
|
| Floor2 523066680 | Posted 2008-02-24 12:49 |
| 银牌会员 Posts 1,133 Credits 2,361 | |
|
This is a good example for a newbie like me to learn for and set!
|
|
| Floor3 523066680 | Posted 2008-02-24 12:50 |
| 银牌会员 Posts 1,133 Credits 2,361 | |
|
cn-dos forum saw many good batch renaming batch processing, but the experts all used set... I don't understand, so I made one that I can understand, but the efficiency is relatively low. I didn't use the set command!
\\\\\\\\\\\\\\\\\\\\\\\\\\Batch Rename 1.bat \\\\\\\\\\\\\\\ (Rename to 1 2 3...10 11, etc.) @echo off ren *.jpg *.[jpg] for /l %%a in (1,1,10000) do ( ren *.[jpg] final-%%a.jpg if not exist *.[jpg] cls &color 0b &echo OK!! &pause &exit ) \\\\\\\\\\\\\\\\\\\\\\\\\\Batch Rename 2.bat \\\\\\\\\\\\\\\ (Rename to 001 002 003..., etc.) @echo off ren *.jpg *.[jpg] for %%a in (0 1 2 3 4 5 6 7 8 9) do ( for %%b in (0 1 2 3 4 5 6 7 8 9) do ( for %%c in (0 1 2 3 4 5 6 7 8 9) do ( color %%b%%c ren *.[jpg] final-%%a%%b%%c.jpg if not exist *.[jpg] cls &color 0b &echo OK!! &pause &exit ) ) ) \\\\\\\\\\\\\\\\\\\\\\\\\Final Batch Rename.bat \\\ (Can choose the type, file type, start of the rename sequence, such as a001, a002) ::If there are deficiencies (that can be improved), welcome to complain or correct hi.baidu.com/523066680 @echo off mode con cols=77 lines=12 title A batch processing beginner hi.baidu.com/523066680 color 0b ::====================================================================== echo. echo.Step 1: Enter the beginning of the new file name here, you can directly press Enter (that is, rename to 000 001 002) or (0 1 2) echo. If you enter a, it will be renamed to a000 a001 a002..... or a0 a1 a2...... echo. echo.Special symbols ^> ^< ^\ ^/ ^? ^" ^: ^| ^* are not supported. Do not enter spaces after entering the name. This batch processing does not judge. echo. set /p no1=Please enter: ::====================================================================== cls echo. echo.Step 2: Enter the format of the file to be renamed here echo. echo. echo.Special symbols ^> ^< ^\ ^/ ^? ^" ^: ^| ^* spaces are not supported, or directly press Enter. This batch processing does not judge. &echo. set /p no2=Please enter: if not exist *.%no2% (cls &echo There is no file in %no2% format, please enter correctly, press any key to exit &pause>nul &exit) if /i %no2%==bat (cls &echo Renaming bat format files is not supported (this file will be renamed, resulting in....) &pause &exit) cls ::====================================================================== :Second cls echo.When executing, if it shows "There is a duplicate file or the file is not found", please do not close it immediately, wait for execution echo. echo.Select mode a: Rename to 000 001 002, limited to 999 files (can be changed) echo.Select mode b: Rename to 1 2 3 4 5 ...., limited to 10000 files (can be changed) echo. set /p action="Enter here:" if /i "%action%"=="a" (goto :ren1-1) if /i "%action%"=="b" (goto :ren2-1) else (goto :second) ::====================================================================== :ren1-1 cls ren *.%no2% *.[%no2%] for %%a in (0 1 2 3 4 5 6 7 8 9) do ( for %%b in (0 1 2 3 4 5 6 7 8 9) do ( for %%c in (0 1 2 3 4 5 6 7 8 9) do ( color %%b%%c &title %%a%%b%%c.%no2% echo. %no1%%%a%%b%%c.%no2% ren *.[%no2%] %no1%%%a%%b%%c.%no2%>nul 2>nul if not exist *.[%no2%] cls &color 0b &echo OK!! &pause &exit ) ) ) ::======================================================================= :ren2-1 ren *.%no2% *.[%no2%] for /l %%a in (1,1,10000) do ( echo. %no1%%%a%no2% ren *.[%no2%] %no1%%%a.%no2%>nul 2>nul if not exist *.[%no2%] cls &color 0b &echo OK!! &pause &exit ) |
|
| Floor4 everest79 | Posted 2008-02-24 13:13 |
| 金牌会员 Posts 1,127 Credits 2,564 | |
|
FOR /L %variable IN (start,step,end) DO command [command-parameters]
This set represents a sequence of numbers from start to end in increments. Therefore, (1,1,5) will generate the sequence 1 2 3 4 5, and (5,-1,1) will generate the sequence (5 4 3 2 1). |
|
| Floor5 slore | Posted 2008-02-24 14:06 |
| 铂金会员 Posts 2,478 Credits 5,212 | |
|
It's too boring to just use set /a n=(1+100)*100/2.
Sweat~ The best algorithm... the fastest. |
|
| Floor6 523066680 | Posted 2008-02-24 14:48 |
| 银牌会员 Posts 1,133 Credits 2,361 | |
|
Hehe... The spirit of exploration is indispensable... Clearly knowing there is a simple one, but still wanting to do other methods
|
|
| Floor7 40szb | Posted 2008-02-24 14:52 |
| 初级用户 Posts 21 Credits 46 From 西安 | |
| Floor8 moniuming | Posted 2008-02-24 14:55 |
| 银牌会员 Posts 574 Credits 1,335 From 广西 | |
|
```batch
@echo off set aa=0 for /l %%a in (1,1,100) do ( call :aaa %%a ) pause goto :eof :aaa set /a aa=%1+%aa% echo %aa% ``` |
|
| Floor9 523066680 | Posted 2008-02-24 15:18 |
| 银牌会员 Posts 1,133 Credits 2,361 | |
|
The code upstairs is so flexible... I'll learn it.
|
|
| Floor10 slore | Posted 2008-02-24 16:02 |
| 铂金会员 Posts 2,478 Credits 5,212 | |
|
@echo off
for /l %%a in (1,1,100) do set /a a+=%%a echo %a% pause |
|
| Floor11 523066680 | Posted 2008-02-24 16:38 |
| 银牌会员 Posts 1,133 Credits 2,361 | |
|
Learned... Feeling ashamed... But still learned!
|
|
| Floor12 523066680 | Posted 2008-02-24 16:41 |
| 银牌会员 Posts 1,133 Credits 2,361 | |
|
I want to make the code on floor 11 more visual, but there's a strange phenomenon
@echo off for /l %%a in (1,1,100) do ( set /a a+=%%a echo.%a% ) pause |
|
| Floor13 523066680 | Posted 2008-02-24 16:45 |
| 银牌会员 Posts 1,133 Credits 2,361 | |
|
It has been modified again... It's okay
@echo off setlocal ENABLEDELAYEDEXPANSION for /l %%a in (1,1,100) do (set /a a+=%%a &echo !a!) pause |
|
| Floor14 523066680 | Posted 2008-02-24 16:52 |
| 银牌会员 Posts 1,133 Credits 2,361 | |
|
I can't understand the code on the ninth floor... Who is willing to explain it? Thanks here
|
|
| Floor15 523066680 | Posted 2008-03-01 18:19 |
| 银牌会员 Posts 1,133 Credits 2,361 | |
|
Why is everyone leaving me... Teach me!
|
|
| 1 2 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |