Board logo

标题: [数值计算] 批处理如何批量转换IP为整数 [打印本页]

作者: qaz123654    时间: 2009-9-29 09:28     标题: 批处理如何批量转换IP为整数

我写的代码是这样的
  1. @echo off
  2. for /f "delims=. tokens=1-4" %%a in (001.txt) do set /a result=%%a*256*256*256+%%b*256*256+%%c*256+%%d&echo %result% >>002.txt
  3. pause
复制代码

结果是 ECHO 处于关闭状态
001.txt内容是这样的
  1. 192.168.10.1
  2. 192.168.10.2
  3. 192.168.10.3
  4. 192.168.10.4
  5. 192.168.10.5
  6. 192.168.10.6
  7. 192.168.10.7
  8. 192.168.10.8
  9. 192.168.10.9
复制代码

作者: asnahu    时间: 2009-9-29 10:39

没用过这种功能~~

[ 本帖最后由 asnahu 于 2009-9-29 10:40 编辑 ]
作者: qaz123654    时间: 2009-9-29 10:51     标题: 回复 2楼 的帖子

IP化整数这个在网页中用的多……,批处理肯定是能做到,只是我还没想出来^
作者: asnahu    时间: 2009-9-29 11:08

这里有个脚本,仅供参考:

http://www.3gcomet.com/article.asp?id=150
作者: qaz123654    时间: 2009-9-29 11:12     标题: 回复 4楼 的帖子

恩,谢谢asnahu  这个在网页中我会用,现在想用bat实现
作者: Batcher    时间: 2009-9-29 11:42

%%a*256*256*256超出了批处理可以直接进行计算的范围,需要采用间接地方法,参考:
  1. @echo off&setlocal enabledelayedexpansion
  2. for /f "tokens=1-8,* delims=        . " %%a in ('type ip.txt') do (
  3. Call :GetDIP %%a %%b %%c %%d one
  4. Call :GetDIP %%e %%f %%g %%h tow
  5. if "!one:~4,1!"=="" set/p "=  "<nul
  6. echo  !one!              !tow!         %%i
  7. )
  8. pause
  9. goto :eof
  10. :GetDIP
  11. set /a sur=%2*256*256+%3*256+%4
  12. if %1 gtr 127 (
  13.         set /a "sur=(%1-128)*256*256*256+!sur!"
  14.         set suw=000000000!sur:~-9!
  15.         set /a suw=1!suw:~-9!-1000000000+147483648
  16.         set /a sut=!sur:~0,-9!+!suw:~0,-9!+2
  17.         set "sur=!sut!!suw:~-9!"
  18. ) else (
  19.         set /a sur=%1*256*256*256+!sur!
  20. )
  21. set "%5=!sur!"
  22. goto :eof
复制代码

作者: qaz123654    时间: 2009-9-29 12:09     标题: 回复 6楼 的帖子

感谢Batcher老大,那个 delims=   后边的空格是不是应该去掉?
去掉后这个情况又这么处理?看图:
作者: Seter    时间: 2009-9-29 18:16

不能去掉的,那是分隔符




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2