Software > Computer programming

Batch Files, Select all files EXCEPT

<< < (5/5)

mechaflash:
Final Code:
Added a way to exit the script if there aren't any files to process, as well as variable cleanup.


--- Code: ---@echo off
REM move to the right location
CD c:\test

REM must be on to enumerate correctly
setlocal enabledelayedexpansion

REM Create string, set it to zero length
set AttachmentList=

REM Set error-out counter
set /a error=-1

REM Examine each file, if it is not a pdf, add its name to the list followed by a comma
for %%F in (c:\test\*.*) do if not "%%~xF"==".pdf" (set AttachmentList=!AttachmentList!%%~dpnxF,) & (set /a error=!error!+1)

REM End script if no files are processed
IF %error% LSS 0 GOTO cleanup

REM Remove final character (a comma)
set AttachmentList=%AttachmentList:~0,-1%

REM Compose email with attachments
"c:\Program Files\Mozilla Thunderbird\thunderbird.exe" -compose "to='[email protected]',subject='Non-PDF File',preselectid='id',body='Please ensure that the entity is sending us a PDF file and not any other file type. Attached is the file in question.',attachment='%Attachmentlist%'"

REM Run script to send email
START /WAIT c:\test\sendkeys.vbs

REM Move files to finished folder
for %%F in (c:\test\*.*) do if not "%%~xF"==".pdf" (move "%%~fF" "c:\test\finish\%%~nxF")
GOTO CLEANUP

:CLEANUP
set AttachmentList=
set /a error=0
localoff
GOTO NEXT

:NEXT
START c:\test\mainprogram.vbs
--- End code ---

After this code runs, I run a simple .vbs for sendkeys to send the email automatically (no way to auto-send in thunderbird).  Then voila, no more looking in the directory to ensure we haven't received something other than a .PDF file. 

Purpose of creating this extra script:
I have an auto-print program that I created for my company that works in conjunction w/Thunderbird.  Thunderbird automatically pulls the attachments from all emails at a specified time.  Then all of the extracted PDFs are printed to a network printer for processing.  Then another script runs to check for the date of the file and delete any files older than 30 days.  Used daily and processes approximately 1200 documents a month.  The only problem I was having is receiving files that weren't PDF files, and they sometimes were unrelated to the task, but important to process none-the-less.  We did not want them mixed in with the rest of the files, but it still needed to be brought to the attention of the correct staff member.  So this is it...

TANXSOOMUCH SALMON!

Salmon Trout:
You can remove the GOTO CLEANUP and GOTO NEXT lines in blue below - the code is gong to go to those labels anyway so they are superfluous.

REM Move files to finished folder
for %%F in (c:\test\*.*) do if not "%%~xF"==".pdf" (move "%%~fF" "c:\test\finish\%%~nxF")
GOTO CLEANUP

:CLEANUP
set AttachmentList=
set /a error=0
localoff
GOTO NEXT

:NEXT
START c:\test\mainprogram.vbs

Navigation

[0] Message Index

[*] Previous page

Go to full version