标题: 【已解决】50元求助批处理根据文件名统计数量 [打印本页]
作者: sysclock 时间: 2019-5-19 11:10 标题: 【已解决】50元求助批处理根据文件名统计数量
本帖最后由 sysclock 于 2019-5-22 14:00 编辑
有一些文件,通过dir命令已经得到文件名,要进一步处理
===文件名=====
20190511152311.032357965-CP14.dat
20190511152313.030170535-CP14.dat
20190511152315.030184055-CP14.dat
20190511152317.030193560-CP14.dat
20190511152319.030201595-CP14.dat
20190511152321.030213575-CP14.dat
20190511152323.030224565-CP14.dat
20190511152325.030236580-CP14.dat
20190511152327.030251605-CP14.dat
20190511152329.030265035-CP14.dat
20190511152331.030282525-CP14.dat
20190511152333.030282450-CP14.dat
20190511152335.030294475-CP14.dat
20190511152337.030309415-CP14.dat
上述名字的格式:如“20190511152337.030309415-CP14” 是年4位-月2位-日2位-时2位-分2位-秒2位.ms3位-us3位-ns3位 - 站点名字符若干-站点序号2位
要处理的事:
1、统计文件的数量;
2、统计所有文件中ns的分布:000-099内多少个;100-199内多少个,---;能再统计us分布更好
us分布 ms 次数 us 次数 ns 次数
----------------------------------------------------------------------------------
000-099 53
100-199 22
.........
3、如果小数点后面的年月日时分秒相同的文件,这些文件的站点序号(.dat的前2个数字不同)不同,统计不同的站点序号,在不容的时刻相差多少
支付宝50元有人愿意帮忙吗?后面还有类似的事情请求帮忙。
谢谢。- dir /b *.dat > 1.txt
-
- ::get ms-us-ns value
- ::for /f "delims=. tokens=2" %%i in (1.txt) do (echo %%i)
- for /f "delims=. tokens=2" %%i in (1.txt) do (
- echo %%i >>2.txt
- )
-
- ::discard -StationName-StationNumber
- for /f "delims=- tokens=1" %%i in (2.txt) do (
- echo %%%i >>3.txt
- )
-
- ::这一步总是出错。
- for /f "delims=- tokens=1 %%i in (3.txt) do (
- set str=%%i
- set var=%str:~7,9%
- echo %var% >>4.txt
- )
复制代码
作者: zaqmlp 时间: 2019-5-19 11:46
第3点什么意思啊,读不顺呀
作者: sysclock 时间: 2019-5-19 12:01
本帖最后由 sysclock 于 2019-5-19 12:03 编辑
谢谢版主~
3、如果小数点后面的年月日时分秒相同的文件,这些文件的站点序号(.dat的前2个数字不同)不同,统计不同的站点序号,在不同的时刻相差的ns数是多少
实际中,采集的文件是这样的
20190511152335.030294475-CP14.dat
20190511152337.030309415-CP14.dat
20190511152335.030294435-CP15.dat
20190511152337.030309455-CP15.dat
20190511152335.030294495-CP16.dat
20190511152337.030309435-CP16.dat
.......
可以看到,20190511152335这一秒,有三个站点生成了文件,产生如下一个比较输出是最好(diff是所有不同的台站的同一时刻的最大值-最小值):
TIME CP14 CP15 CP16 diff
-----------------------------------------------------------------------------------------------------------------
20190511152335 475 435 495 60
20190511152337 415 455 435 40
......
如果某个时刻,某个站点没有数据,用×表示;
作者: zaqmlp 时间: 2019-5-19 13:11
本帖最后由 zaqmlp 于 2019-5-19 19:38 编辑
- @echo off
- mode con lines=1000
- set info=互助互利,支付宝扫码头像,感谢赞助
- rem 有问题,可加QQ956535081及时沟通
- title %info%
- cd /d "%~dp0"
- powershell -NoProfile -ExecutionPolicy bypass ^
- $list=New-Object 'System.Collections.Generic.Dictionary[string,Object]';^
- for($i=0;$i -le 9;$i++){$list.Add((''+$i+'00-'+$i+'99'),@(0,0,0))};^
- $c=0;$dic=New-Object 'System.Collections.Generic.Dictionary[string,Object]';^
- $files=@(dir^|?{$_ -is [System.IO.FileInfo]});^
- for($i=0;$i -lt $files.Count;$i++){^
- $m=[regex]::match($files[$i].Name,'^^(\d{14})\.(\d{3})(\d{3})(\d{3})-(.+?\d{2})\.dat');^
- if($m.success){^
- foreach($it in $list.keys){^
- $t=$it.split('-');^
- for($j=0;$j -le 2;$j++){^
- if(([int]$m.groups[$j+2].value -ge [int]$t[0]) -and ([int]$m.groups[$j+2].value -le [int]$t[1])){^
- $list[$it][$j]++;^
- };^
- };^
- };^
- $item=$m.groups[5].value+':'+$m.groups[4].value;^
- if(-not $dic.ContainsKey($m.groups[1].value)){^
- [System.Collections.ArrayList]$s=@($item);^
- $dic.Add($m.groups[1].value, $s);^
- }else{^
- [void]$dic[$m.groups[1].value].Add($item);^
- };^
- $c++;^
- };^
- };^
- $result=''+$c+\"`r`n-----------------------`r`n\";^
- foreach($it in $list.Keys){$result+=$it+\"`t\"+($list[$it] -join ',')+\"`r`n\";};^
- $result+=\"-----------------------`r`n\";^
- foreach($it in $dic.Keys){^
- $arr=@($dic[$it]^|sort {[int]$_.split(':')[1]});^
- $result+=$it+\"`t\"+($arr -join ',')+\"`t\"+([int]$arr[-1].split(':')[1]-[int]$arr[0].split(':')[1])+\"`r`n\";^
- };^
- $result;[IO.File]::WriteAllText('$result.log', $result, [Text.Encoding]::Default);^
- write-host '%info%' -ForegroundColor green;
- pause
复制代码
作者: sysclock 时间: 2019-5-19 14:59
佩服佩服~
谢谢!
回复 4# zaqmlp
作者: amwfjhh 时间: 2019-5-19 19:03
纯批处理版
以题干所列文件名为样本测试- 20190511152311.032357965-CP14.dat
- 20190511152313.030170535-CP14.dat
- 20190511152315.030184055-CP14.dat
- 20190511152317.030193560-CP14.dat
- 20190511152319.030201595-CP14.dat
- 20190511152321.030213575-CP14.dat
- 20190511152323.030224565-CP14.dat
- 20190511152325.030236580-CP14.dat
- 20190511152327.030251605-CP14.dat
- 20190511152329.030265035-CP14.dat
- 20190511152331.030282525-CP14.dat
- 20190511152333.030282450-CP14.dat
- 20190511152335.030294435-CP15.dat
- 20190511152335.030294475-CP14.dat
- 20190511152335.030294495-CP16.dat
- 20190511152337.030309415-CP14.dat
- 20190511152337.030309435-CP16.dat
- 20190511152337.030309455-CP15.dat
复制代码
- @echo off
- setlocal enabledelayedexpansion
-
- set "nFileCount=0"
-
- REM =====统计文件总数
- echo,Step 1 :
- for /f "tokens=*" %%i in ('dir /b *.dat') do (
- set /a "nFileCount+=1"
- )
- echo,统计文件总数为 : !nFileCount!
- echo,&echo,
-
- REM =====统计毫秒、微秒、纳秒数量级分布规律
- echo,Step 2 :
- call :_ShowTimeCount
- REM 显示统计结果
- echo,LEVEL ms次数 us次数 ns次数
- echo,------------------------------------
- for /l %%i in (0 1 9) do (
- echo,%%i00-%%i99 !nMS%%i00%%i99! !nUS%%i00%%i99! !nNS%%i00%%i99!
- )
- echo,&echo,
-
- REM =====显示同一时刻不同网站纳秒差
- echo,Step 3 :
- for /l %%i in (1 1 !nMainCount!) do (
- call :_ShowTimeDiff %%i
- )
-
- pause
- goto :EOF
-
- :_ShowTimeCount
- (
- REM 变量初始化
- for /l %%i in (0 1 9) do (
- set "nMS%%i00%%i99=0"
- set "nUS%%i00%%i99=0"
- set "nNS%%i00%%i99=0"
- )
-
- REM for /l %%i in (0 1 9) do (
- REM echo,nMS%%i00%%i99=!nMS%%i00%%i99!
- REM echo,nUS%%i00%%i99=!nUS%%i00%%i99!
- REM echo,nNS%%i00%%i99=!nNS%%i00%%i99!
- REM )
-
- REM 定义查询字典
- set "strDict="
- REM 统计总时刻数
- set "nMainCount=0"
- for /f "tokens=1,2,3 delims=.-" %%i in ('dir /b *.dat') do (
- REM echo,当前处理信息为 : %%i.%%j-%%k.dat
- set "strMain=%%i"
- set "strS=%%j"
-
- set "nMS=!strS:~0,3!"
- REM echo,当前毫秒值为 : !nMS!
- for /l %%a in (0 1 9) do (
- if "!nMS:~0,1!" EQU "%%a" (
- set /a "nMS%%a00%%a99+=1"
- )
- )
-
- set "nUS=!strS:~3,3!"
- REM echo,当前微秒值为 : !nUS!
- for /l %%a in (0 1 9) do (
- if "!nUS:~0,1!" EQU "%%a" (
- set /a "nUS%%a00%%a99+=1"
- )
- )
-
- set "nNS=!strS:~6!"
- REM echo,当前纳秒值为 : !nNS!
- for /l %%a in (0 1 9) do (
- if "!nNS:~0,1!" EQU "%%a" (
- set /a "nNS%%a00%%a99+=1"
- )
- )
-
- REM 对同一秒内不同站点的纳秒值差进行统计
- REM echo,当前字典 : !strDict!
- echo,!strDict! | findstr "!strMain!" >nul 2>nul && (
- REM 有此时刻统计信息,更新网站数
- set /a "n!strMain!Count+=1"
- ) || (
- REM 无此时刻统计信息
- REM 更新时刻总数
- set /a "nMainCount+=1"
- REM 当前新时刻计数初始化
- set "n!strMain!Count=1"
- REM 更新字典
- set "strDict=!strDict!(!strMain!)"
- )
- REM 更新MAP
- call set "str!strMain!.%%n!strMain!Count%%.Name=%%k"
- call set "str!strMain!.%%n!strMain!Count%%.NS=!nNS!"
- REM echo,当前总时刻数 : nMainCount = !nMainCount!
- REM call echo,当前时刻总数 : n!strMain!Count = %%n!strMain!Count%%
- REM call call echo,当前分析站点 : str!strMain!.%%n!strMain!Count%%.Name = %%%%str!strMain!.%%n!strMain!Count%%.Name%%%%
- REM call call echo,当前分析纳秒 : str!strMain!.%%n!strMain!Count%%.NS = %%%%str!strMain!.%%n!strMain!Count%%.NS%%%%
- )
- for /l %%i in (0 1 9) do (
- set "nMS%%i00%%i99=000000!nMS%%i00%%i99!"
- set "nMS%%i00%%i99=!nMS%%i00%%i99:~-6!"
- set "nUS%%i00%%i99=000000!nUS%%i00%%i99!"
- set "nUS%%i00%%i99=!nUS%%i00%%i99:~-6!"
- set "nNS%%i00%%i99=000000!nNS%%i00%%i99!"
- set "nNS%%i00%%i99=!nNS%%i00%%i99:~-6!"
- )
- goto :EOF
- )
-
- :_ShowTimeDiff
- (
- for /f "tokens=%1 delims=()" %%a in ('echo,!strDict!') do (
- REM echo,当前处理时刻 %%a
- set "strTime=%%a"
- set "nTmpCount=0"
- call set "nTmpCount=%%n!strTime!Count%%"
- REM echo,共有网站记录数 : !nTmpCount!
- set /a "nMin=999"
- set /a "nMax=0"
- set /p=!strTime! <nul
- for /l %%i in (1 1 !nTmpCount!) do (
- REM echo,第 %%i 个网站 :
- call set "strTmpName=%%str!strTime!.%%i.Name%%"
- REM echo,Name : !strTmpName!
- call set "strTmpNS=%%str!strTime!.%%i.NS%%"
- REM call echo,NS : !strTmpNS!
- set /p=!strTmpName!:!strTmpNS! <nul
- if !nMin! GEQ !strTmpNS! set "nMin=!strTmpNS!"
- if !nMax! LEQ !strTmpNS! set "nMax=!strTmpNS!"
- )
- set /a "nDiff=!nMax!-!nMin!"
- echo,Diff:!nDiff!
- )
- goto :EOF
- )
复制代码
作者: sysclock 时间: 2019-5-19 22:11
本帖最后由 sysclock 于 2019-5-21 21:39 编辑
已经结题.高手中更有高手,请amwfjhh兄弟留个支付宝号码,一包香烟,略表谢意,谢谢大家
作者: WHY 时间: 2019-5-20 15:20
本帖最后由 WHY 于 2019-5-21 09:24 编辑
- @if(0)==(0) echo off
- dir /b *.dat | cscript //nologo //e:jscript "%~f0" > Result.Log
- pause & goto :EOF
- @end
-
- var ms = [], us = [], ns = [];
- for(var i=0; i<=9; i++){ ms[i] = 0; us[i] = 0; ns[i] = 0; }
- var total = 0;
- var map = [];
-
- while(!WSH.StdIn.AtEndOfStream) {
- var f = WSH.StdIn.ReadLine();
- var m = f.match(/^(\d{14})\.(\d)\d\d(\d)\d\d(\d)(\d\d)-(.+)\.dat$/i);
- if(!m) continue;
- total++;
- ms[1*m[2]]++;
- us[1*m[3]]++;
- ns[1*m[4]]++;
- if(!map[m[1]]) map[m[1]] = [];
- map[m[1]].push( [m[6], m[4] + m[5]] );
- }
-
- WSH.Echo('文件总数:' + total + '\r\n\r\n范围\tms次数\tus次数\tns次数');
- for(var i=0; i<=9; i++){
- WSH.Echo(i + '00-' + i + '99\t' + ms[i] + '\t' + us[i] + '\t' + ns[i]);
- }
-
- WSH.Echo('');
- for(var key in map) {
- var Len = map[key].length;
- if(Len <= 1) continue;
- var arr = map[key].sort(function(a,b){return a[1]-b[1]});
- var min = arr[0][1];
- var max = arr[Len-1][1];
- WSH.Echo(key + '\t' + arr.join('\t') + '\tdiff,' + (max-min));
- }
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |