在文件夹中自动打开pdf文件

时间:2017-12-12 12:35:48

标签: batch-file pdf automation watch

我需要创建一个批处理脚本,它应该充当监视文件夹。每当pdf文件登陆特定文件夹时,它应该自动打开,如果可能的话,它应该在两天后自行删除。

我试过了:

IF EXIST C:\TEMP\PDFbatch\*.pdf (
    CD C:\Temp\PDFBatch\
    for %%v in (*.pdf) do (
        "%%v"
        move "%%v" C:\TEMP\PDFbatch\Done\
    )
)
REM Delete files older than 2 days in Done folder
forfiles /p "C:\TEMP\PDFbatch\Done" /s /m *.pdf /D -2 /C "cmd /c del @PATH"

1 个答案:

答案 0 :(得分:0)

我建议使用以下注释批处理文件:

@echo off
cd /D C:\Temp\PDFBatch

:ResetCount
set "WaitCount=0"

rem Search in set directory for *.pdf files (not directories) with
rem archive attribute set. For each PDF file having archive attribute
rem set, remove the archive attribute and open the PDF file by default
rem application for PDF files.

:OpenLoop
for /F "delims=" %%I in ('dir *.pdf /AA-D /B 2^>nul') do (
    %SystemRoot%\System32\attrib.exe -a "%%I"
    start "" "%%I"
)

rem Wait 60 seconds before next loop run.
%SystemRoot%\System32\timeout.exe /T 60

rem After 3 hours (180 minutes) delete all PDF files older than 2 days.
set /A WaitCount+=1
if not %WaitCount% == 180 goto OpenLoop

rem Delete files older than 2 days in current folder.
%SystemRoot%\System32\forfiles.exe /p "C:\Temp\PDFBatch" /m *.pdf /D -2 /C "%SystemRoot%\System32\cmd.exe /c del @PATH"
goto ResetCount

要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • attrib /?
  • cd /?
  • del /?
  • dir /?
  • echo /?
  • for /?
  • forfiles /?
  • goto /?
  • rem /?
  • set /?
  • start /?
  • timeout /?

另请阅读有关Using Command Redirection Operators的Microsoft文章,了解2>nul的说明。重定向运算符>必须使用 FOR 命令行上的插入符^进行转义,以便在执行命令之前Windows命令解释程序处理此命令行时将其解释为文字字符FOR ,它在后台启动的单独命令进程中执行嵌入式dir命令行。