[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文本处理] 批处理怎样把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

回复 1# shchaoge
  1. @echo off
  2. set inputFile=1.txt
  3. set outputFile=2.txt
  4. if exist "%outputFile%" del "%outputFile%"
  5. for /f %%a in ('type "%inputFile%"') do (
  6.     set /a "result=%%a/100"
  7.     echo !result!>>"%outputFile%"
  8. )
  9. echo Done!
  10. pause
复制代码
脚本会首先将 1.txt 中的每一行内容读取到 result 变量中,并将其除以 100 得到计算结果。然后将每次循环得到的 result 变量值追加到 2.txt 文件中。如果 2.txt 已经存在,则在执行前将其删除,以免原来的内容对结果产生影响。

ps:不支持处理浮点数,只能处理整数,请不要包含任何空格或其他无关字符。
QQ 1980286392

TOP

!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
结果如上?

TOP

回复 3# shchaoge


    额,还没有测试,我看看
QQ 1980286392

TOP

本帖最后由 BingjianREMIX 于 2023-6-11 14:19 编辑
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
!result!
...
shchaoge 发表于 2023-6-11 14:12



    好了:
  1. @echo off
  2. cd /d %~dp0
  3. setlocal EnableDelayedExpansion
  4. set "inputFile=1.txt"
  5. set "outputFile=2.txt"
  6. if exist "%outputFile%" del "%outputFile%"
  7. for /f %%a in ('type "%inputFile%"') do (
  8.     set /a "result=%%a/100"
  9.     echo !result!>>"%outputFile%"
  10. )
  11. echo Done!
  12. pause
复制代码
忘记加setlocal EnableDelayedExpansion力
QQ 1980286392

TOP

好了:
BingjianREMIX 发表于 2023-6-11 14:17


搞定,谢谢大佬。

TOP

稍微美化了一点:
  1. @echo off
  2. title Delay divider
  3. cd /d %~dp0
  4. :start
  5. cls
  6. echo.
  7. echo The script will divide all the numbers by 100.
  8. echo Please put all the number segments to be divided in 1.txt.(Only integers are supported)
  9. echo Then enter start to start.
  10. echo All the results will be in 2.txt
  11. echo.
  12. set /p "input=Please enter start to start:"
  13. if /i "%input%"=="start" goto start0
  14. cls
  15. echo ERROR!
  16. echo Please push any key to restart.
  17. pause>nul
  18. goto start
  19. :start0
  20. cls
  21. echo loading...
  22. setlocal EnableDelayedExpansion
  23. set "inputFile=1.txt"
  24. set "outputFile=2.txt"
  25. if exist "%outputFile%" del "%outputFile%"
  26. for /f %%a in ('type "%inputFile%"') do (
  27.     set /a "result=%%a/100"
  28.     echo !result!>>"%outputFile%"
  29. )
  30. echo Done!
  31. echo Please push any key to restart.
  32. call 2.txt
  33. pause>nul
  34. goto start
复制代码
QQ 1980286392

TOP

稍微美化了一点:
BingjianREMIX 发表于 2023-6-11 14:35



:victory: :victory: :handshake :handshake

TOP

返回列表