批处理脚本找到丢失的序列不 [英] batch script to find missing sequence no

查看:302
本文介绍了批处理脚本找到丢失的序列不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的批处理脚本,

I am new to batch script,

我收到的源文件格式
ABC_CDEFG_HIJK_20120712105523_000001(包括时间戳和放大器;指令序列没有将改变)
它可以是单个文件或多个文件中每30分钟一班。
我需要找出是否有任何缺失sequnece没有从列表中发送警报给用户。
谁能帮我这个问题。

I am getting the source file in the format ABC_CDEFG_HIJK_20120712105523_000001(both timestamp&sequnce no will be changing) It can be single file or multiple file for every 30 mins. I need to find out if there are any missing sequnece no from the list and send alert to the user. Can anyone help me with this.

先谢谢了。

推荐答案

选项1

我测试过以下,和它的作品。的(我原来的code的不是测试的最后基本文件名。这code已被固定)

I've tested the following, and it works. (My original code was not testing the very last base file name. This code has been fixed)

该版本循环遍历字母顺序的文件,并记住以前的文件名。当检测到的基本名称的改变它,然后会在事先基本名称序列的空白。最后基本名称没有在循环测试,因此它必须在循环之后进行测试。

This version loops through the files in alpha order and remembers the prior file name. When a change in the base name is detected it then looks for gaps in the prior base name sequences. The last base name is not tested in the loop so it must be tested after the loop.

@echo off
setlocal disableDelayedExpansion
set "ext=.txt"
set "lastName="
for /f "eol=: delims=" %%F in ('dir /b /a-d *%ext% ^| findstr /irec:"_[0-9][0-9][0-9][0-9][0-9][0-9]%ext%"') do (
  set "name=%%~nF"
  setlocal enableDelayedExpansion
  if defined lastName if "!lastName:~0,-6!" neq "!name:~0,-6!" (
    for /f "delims=0" %%I in ("!lastName:~-6!") do (
      for /l %%N in (1 1 %%I) do (
        set "n=000000%%N"
        set "test=!lastName:~0,-6!!n:~-6!!ext!"
        if not exist "!test!" echo "!test!" is missing
      )
    )
  )
  endlocal
  set "lastName=%%~nF"
)
setlocal enableDelayedExpansion
if defined lastname (
  for /f "delims=0" %%I in ("!lastName:~-6!") do (
    for /l %%N in (1 1 %%I) do (
      set "n=000000%%N"
      set "test=!lastName:~0,-6!!n:~-6!!ext!"
      if not exist "!test!" echo "!test!" is missing
    )
  )
)

选项2

下面是一个使用更少code另一个版本(也测试和工作)。它通过循环的字母顺序的文件和构建的名称中的基本名称和值的序列号的变量。星号用于分隔名称和值的部分。挂羊头卖狗肉的位是用来使一个 = 中的文件名不破code。但是,如果多个基地名称中包含 = 的名义,那么他们必须区分 = ,其他明智之前只这些名称中的一个将被测试

Here is another version (also tested and working) that uses less code. It loops through the files in alpha order and constructs a variable with the base name in the name and the sequence number as the value. Asterisks are used to delimit the sections of the name and value. A bit of trickery is used so that an = in the file name does not break the code. However, if multiple base names contain = in the name, then they must differentiate prior to the =, other wise only one of those names will be tested.

在没有该有值最大的序列号每个基地的名字一个变量。第二个循环迭代并解析变量分为基本名称和最大的序列号,然后查找在序列号的空白。

After the loop completes there is one variable for each base name that has the max sequence number in the value. A second loop iterates and parses the variables into base name and max sequence number, and then looks for gaps in the sequence number.

@echo off
setlocal disableDelayedExpansion
set "ext=.txt"
for /f "delims==" %%A in ('2^>nul set file*') do set "%%A="
for /f "eol=: delims=" %%F in ('dir /b /a-d *%ext% ^| findstr /irec:"_[0-9][0-9][0-9][0-9][0-9][0-9]%ext%"') do (
  set "name=%%~nF"
  setlocal enableDelayedExpansion
  for /f "delims=" %%A in ("file*!name:~0,-6!*=*!name:~-6!") do (
    endlocal
    set %%A
  )
)
for /f "tokens=2,4 delims=*" %%A in ('set file*') do (
  set "name=%%A"
  setlocal enableDelayedExpansion
  for /l %%N in (1 1 %%B) do (
    set "n=000000%%N"
    set "test=!name!!n:~-6!!ext!"
    if not exist "!test!" echo "!test!" is missing
  )
  endlocal
)

这篇关于批处理脚本找到丢失的序列不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆