【问题标题】:open pdf file automatically in folder在文件夹中自动打开pdf文件
【发布时间】:2018-05-26 03:36:09
【问题描述】:
我需要创建一个作为监视文件夹的批处理脚本。每当一个 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"
【问题讨论】:
标签:
batch-file
pdf
automation
watch
【解决方案1】:
我建议使用以下注释的批处理文件:
@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 命令行。