创建批处理脚本以在任务计划程序中使用 sendemail.exe
Create a batch script to use sendemail.exe in Task Scheduler
我正在尝试通过任务计划程序为 运行 创建一个小批处理脚本,但 运行 遇到同一目录中多个 PDF 文件的问题。
我的目标是:
- 映射远程NAS(这是为了减轻服务器的负担,并作为备份)
- 将现有 PDF 移动到存档文件夹(同一目录),最新的 2 个文件除外
- 清理存档以删除最旧的 n
- 将附件通过电子邮件发送到我的地址
- 卸载 NAS
目前的代码:
:: delete the old mapping
NET USE A: /DELETE
:: map the backup location
NET USE A: \NAS\Share\BU
:: set the time and date YYYYMMDD-HHMMSS.UUU
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set ldt=%ldt:~0,4%%ldt:~4,2%%ldt:~6,2%-%ldt:~8,2%%ldt:~10,2%%ldt:~12,6%
:: archive older than the last one - local
pushd "C:\Users\Administrator\Documents\My Database Backups"
echo :: Move these files ::
echo ------------------------
for /f "skip=2 eol=: delims=" %%F in ('dir /b /o-d *.pdf') do MOVE "%%F" ".\archive"
:: clean the archive folder - local
echo :: Delete these files ::
echo ------------------------
pushd "C:\Users\Administrator\Documents\My Database Backups\archive"
for /f "skip=46 eol=: delims=" %%a in ('dir /a-d /o-d /b /s *.pdf') do del "%%a"
:: go to export directory
cd "C:\Users\Administrator\Documents\My Database Backups"
:: copy to NAS
copy /b *.pdf "\NAS\Share\BU\File_PDF_BU_%ldt%.pdf"
:: archive older than the last one - remotely
pushd "A:\"
echo :: Move these files ::
echo ------------------------
for /f "skip=2 eol=: delims=" %%F in ('dir /b /o-d *.pdf') do MOVE "%%F" "A:\archive"
:: delete old archives - remotely
echo :: Delete these files ::
echo ------------------------
pushd "A:\archive"
for /f "skip=46 eol=: delims=" %%b in ('dir /a-d /o-d /b /s *.pdf') do del "%%b"
:: send confirmation email
"C:\Program Files\sendEmail\sendEmail.exe" -o -f "FromServer" -t me@domain.ltd -a "\NAS\Share\BU\File_PDF_BU_%ldt%.pdf" -s smtp.domain.ltd:25 -u "Subject: %ldt%" -m Script successfully run"
:: wait 60 seconds
TIMEOUT /T 60
:: delete the old mapping
NET USE A: /DELETE /Y
:: exit the script
exit
目前,我对程序本身生成的文件有疑问。我在 C:\Users\Administrator\Documents\My Database Backups
文件夹中收到两个文件 Database01_YYYYMMDD-HHMMSSUUUU.pdf
和 Database02_YYYYMMDD-HHMMSSUUUU.pdf
。所以我在复制的时候,没有办法限制到两个PDF。
有没有办法将根文件夹中的当前两个文件移动到一个文件夹中,然后将其移动到archive
文件夹中。然后在存档文件夹中保留最近的 11 个文件夹(运行s 12 小时 - 11 + 1 在根目录中)。并将文件夹附加到 sendemail.exe
?
谢谢!!
我通过从 sendEmail.exe
更改为 cMail.exe
解决了这个问题:
@ECHO OFF
:: set the time and date YYYYMMDD-HHMMSS.UUU
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set ldt=%ldt:~0,4%%ldt:~4,2%%ldt:~6,2%-%ldt:~8,2%%ldt:~10,2%%ldt:~12,6%
:: map the backup location
NET USE A: \NAS\SHARE\DB_BU
:: archive the old files - local
pushd "C:/Users/Administrator/Documents/My Database Backups/"
for /f "skip=2 eol=: delims=" %%F in ('dir /b /o-d *.pdf') do MOVE "%%F" ".\archive"
:: clean the archive folder - local
pushd "C:/Users/Administrator/Documents/My Database Backups/archive/"
for /f "skip=22 eol=: delims=" %%a in ('dir /a-d /o-d /b /s *.pdf') do DEL "%%a"
:: go to export directory
cd "C:\Users\Administrator\Documents\My Database Backups"
:: copy PDFs to A: drive
copy /b "*.pdf" "A:/"
:: archive the old files - local
pushd "A:/"
for /f "skip=2 eol=: delims=" %%F in ('dir /b /o-d *.pdf') do MOVE "%%F" ".\archive"
:: clean the archive folder - local
pushd "A:/archive/"
for /f "skip=22 eol=: delims=" %%a in ('dir /a-d /o-d /b /s *.pdf') do DEL "%%a"
:: send confirmation email
"C:\Program Files\cMail\CMail.exe" -host:smtp.domain.ltd:25 -from:backup@domain.ltd -to:me@domain.ltd -awild:"A:\Database_*.pdf" "-subject:PDF backup: %ldt%" "-body:Backup script successfully run at %ldt%\nPlease find attached database documents\n\nThis is automated"
:: wait 60 seconds
TIMEOUT /T 60
:: delete the old mapping
NET USE A: /DELETE /Y
:: exit the script
exit
@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 个指定目标。我坚持与文件类似的概念,而不是您将文件夹添加到存档文件夹的想法。
存储在 ltd
中的 LocalDateTime
值是 YYYYMMDD-HHMMSSUUUU
,而不是您发布的代码与您发布的摘要冲突的 YYYYMMDD-HHMMSS.UUU
。如果需要匹配正确的模式,可以更改它。
代码最多复制2个文件,因为它是由copied_file_1
和copied_file_2
的设置变量跟踪的。这两个相同的值用于通过电子邮件发送 2 个附件文件。
映射的驱动器连接是通过使用 pushd
完成的,最后使用 popd
将断开驱动器。如果最后使用popd
没有用到,那么在做脚本的时候驱动可能会保持exit
。因此,pushd
和 popd
需要配对才能正确完成。
您的代码将通配符路径复制到一个文件名。此代码使用 for
循环复制每个文件并使用原始日期戳。
我正在尝试通过任务计划程序为 运行 创建一个小批处理脚本,但 运行 遇到同一目录中多个 PDF 文件的问题。
我的目标是:
- 映射远程NAS(这是为了减轻服务器的负担,并作为备份)
- 将现有 PDF 移动到存档文件夹(同一目录),最新的 2 个文件除外
- 清理存档以删除最旧的 n
- 将附件通过电子邮件发送到我的地址
- 卸载 NAS
目前的代码:
:: delete the old mapping
NET USE A: /DELETE
:: map the backup location
NET USE A: \NAS\Share\BU
:: set the time and date YYYYMMDD-HHMMSS.UUU
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set ldt=%ldt:~0,4%%ldt:~4,2%%ldt:~6,2%-%ldt:~8,2%%ldt:~10,2%%ldt:~12,6%
:: archive older than the last one - local
pushd "C:\Users\Administrator\Documents\My Database Backups"
echo :: Move these files ::
echo ------------------------
for /f "skip=2 eol=: delims=" %%F in ('dir /b /o-d *.pdf') do MOVE "%%F" ".\archive"
:: clean the archive folder - local
echo :: Delete these files ::
echo ------------------------
pushd "C:\Users\Administrator\Documents\My Database Backups\archive"
for /f "skip=46 eol=: delims=" %%a in ('dir /a-d /o-d /b /s *.pdf') do del "%%a"
:: go to export directory
cd "C:\Users\Administrator\Documents\My Database Backups"
:: copy to NAS
copy /b *.pdf "\NAS\Share\BU\File_PDF_BU_%ldt%.pdf"
:: archive older than the last one - remotely
pushd "A:\"
echo :: Move these files ::
echo ------------------------
for /f "skip=2 eol=: delims=" %%F in ('dir /b /o-d *.pdf') do MOVE "%%F" "A:\archive"
:: delete old archives - remotely
echo :: Delete these files ::
echo ------------------------
pushd "A:\archive"
for /f "skip=46 eol=: delims=" %%b in ('dir /a-d /o-d /b /s *.pdf') do del "%%b"
:: send confirmation email
"C:\Program Files\sendEmail\sendEmail.exe" -o -f "FromServer" -t me@domain.ltd -a "\NAS\Share\BU\File_PDF_BU_%ldt%.pdf" -s smtp.domain.ltd:25 -u "Subject: %ldt%" -m Script successfully run"
:: wait 60 seconds
TIMEOUT /T 60
:: delete the old mapping
NET USE A: /DELETE /Y
:: exit the script
exit
目前,我对程序本身生成的文件有疑问。我在 C:\Users\Administrator\Documents\My Database Backups
文件夹中收到两个文件 Database01_YYYYMMDD-HHMMSSUUUU.pdf
和 Database02_YYYYMMDD-HHMMSSUUUU.pdf
。所以我在复制的时候,没有办法限制到两个PDF。
有没有办法将根文件夹中的当前两个文件移动到一个文件夹中,然后将其移动到archive
文件夹中。然后在存档文件夹中保留最近的 11 个文件夹(运行s 12 小时 - 11 + 1 在根目录中)。并将文件夹附加到 sendemail.exe
?
谢谢!!
我通过从 sendEmail.exe
更改为 cMail.exe
解决了这个问题:
@ECHO OFF
:: set the time and date YYYYMMDD-HHMMSS.UUU
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set ldt=%ldt:~0,4%%ldt:~4,2%%ldt:~6,2%-%ldt:~8,2%%ldt:~10,2%%ldt:~12,6%
:: map the backup location
NET USE A: \NAS\SHARE\DB_BU
:: archive the old files - local
pushd "C:/Users/Administrator/Documents/My Database Backups/"
for /f "skip=2 eol=: delims=" %%F in ('dir /b /o-d *.pdf') do MOVE "%%F" ".\archive"
:: clean the archive folder - local
pushd "C:/Users/Administrator/Documents/My Database Backups/archive/"
for /f "skip=22 eol=: delims=" %%a in ('dir /a-d /o-d /b /s *.pdf') do DEL "%%a"
:: go to export directory
cd "C:\Users\Administrator\Documents\My Database Backups"
:: copy PDFs to A: drive
copy /b "*.pdf" "A:/"
:: archive the old files - local
pushd "A:/"
for /f "skip=2 eol=: delims=" %%F in ('dir /b /o-d *.pdf') do MOVE "%%F" ".\archive"
:: clean the archive folder - local
pushd "A:/archive/"
for /f "skip=22 eol=: delims=" %%a in ('dir /a-d /o-d /b /s *.pdf') do DEL "%%a"
:: send confirmation email
"C:\Program Files\cMail\CMail.exe" -host:smtp.domain.ltd:25 -from:backup@domain.ltd -to:me@domain.ltd -awild:"A:\Database_*.pdf" "-subject:PDF backup: %ldt%" "-body:Backup script successfully run at %ldt%\nPlease find attached database documents\n\nThis is automated"
:: wait 60 seconds
TIMEOUT /T 60
:: delete the old mapping
NET USE A: /DELETE /Y
:: exit the script
exit
@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 个指定目标。我坚持与文件类似的概念,而不是您将文件夹添加到存档文件夹的想法。
存储在 ltd
中的 LocalDateTime
值是 YYYYMMDD-HHMMSSUUUU
,而不是您发布的代码与您发布的摘要冲突的 YYYYMMDD-HHMMSS.UUU
。如果需要匹配正确的模式,可以更改它。
代码最多复制2个文件,因为它是由copied_file_1
和copied_file_2
的设置变量跟踪的。这两个相同的值用于通过电子邮件发送 2 个附件文件。
映射的驱动器连接是通过使用 pushd
完成的,最后使用 popd
将断开驱动器。如果最后使用popd
没有用到,那么在做脚本的时候驱动可能会保持exit
。因此,pushd
和 popd
需要配对才能正确完成。
您的代码将通配符路径复制到一个文件名。此代码使用 for
循环复制每个文件并使用原始日期戳。