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-07-02 06:54
中国DOS联盟论坛 » 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. View 4,692 Replies 21
Original Poster Posted 2008-02-24 12:48 ·  中国 广东 揭阳 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
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
Floor 2 Posted 2008-02-24 12:49 ·  中国 广东 揭阳 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
This is a good example for a newbie like me to learn for and set!
Floor 3 Posted 2008-02-24 12:50 ·  中国 广东 揭阳 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
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
)
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
abcd +3 2008-02-24 12:52
Floor 4 Posted 2008-02-24 13:13 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
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).
Floor 5 Posted 2008-02-24 14:06 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
It's too boring to just use set /a n=(1+100)*100/2.

Sweat~
The best algorithm... the fastest.
Floor 6 Posted 2008-02-24 14:48 ·  中国 广东 揭阳 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
Hehe... The spirit of exploration is indispensable... Clearly knowing there is a simple one, but still wanting to do other methods
Floor 7 Posted 2008-02-24 14:52 ·  中国 广东 佛山 电信
初级用户
Credits 46
Posts 21
Joined 2006-12-25 14:28
19-year member
UID 74536
Gender Male
From 西安
Status Offline
Floor 8 Posted 2008-02-24 14:55 ·  中国 北京 华为云
银牌会员
★★★
永远的菜鸟
Credits 1,335
Posts 574
Joined 2007-11-27 12:50
18-year member
UID 103929
Gender Male
From 广西
Status Offline
```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%
```
Floor 9 Posted 2008-02-24 15:18 ·  中国 广东 揭阳 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
The code upstairs is so flexible... I'll learn it.
Floor 10 Posted 2008-02-24 16:02 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
@echo off
for /l %%a in (1,1,100) do set /a a+=%%a
echo %a%
pause
Floor 11 Posted 2008-02-24 16:38 ·  中国 广东 揭阳 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
Learned... Feeling ashamed... But still learned!
Floor 12 Posted 2008-02-24 16:41 ·  中国 广东 揭阳 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
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
Floor 13 Posted 2008-02-24 16:45 ·  中国 广东 揭阳 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
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
Floor 14 Posted 2008-02-24 16:52 ·  中国 广东 揭阳 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
I can't understand the code on the ninth floor... Who is willing to explain it? Thanks here
Floor 15 Posted 2008-03-01 18:19 ·  中国 广东 揭阳 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
Why is everyone leaving me... Teach me!
Forum Jump: