Board logo

标题: [代码合集] 批处理如何求和 [打印本页]

作者: yps851022    时间: 2014-9-24 14:59     标题: 批处理如何求和

@echo off
:begin
set d=%date:~0,10%
set t=%time:~0,8%
set /p =" %d% %t% ,8000,close_wait,  " <nul >> D:\123.txt
netstat -aon -p tcp | find /i"8000" |find /i "close_wait" /c  >> D:\123.txt
set /p =" %d% %t% ,8000,time_wait,  " <nul >> D:\123.txt
netstat -aon -p tcp | find /i"8000" |find /i "time_wait" /c  >> D:\123.txt
set /p =" %d% %t% ,8000,established,  " <nul >> D:\123.txt
netstat -aon -p tcp | find /i"8000" |find /i "ESTABLISHED" /c  >> D:\123.txt
set /p =" %d% %t% ,9000,close_wait,  " <nul  >> D:\123.txt
netstat -aon -p tcp | find /i"9000" |find /i "close_wait" /c  >> D:\123.txt
set /p =" %d% %t% ,9000,time_wait,  " <nul  >> D:\123.txt
netstat -aon -p tcp | find /i"9000" |find /i "time_wait" /c  >> D:\123.txt
set /p =" %d% %t% ,9000,established,  " <nul >> D:\123.txt
netstat -aon -p tcp | find /i"9000" |find /i "ESTABLISHED" /c  >> D:\123.txt
echo.  >> D:\123.txt
echo --------------------------------------------------------------------- >> D:\123.txt
ping 127.1 /n 600 >nul
goto begin

输出结果是:
2014/09/24 14:30:23 ,8000,close_wait,  1
2014/09/24 14:30:23 ,8000,time_wait,  2
2014/09/24 14:30:23 ,8000,established,  3
2014/09/24 14:30:23 ,9000,close_wait,  1
2014/09/24 14:30:23 ,9000,time_wait,  1
2014/09/24 14:30:23 ,9000,established,  1
--------------------------------------------------------
如何才可以输出:
2014/09/24 14:30:23 ,8000,close_wait,  1
2014/09/24 14:30:23 ,8000,time_wait,  2
2014/09/24 14:30:23 ,8000,established,  3
2014/09/24 14:30:23 ,9000,close_wait,  1
2014/09/24 14:30:23 ,9000,time_wait,  1
2014/09/24 14:30:23 ,9000,established,  1

close_wait 2; time_wait 3; established 4
-------------------------------------------------------
作者: DAIC    时间: 2014-9-24 15:31

本帖最后由 DAIC 于 2014-9-27 11:50 编辑

@echo off
:begin
set d=%date:~0,10%
set t=%time:~0,8%
set /p =" %d% %t% ,8000,close_wait,  " <nul > D:\123.txt
netstat -aon -p tcp | find /i"8000" |find /i "close_wait" /c  >> D:\123.txt
set /p =" %d% %t% ,8000,time_wait,  " <nul >> D:\123.txt
netstat -aon -p tcp | find /i"8000" |find /i "time_wait" /c  >> D:\123.txt
set /p =" %d% %t% ,8000,established,  " <nul >> D:\123.txt
netstat -aon -p tcp | find /i"8000" |find /i "ESTABLISHED" /c  >> D:\123.txt
set /p =" %d% %t% ,9000,close_wait,  " <nul  >> D:\123.txt
netstat -aon -p tcp | find /i"9000" |find /i "close_wait" /c  >> D:\123.txt
set /p =" %d% %t% ,9000,time_wait,  " <nul  >> D:\123.txt
netstat -aon -p tcp | find /i"9000" |find /i "time_wait" /c  >> D:\123.txt
set /p =" %d% %t% ,9000,established,  " <nul >> D:\123.txt
netstat -aon -p tcp | find /i"9000" |find /i "ESTABLISHED" /c  >> D:\123.txt
echo.  >> D:\123.txt

>> D:\123.txt echo,
setlocal
set #close_wait=
set #time_wait=
set #established=
for /f "tokens=4,5 delims=, " %%i in (D:\123.txt) do (
    set /a #%%i+=%%j
)
for /f "tokens=1,2 delims=#=" %%i in ('set #') do (
    >> D:\123.txt set /p ="%%i %%j; " <nul
)
endlocal
>> D:\123.txt echo,


echo --------------------------------------------------------------------- >> D:\123.txt
type D:\123.txt >> D:\456.txt
ping 127.1 /n 600 >nul
goto begin
作者: yps851022    时间: 2014-9-24 16:28

大侠,貌似不行啊
作者: yps851022    时间: 2014-9-24 16:50

回复 2# DAIC


    大侠再帮分析下,按照你那样的写法,输出有错误哦 ,拜托
作者: xxpinqz    时间: 2014-9-24 17:44

a.txt改为d:\123.txt
作者: DAIC    时间: 2014-9-24 17:50

回复 4# yps851022


    已更改,你再试试。
作者: CrLf    时间: 2014-9-24 19:30

>> D:\123.txt 是错误的写法,>> 后应紧接文件路径
-------------------------------------
好像可以简化成这样:
  1. @echo off
  2. set d=%date:~0,10%
  3. set t=%time:~0,8%
  4. for /f "skip=4 tokens=3,5,6,7 delims=: " %%a in ('netstat -aon -p tcp') do (
  5. set /a tcp[%%a][%%c]+=1,tcp[%%b][%%c]+=1,tcp[%%d][%%c]+=1
  6. )
  7. (for /f "tokens=2-4 delims=[]=" %%a in ('"set tcp[8000]&set tcp[9000]"') do (
  8. echo  %d% %t% ,%%a,%%b,  %%c
  9. ))>>"D:\123.txt"
  10. pause
复制代码

作者: yps851022    时间: 2014-9-26 11:29

回复 6# DAIC


    大侠,再帮改改,下面是按照你是写法输出的结果,貌似是累计求和了哦

2014/09/26 11:25:09 ,8000,close_wait,  0
2014/09/26 11:25:09 ,8000,time_wait,  0
2014/09/26 11:25:09 ,8000,established,  0
2014/09/26 11:25:09 ,9000,close_wait,  0
2014/09/26 11:25:09 ,9000,time_wait,  0
2014/09/26 11:25:09 ,9000,established,  1
  

close_wait 0; established 1; time_wait 0;
---------------------------------------------------------------------
2014/09/26 11:26:09 ,8000,close_wait,  0
2014/09/26 11:26:09 ,8000,time_wait,  0
2014/09/26 11:26:09 ,8000,established,  0
2014/09/26 11:26:09 ,9000,close_wait,  0
2014/09/26 11:26:09 ,9000,time_wait,  0
2014/09/26 11:26:09 ,9000,established,  1
  

1; 0; close_wait 0; established 2; time_wait 0;
---------------------------------------------------------------------
2014/09/26 11:27:10 ,8000,close_wait,  0
2014/09/26 11:27:10 ,8000,time_wait,  0
2014/09/26 11:27:10 ,8000,established,  0
2014/09/26 11:27:10 ,9000,close_wait,  0
2014/09/26 11:27:10 ,9000,time_wait,  0
2014/09/26 11:27:10 ,9000,established,  1
  

0; 0; 1; 0; close_wait 0; established 3; time_wait 0;
---------------------------------------------------------------------

我想显示的是这个样子的:
2014/09/26 11:25:09 ,8000,close_wait,  0
2014/09/26 11:25:09 ,8000,time_wait,  0
2014/09/26 11:25:09 ,8000,established,  0
2014/09/26 11:25:09 ,9000,close_wait,  0
2014/09/26 11:25:09 ,9000,time_wait,  0
2014/09/26 11:25:09 ,9000,established,  1
  
close_wait 0; time_wait 0; established 1;
---------------------------------------------------------------------
2014/09/26 11:26:09 ,8000,close_wait,  0
2014/09/26 11:26:09 ,8000,time_wait,  0
2014/09/26 11:26:09 ,8000,established,  1
2014/09/26 11:26:09 ,9000,close_wait,  0
2014/09/26 11:26:09 ,9000,time_wait,  0
2014/09/26 11:26:09 ,9000,established,  1
  
close_wait 0; time_wait 0; established 2;
---------------------------------------------------------------------
2014/09/26 11:27:10 ,8000,close_wait,  0
2014/09/26 11:27:10 ,8000,time_wait,  0
2014/09/26 11:27:10 ,8000,established,  1
2014/09/26 11:27:10 ,9000,close_wait,  0
2014/09/26 11:27:10 ,9000,time_wait,  0
2014/09/26 11:27:10 ,9000,established,  0
  
close_wait 0; time_wait 0; established 1;
---------------------------------------------------------------------
作者: DAIC    时间: 2014-9-26 13:38

回复 8# yps851022


    第二次更新,你再试试。
作者: yps851022    时间: 2014-9-26 14:08

回复 9# DAIC

输出没有改变什么呀
还是这样的
2014/09/26 14:04:26 ,8000,close_wait,  0
2014/09/26 14:04:26 ,8000,time_wait,  0
2014/09/26 14:04:26 ,8000,established,  0
2014/09/26 14:04:26 ,9000,close_wait,  0
2014/09/26 14:04:26 ,9000,time_wait,  0
2014/09/26 14:04:26 ,9000,established,  1
  

close_wait 0; established 1; time_wait 0;
---------------------------------------------------------------------
2014/09/26 14:05:26 ,8000,close_wait,  0
2014/09/26 14:05:26 ,8000,time_wait,  0
2014/09/26 14:05:26 ,8000,established,  0
2014/09/26 14:05:26 ,9000,close_wait,  0
2014/09/26 14:05:26 ,9000,time_wait,  0
2014/09/26 14:05:26 ,9000,established,  1
  

1; 0; close_wait 0; established 2; time_wait 0;
---------------------------------------------------------------------
2014/09/26 14:06:27 ,8000,close_wait,  0
2014/09/26 14:06:27 ,8000,time_wait,  0
2014/09/26 14:06:27 ,8000,established,  0
2014/09/26 14:06:27 ,9000,close_wait,  0
2014/09/26 14:06:27 ,9000,time_wait,  0
2014/09/26 14:06:27 ,9000,established,  1
  

0; 0; 1; 0; close_wait 0; established 3; time_wait 0;
-------------------------------------------------------
作者: DAIC    时间: 2014-9-27 11:51

回复 10# yps851022


    第三次修改




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