 :: Created by Ryan Cragg 2008-02-15
 :: Last edited by Ryan 2008-02-15
 :: it is probably a good idea to keep track of when a file is edited, and by who
@ECHO OFF  
 
 :: The following line is necessary for ERRORLEVEL capture.

SetLocal EnableDelayedExpansion

 :: Set the name of the report file and other file locations
 :: You might as well just set them once--easier to update
 
SET OUTPUT=.\output
SET LOG=.\logs
SET COMPLETED=.\completed
SET FAILED=.\failed
SET SOURCE=%CD%
SET REPORT=report.txt

 :: If the directories don't exist, later commands run into problems.
 
MD %OUTPUT%
MD %LOG%
MD %COMPLETED%
MD %FAILED%


 :: It is probably a good idea to delete the existing log files
 :: If you don't, the report might not be accurate.

DEL /Q %LOG%\*.*


 :: The following command creates the report file
 :: showing date, time, and FME build.  First line It is offset for formatting
 :: Use >> if you want to append to an existing report file
 
ECHO          This translation run started on %date% at %time% using> %REPORT%
fme | find "Build" >>%REPORT%

 :: The FOR loop runs the actual translation and moves the files
 :: If the FIND finds SUCCESSFUL in the log, the load translation is run.
 :: Otherwise, the file is placed in the FAILED directory.
 
FOR %%F IN ("%SOURCE%\*.dwg") DO (
   fme acad2gml_validate.fmw --SourceDataset_ACAD "%%F"^
                             --LOG_FILE "%LOG%\%%~nF_validate.log"
                           
   FIND "Translation was SUCCESSFUL" "%LOG%\%%~nF_validate.log" >> %REPORT% 
 
   IF !ERRORLEVEL! EQU 0 (
      ECHO %%F PASSED VALIDATION with ERRORLEVEL !ERRORLEVEL! >> %REPORT%
      :: If the first translation if a validation, you can 
      :: run the load translation here, with its own log file test.
      fme acad2gml_load.fmw --SourceDataset_ACAD "%%F"^
                            --DestDataset_GML "%OUTPUT%\%%~nF.gml"^
                            --LOG_FILE "%LOG%\%%~nF_load.log"
    
      FIND "Translation was SUCCESSFUL" %LOG%\%%~nF_load.log >> %REPORT%
         IF !ERRORLEVEL! EQU 0 (
            ECHO %%F PASSED LOADING with ERRORLEVEL !ERRORLEVEL! >> %REPORT%
            MOVE /Y "%%F" "%COMPLETED%"
            ) ELSE (         	
         	  ECHO %%F FAILED LOADING with ERRORLEVEL !ERRORLEVEL! >> %REPORT%
            FIND "|ERROR |" %LOG%\%%~nF_load.log >> %REPORT%
            MOVE /Y "%%F" "%FAILED%"
         )
         
   ) ELSE (
        ECHO %%F FAILED VALIDATION with ERRORLEVEL !ERRORLEVEL! >> %REPORT%
        FIND "|ERROR |" %LOG%\%%~nF_validate.log >> %REPORT%
        MOVE /Y "%%F" "%FAILED%"
   )
)
  
 :: Just to be helpful, let's list the failed files in the report file

ECHO. >> %REPORT% 
ECHO The following files failed translation: >> %REPORT%
DIR /B %FAILED% >> %REPORT%
  
 :: If something went wrong, we email the report.txt to the interested parties
 :: I use BLAT for this (http://www.blat.net/). You can use any command-line emailer.
  
FIND "FAILED" report.txt

IF !ERRORLEVEL! EQU 0 (
   @.\blat\blat %REPORT%^
         -to rjcragg@gmail.com^
         -f rjcragg@shaw.ca^
         -subject "I'm afraid I have some bad news..."^
         -server shawmail.vc.shawcable.net^
         -u rjcragg@shaw.ca^
         -pw %PW%
)
   
              
 :: I've been told to use the following command.
 :: I don't know what happens if I don't.
 
EndLocal

pause