Todays post is mostly some powershell code to process Microsoft Document Imaging Files (.mdi or .tif) and converting them to PDF's using a program called MDI2PDF from Bugysoft.
Part of our specific problem is these files are really Microsoft Document Imaging Files but they have the .tif file extenstion. We wrote a PHP script to process all of our uploads from the one client that sends us these files and based on some tests we put the MDI Files into a specific directory for processing.
And now on to the Code. Lines that start with a # are comments for explaining what each line / section does.
#Setting up variables for our use
$input = "y:\allAuths\problemAuths\"
$output = "y:\allAuths\processAuths\"
$convert = "C:\Program Files\MDIConvertor\MDI2PDF.exe"
#Get the current number of errors present in the global $error variable.
$olderrorcount = $error.count
#Check to see if MDI2PDF is running. If it is this script will fail, so we need to kill it.
$proclist = get-process mdi2pdf -ea silentlycontinue
#If the number of errors is the same as is used to be then we can kill the running process(es). Otherwise we have nothing to kill and continue on.
if ($error.couint -eq $olderrorcount) {$proclist | stop-process}
#Here we get the contents of the input directory filtered for the type of file we want to convert.
$filelist = get-childitem -Path $input -Filter *.tif
#For every file in our file list we need to run the following command in order from top to bottom.
foreach ($file in $filelist)
{
#Replaces the .tif with .pdf.
$filename=$file -replace ($file.extension + "`$"),".pdf"
#Write something to the screen so the user knows something is happening.
write-host "Converting $input$file"
#Look ma! Simple string concatenation!!!
#Roughly equal to &"C:\program files\mdiconvertor\mdi2pdf.exe export:y:\allauths\problemauths\.tif y:\allauths\processauths\.tif.pdf
&$convert "export:$input$file $output$filename"
#get the process info for the currently running mdi2pdf process.
$convertprocess = get-process mdi2pdf
#Wait for the process to exit before moving on to the next one. I used to use sleep 10 here but this is much better.
$convertprocess.waitforexit()
#We need to check and make sure the converted file exists. If it does exist then we need to delete the original file.
#variable to store the input filename. We will need this later to delete the file.
$inputfile = $file.fullname
#Variable to store the output filename and full path. We use this later to check for its existance.
$outputfile = $output + $filename
#If the result of the path test is true we remove the file.
if (test-path $outputfile)
{remove-item $inputfile}
}