Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 创建批处理脚本以在任务计划程序中使用sendemail.exe_Batch File_Pdf_Cmd_Taskscheduler - Fatal编程技术网

Batch file 创建批处理脚本以在任务计划程序中使用sendemail.exe

Batch file 创建批处理脚本以在任务计划程序中使用sendemail.exe,batch-file,pdf,cmd,taskscheduler,Batch File,Pdf,Cmd,Taskscheduler,我试图创建一个小的批处理脚本,通过任务调度器运行,但遇到了一个问题,同一目录中有多个PDF文件 我的目标是: 映射远程NAS(这是为了从服务器上卸下负载,并作为备份) 将现有PDF移到存档文件夹(同一目录),最新的2个文件除外 清除存档以删除最旧的n 通过电子邮件将附件发送到我的地址 卸载NAS 迄今为止的守则: :: delete the old mapping NET USE A: /DELETE :: map the backup location NET USE A: \\NAS\S

我试图创建一个小的批处理脚本,通过任务调度器运行,但遇到了一个问题,同一目录中有多个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\u yyyyymmdd-hhmmssuuu.pdf
    Database02\u yyyyymmdd-hhmmssuuu.pdf
    。因此,当我复制时,无法限制两个PDF

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


    谢谢

    我通过将
    sendmail.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
    
    此代码遵循5个指定目标。我坚持与文件类似的概念,而不是您将文件夹添加到归档文件夹的想法

    ltd
    中存储的
    LocalDateTime
    值是
    YYYYMMDD-hhmmsuuu
    ,而不是您发布的代码中与您发布的摘要冲突的
    YYYYMMDD-HHMMSS.UUU
    。如果需要,可以更改它以匹配正确的模式

    代码最多复制2个文件,因为它由
    copied\u file\u 1
    copied\u file\u 2
    的设置变量跟踪。这两个相同的值用于通过电子邮件发送两个附件文件

    映射的驱动器连接通过使用
    pushd
    完成,最后使用
    popd
    将断开驱动器。如果未最终使用
    popd
    ,则当脚本退出
    时,驱动器可能会保留。因此,
    pushd
    popd
    需要配对才能正确完成


    代码将通配符路径复制到一个文件名。此代码使用
    for
    循环复制每个文件并使用原始日期戳。

    名为
    Database01_YYYYMMDD-hhmmsuuu.pdf
    的文件是否由其他程序生成?如果是这样的话,
    ldt
    有什么用,因为日期时间可以从文件名中重复使用。似乎包含单个常量值的
    ltd
    不适合基于通配符复制多个文件。我重写了您的一些代码,但现在我认为
    ltd
    已经过时,或者我误解了实际过程。@michael_heath这些名称是从另一个程序的输出中生成的,但后来改为
    Database0\u YYYYMMDD-HHMM.pdf
    %ltd%
    在电子邮件中用于通知脚本何时运行/发送电子邮件-我的错误是没有删除它。我已经解决了它,因为它是对
    sendmail.exe
    的限制,我正在使用
    cMail.exe
    ,因为它为他们提供了一个通配符功能。感谢您提供的信息,我想我走对了路。我发布了我的答案,希望能给你一些想法,而不是删除它
    cMail
    使用通配符支持似乎更好,这有助于保持代码的简单性。只需使用
    dir
    /s
    一起观看,同时按日期排序,因为
    dir
    是按目录排序的,因此如果多个目录中存在最新的PDF,则不一定要删除。
    @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