批处理文件路径警告消息

时间:2018-08-25 12:33:07

标签: batch-file

我写了下面的脚本与pdftk结合使用。它会在多个目录中查找,如果有任何PDf,它将合并它们,删除原始文件并创建一个文件夹,然后将它们移动到该文件夹​​。我的问题是:有没有一种方法,如果一个或多个目录不存在,则会生成一条警告消息,提示该目录丢失了?

@echo off
Pushd c:\Directory1
 pdftk *.pdf cat output %fn%.pdf
 ren %fn%.pdf %fn%.xxx
 del *.pdf
 ren %fn%.xxx   "Daily Report".pdf
 for /f "tokens=1-3 delims=/" %%a in ('echo %date%') do set Date=%%a-%%b-%%c
 for %%f in (*.pdf) do ren "%%f" "%%~nf_%Date%_%time:~0,2%.%time:~3,2%.%time:~6,2%%%~xf"
 mkdir "Daily Reports for Review"
 move *.pdf "Daily Reports for Review"

Pushd c:\Directory2
 pdftk *.pdf cat output %fn%.pdf
 ren %fn%.pdf %fn%.xxx
 del *.pdf
 ren %fn%.xxx   "Daily Report".pdf
 for /f "tokens=1-3 delims=/" %%a in ('echo %date%') do set today=%%a-%%b-%%c
 for %%f in (*.pdf) do ren "%%f" "%%~nf_%Date%_%time:~0,2%.%time:~3,2%.%time:~6,2%%%~xf"
 mkdir "Daily Reports for Review"
 move *.pdf "Daily Reports for Review"

1 个答案:

答案 0 :(得分:1)

此答案与您上一个问题中的最后一个答案(尚未解决)一样,都假定您要将每个目录中的所有.pdf合并到一个输出文件中。在这种情况下,输出文件将被命名为Daily Report_mm-DD-YYYY_HH.MM.SS.pdf并被写入名为Daily Reports For Review的相对目录中。

@Echo Off
Set "DS="
For /F "Tokens=1-6 Delims=/: " %%A In ('RoboCopy/NJH /L "\|" Null'
) Do If Not Defined DS Set "DS=%%B-%%C-%%A_%%D.%%E.%%F"
If Not Defined DS Exit /B
Set "DN=Daily Reports for Review"
Set "FN=Daily Report"
For %%A In ("C:\Directory1" "C:\Directory2") Do If Not Exist "%%~A\" (
    Echo %%~A does not exist) Else (
    If Not Exist "%%~A\*.pdf" (Echo No PDF's in %%A) Else Call :Sub "%%~A")
Pause 
Exit /B

:Sub
PushD %1
If Not Exist "%DN%\" MD "%DN%"
PDFTk *.pdf cat output "%DN%\%FN%_%DS%.pdf" && Del /Q *.pdf
PopD
Exit /B

如果您没有将每个目录中的所有.pdf都写到单个输出文件中,则需要解释您在做什么,因为这是有关同一任务的第三个问题,而您没有充分解释了该任务


礼貌编辑
如果要将星期几名称作为字符串的一部分,可以使用WMIC生成DateStamp。

为此,请将上面脚本中的34行替换为:

Set "WeekDays=Sun Mon Tue Wed Thu Fri Sat"
For /F %%A In ('WMIC Path Win32_LocalTime Get /Value^|FindStr "[0-9]$"') Do Set "%%A"
For /F "Tokens=1%DayOfWeek%" %%A In (". . . . . . . . . %WeekDays%") Do Set "DayName=%%A"
For %%A In (Day Hour Minute Month Second) Do Call Set "%%A=0%%%%A%%" & Call Set "%%A=%%%%A:~-2%%"
Set "DS=%DayName%_%Month%-%Day%-%Year%_%Hour%.%Minute%.%Second%"