[创建批处理脚本以在Task Scheduler中使用sendemail.exe

问题描述 投票:0回答:1

我正在尝试创建一个小的批处理脚本以通过任务计划程序运行,但是在同一目录中存在多个PDF文件的问题。

我的目标是:

  1. 映射远程NAS(以减轻服务器负载并作为备份)
  2. 将最新的2个文件以外的现有PDF移到相同目录的存档文件夹中
  3. 清除存档以删除最早的n
  4. 通过电子邮件将附件发送到我的地址
  5. 卸载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 [email protected] -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

目前,我在从程序本身生成的文件方面遇到问题。我在Database01_YYYYMMDD-HHMMSSUUUU.pdf文件夹中收到两个文件Database02_YYYYMMDD-HHMMSSUUUU.pdfC:\Users\Administrator\Documents\My Database Backups。因此,当我进行复制时,无法限制两个PDF。

是否可以将根文件夹中的当前两个文件移动到一个文件夹中,然后将其移动到archive文件夹中。然后将最新的11个文件夹(运行12小时-根目录下为11 +1)保存在存档文件夹中。并将文件夹附加到sendemail.exe

谢谢!

batch-file pdf cmd taskscheduler
1个回答
0
投票

我解决了从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:[email protected] -to:[email protected] -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
© www.soinside.com 2019 - 2024. All rights reserved.