标题: [文本处理] 批处理怎样把txt每行数值除以100? [打印本页]
作者: shchaoge 时间: 2023-6-11 14:00 标题: 批处理怎样把txt每行数值除以100?
1.txt每行数值除以100,结果输出到新的2.TXT,这个批处理代码怎么写,求助
9300000
1730000
400000
770000
390000
0
110000
1250000
80000
30000
170000
360000
360000
10000
40000
30000
33450000
0
210000
400000
350000
作者: BingjianREMIX 时间: 2023-6-11 14:03
回复 1# shchaoge - @echo off
-
- set inputFile=1.txt
- set outputFile=2.txt
-
- if exist "%outputFile%" del "%outputFile%"
-
- for /f %%a in ('type "%inputFile%"') do (
- set /a "result=%%a/100"
- echo !result!>>"%outputFile%"
- )
-
- echo Done!
-
- pause
复制代码
脚本会首先将 1.txt 中的每一行内容读取到 result 变量中,并将其除以 100 得到计算结果。然后将每次循环得到的 result 变量值追加到 2.txt 文件中。如果 2.txt 已经存在,则在执行前将其删除,以免原来的内容对结果产生影响。
ps:不支持处理浮点数,只能处理整数,请不要包含任何空格或其他无关字符。
作者: shchaoge 时间: 2023-6-11 14:12
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
结果如上?
作者: BingjianREMIX 时间: 2023-6-11 14:15
回复 3# shchaoge
额,还没有测试,我看看
作者: BingjianREMIX 时间: 2023-6-11 14:17
本帖最后由 BingjianREMIX 于 2023-6-11 14:19 编辑
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
...
shchaoge 发表于 2023-6-11 14:12
好了:- @echo off
- cd /d %~dp0
- setlocal EnableDelayedExpansion
- set "inputFile=1.txt"
- set "outputFile=2.txt"
-
- if exist "%outputFile%" del "%outputFile%"
-
- for /f %%a in ('type "%inputFile%"') do (
- set /a "result=%%a/100"
- echo !result!>>"%outputFile%"
- )
-
- echo Done!
-
- pause
复制代码
忘记加setlocal EnableDelayedExpansion力
作者: shchaoge 时间: 2023-6-11 14:19
好了:
BingjianREMIX 发表于 2023-6-11 14:17
搞定,谢谢大佬。
作者: BingjianREMIX 时间: 2023-6-11 14:35
稍微美化了一点:- @echo off
- title Delay divider
- cd /d %~dp0
-
- :start
- cls
- echo.
- echo The script will divide all the numbers by 100.
- echo Please put all the number segments to be divided in 1.txt.(Only integers are supported)
- echo Then enter start to start.
- echo All the results will be in 2.txt
- echo.
- set /p "input=Please enter start to start:"
- if /i "%input%"=="start" goto start0
- cls
- echo ERROR!
- echo Please push any key to restart.
- pause>nul
- goto start
-
- :start0
- cls
- echo loading...
- setlocal EnableDelayedExpansion
- set "inputFile=1.txt"
- set "outputFile=2.txt"
- if exist "%outputFile%" del "%outputFile%"
- for /f %%a in ('type "%inputFile%"') do (
- set /a "result=%%a/100"
- echo !result!>>"%outputFile%"
- )
- echo Done!
- echo Please push any key to restart.
- call 2.txt
- pause>nul
- goto start
复制代码
作者: shchaoge 时间: 2023-6-11 14:38
稍微美化了一点:
BingjianREMIX 发表于 2023-6-11 14:35
:victory: :victory: :handshake :handshake
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |