@echo off
setlocal
set "attachments=%=do_not_modify=%"
set "db_backups_dir=C:\Users\Administrator\Documents\My Database Backups"
set "nas_dir=\\NAS\Share\BU"
set "email=C:\Program Files\sendEmail\sendEmail.exe"
rem Push initial dir to connect drive
pushd "%nas_dir%" || exit /b 1
rem Set the time and date YYYYMMDD-HHMMSSUUUU
set "ldt="
for /f "usebackq tokens=1,2 delims==" %%I in (
`wmic os get LocalDateTime /VALUE 2^>nul`
) do if ".%%~I." == ".LocalDateTime." set "ldt=%%~J"
if not defined ldt popd & exit /b 2
set "ldt=%ldt:~0,8%-%ldt:~8,6%%ldt:~15,4%
rem Archive older than the last one and delete old archives - local
call :process_files "%db_backups_dir%" "%nas_dir%"
rem Archive older than the last one and delete old archives - remote
call :process_files "%nas_dir%"
rem Pop initial dir to disconnect drive
popd
rem Send confirmation email
if not defined attachments exit /b 3
echo(%attachments%| find "|" && exit /b 4
echo "%email%" -o -f "FromServer" -t me@domain.ltd -a %attachments% ^
-s smtp.domain.ltd:25 -u "Subject: %ldt%" -m Script successfully run"
exit /b 0
:process_files target, destination
rem :process_files "%~1" "%~2"
setlocal
if "%~1" == "" exit /b 1
pushd "%~1" || exit /b 2
echo cd "%~1"
set "copy_file="
set "copied_file_1="
set "copied_file_2="
if not exist "archive" md "archive"
rem Copy files from local to remote and move files to archive
for /f "eol=: delims=" %%A in ('dir /b /o-d *.pdf 2^>nul') do (
if defined copied_file_1 if defined copied_file_2 (
echo move "%%~nxA" "archive\"
move "%%~A" "archive\" >nul
)
if not defined copied_file_1 (
set "copy_file=y"
) else if not defined copied_file_2 (
set "copy_file=y"
)
if defined copy_file (
set "copy_file="
if not "%~2" == "" (
for /f "tokens=1,2 delims=_" %%B in ("%%~nA") do (
echo copy "%%~A" "%~2\File_PDF_BU_%%~C.pdf"
copy /b "%%~A" "%~2\File_PDF_BU_%%~C.pdf" >nul
if not defined copied_file_1 (
set "copied_file_1=%~2\File_PDF_BU_%%~C.pdf"
) else if not defined copied_file_2 (
set "copied_file_2=%~2\File_PDF_BU_%%~C.pdf"
)
)
) else if not defined copied_file_1 (
set "copied_file_1=|"
) else if not defined copied_file_2 (
set "copied_file_2=|"
)
)
)
rem Delete old files in the archive folder
pushd "archive" || exit /b 3
for /f "skip=46 eol=: delims=" %%A in ('dir /a-d /o-d /b /s *.pdf 2^>nul') do (
echo del "%%~nxA"
del "%%~A"
)
popd
rem Set attachments as global if set as file paths
endlocal & if not "%copied_file_1%" == "|" if not "%copied_file_2%" == "|" (
set attachments="%copied_file_1%" "%copied_file_2%"
)
exit /b 0
Этот код соответствует 5 указанным целям. Я придерживался аналогичной концепции с файлами, а не вашей идеей добавления папок в папку архива.
Значение LocalDateTime
, хранящееся в ltd
, равно YYYYMMDD-HHMMSSUUUU
, а не YYYYMMDD-HHMMSS.UUU
, который имеет ваш опубликованный код, что противоречит вашему опубликованному резюме. При необходимости его можно изменить, чтобы он соответствовал правильному шаблону.
Код делает максимум 2 копии файла, так как он отслеживается установленными переменными copied_file_1
и copied_file_2
. Те же самые 2 значения используются для отправки по электронной почте двух файлов вложений.
Соединение подключенного диска выполняется с помощью pushd
, а окончательное использование popd
отключит диск. Если окончательное использование popd
не используется, то диск может остаться, когда скрипт сценария exit
. Итак, pushd
и popd
требуют правильного сопряжения с конечным значением sh.
Ваш код копирует подстановочный путь к одному имени файла. Этот код использует for
l oop для копирования каждого файла и использует оригинальную отметку даты.