问题
I am new to batch script,
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.
Thanks in advance.
回答1:
assuming no extension,
@echo off
setlocal enabledelayedexpansion
for /l %%a in (1,999999,1) do (
set test1=000000%%a
set test2=!test1:~-6!
if not exist *!test2! goto loopexit
)
:loopexit
echo %test2% missing
if there is an extension, add it to the if not exist
line, e.g. if not exist *!test2!.txt goto loopexit
It uses test2
to test for the filename, as this is the one with all the extra zeros that you don't get from the loop %a
回答2:
Option 1
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
)
)
)
Option 2
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
)
回答3:
ls -1|awk -F "_" '{print $4}'|cut -d. -f1 >seq.txt
awk 'p && p != $1 { for( i = p; i < $1; i++ ) print i; } {p = $1 + 1 }' seq.txt
来源:https://stackoverflow.com/questions/11454701/batch-script-to-find-missing-sequence-no