@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: ^)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…