Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

filenames - Batch Script To Identify Missing Numerical File Name

I have a custom service that automatically generates files every 60 mins into a particular directory with part of the filename incrementing numerically, Eg:

File_00004.job
File_00003.job
File_00002.job
File_00001.job

Currently I have an issue where on occasion a file isn't generated, which results in gaps in the file sequence. This issue then causes a number of issues if not identified ASAP.

I'd like a batch file to identify if I have a gap in the file name sequence. Tried looking for solutions from existing posts, but haven't found something that fits, so apologies if this has been covered elsewhere.

question from:https://stackoverflow.com/questions/65851915/batch-script-to-identify-missing-numerical-file-name

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
@ECHO OFF
SETLOCAL enabledelayedexpansion
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:your files w o"

SET "mask=file_??????.job"
SET "lowest="
SET "highest="

FOR /f "delims=" %%a IN (
 'dir /b /a-d /on "%sourcedir%\%mask%" '
 ) DO (
 IF NOT DEFINED lowest SET "lowest=%%~na"
 SET "highest=%%~na"
)
SET "lowest=%lowest:*_=1%"
SET "highest=%highest:*_=1%"

ECHO checking range %lowest:~1% to %highest:~1%

:: See whether an entry in the range is missing; report&create an empty file if so.

FOR /L %%a IN (%lowest%,1,%highest%) DO SET "name=%%a"&SET "name=file_!name:~1!.job"&IF NOT EXIST "%sourcedir%!name!" echo !name! missing&(copy nul "%sourcedir%!name!" >nul)

GOTO :EOF

Alternative structure for the for /L loop:

FOR /L %%a IN (%lowest%,1,%highest%) DO (
 SET "name=%%a"
 SET "name=file_!name:~1!.job"
 IF NOT EXIST "%sourcedir%!name!" (
  echo !name! missing
  copy nul "%sourcedir%!name!" >nul
  copy "d:path toemplate.file" "wherever!name!" >nul
  copy "d:path toemplate.file" "anotherplace!name!" >nul
  echo Batch is fun and powerful
  copy "d:path toemplate.file" "a third place!name!" >nul
 )
)

The critical point is the positioning of the ( - must be directly after and on the same line as do or else or the logical comparison clause of if and must be matched by a ) (which doesn't need to be on its own line - I find it easier that way, to align indentation.) )s that are not intended to close a block need to be escaped with ^, thus: ^)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...