More PGP Fun
This process outlined is not unlike what I wrote about in my post here, but involves a bit more logic.
The script has to check for files that were posted that morning, but also have a file naming convention of the previous day. I've also got about 200 EMR postscript files to sort by date of service.
First, the source PGP/tar files must be checked for existence, and must be a pair with the proper date formatted file name.
@ECHO OFF SETLOCAL ENABLEDELAYEDEXPANSION :: ////////////////////////////////////////////// :: :: Set path variables for key access, passphrase, :: and source our key files. SET PGPPATH=C:\PGP_keys\ SET PGPPASS=MYPGPPASS :: The "KEY" to a successful import of our keys is running these coMMands as the user :: that will be running this script. :: pgp +batchmode -ka %PGPPATH%SECRING.SKR :: pgp +batchmode -ka %PGPPATH%PUBRING.PKR :: pgp -ke 0xEC671710 <-- after this coMMand, you will be asked to trust the key; this must be done! :: ////////////////////////////////////////////// :: :: Set our working dir, destination dir, _LOGFILE :: and get in there! SET _LOGFILE=H:\COVNENT\LOGS\MEDRECLOG.TXT SET _OPATH=H:\CLIENT\MEDICA~1 SET _WORKDIR=H:\CLIENT\WORKDIR H: CD \ CD COVNENT\ :: ////////////////////////////////////////////// :: :: Log beautification ECHO --------------------------------------------------------------------------- >> %_LOGFILE% ECHO -- START %DATE% - %TIME% -- >> %_LOGFILE% ECHO --------------------------------------------------------------------------- >> %_LOGFILE% :: ////////////////////////////////////////////// :: :: First test for existance of files, and :: notify us if they don't exist. DIR /B *.TAR.PGP IF ERRORLEVEL 1 SET _MSG=No Medical Record files were found. & ECHO #### ERROR - NO FILES FOUND #### >> %_LOGFILE% & GOTO SENDMAIL :: ////////////////////////////////////////////// :: :: Make sure our file count is correct. It should be 2. DIR /B *.TAR.PGP | FIND /C /V "" > COUNT.TXT FOR /F "tokens=1" %%f IN (COUNT.TXT) DO ( IF NOT %%f==2 SET _MSG=The file count of the Medical Record files is %%f, and 2 were expected. & DEL COUNT.TXT & ECHO #### ERROR - FILE COUNT WAS ONLY %%f. #### >> !_LOGFILE! & GOTO SENDMAIL ) :: ////////////////////////////////////////////// :: :: Delete the count file check DEL COUNT.TXT :: ////////////////////////////////////////////// :: :: Now, get the date in the form of MMDD, subtract 1, and compare it to :: what we received from CLIENT, which SHOULD :: be one less than today. If not, send an email so it :: can be checked out. FOR /F "tokens=2-4 delims=/ " %%f IN ('DATE /T') DO ( SET MM=%%f SET DD=%%g SET YYYY=%%h ) :: Substract your days here SET /A DD=1%DD% - 100 - 1 SET /A MM=1%MM% - 100 :CHKDAY IF /I %DD% GTR 0 GOTO DONE SET /A MM=%MM% - 1 IF /I %MM% GTR 0 GOTO ADJUSTDAY SET /A MM=12 SET /A YYYY=%YYYY% - 1 :ADJUSTDAY IF %MM%==1 GOTO SET31 IF %MM%==2 GOTO LEAPCHK IF %MM%==3 GOTO SET31 IF %MM%==4 GOTO SET30 IF %MM%==5 GOTO SET31 IF %MM%==6 GOTO SET30 IF %MM%==7 GOTO SET31 IF %MM%==8 GOTO SET31 IF %MM%==9 GOTO SET30 IF %MM%==10 GOTO SET31 IF %MM%==11 GOTO SET30 REM ** Month 12 falls through :SET31 SET /A DD=31 + %DD% GOTO CHKDAY :SET30 SET /A DD=30 + %DD% GOTO CHKDAY :LEAPCHK SET /A TT=%YYYY% %% 4 IF NOT %TT%==0 GOTO SET28 SET /A TT=%YYYY% %% 100 IF NOT %TT%==0 GOTO SET29 SET /A TT=%YYYY% %% 400 IF %TT%==0 GOTO SET29 :SET28 SET /A DD=28 + %DD% GOTO CHKDAY :SET29 SET /A DD=29 + %DD% GOTO CHKDAY :DONE SET _TODAY=%MM%%DD% FOR /F %%f IN ('DIR /B *.TAR.PGP') DO ( SET _FILE=%%f REM ///////////////////////////////////////// REM This bit checks and removes leading zeros REM our MMDD check SET _MCHECK=!_FILE:~13,1! SET _DCHECK=!_FILE:~15,1! IF !_MCHECK!==0 (SET _FMM=!_FILE:~14,1!) ELSE (SET _FMM=!_FILE:~13,2!) IF !_DCHECK!==0 (SET _FDD=!_FILE:~16,1!) ELSE (SET _FDD=!_FILE:~15,2!) SET _FDATE=!_FMM!!_FDD! IF NOT !_FDATE! == !_TODAY! SET _MSG=The date of the Medical Record file, %%f, was not what was expected. It should be !_TODAY!. --- FILE DATE = !_FDATE!, RUN DATE = !_TODAY! & ECHO #### ERROR - DATE MISMATCH - FILE DATE = !_FDATE!, RUN DATE = !_TODAY! #### >> !_LOGFILE! & GOTO SENDMAIL ) :: ////////////////////////////////////////////// :: Now that we've passed the initial conditions :: move the files to our workdir MOVE *.TAR.PGP %_WORKDIR% CD WORKDIR :: ////////////////////////////////////////////// :: :: Loop through our tarred and encrypted files, :: decrypt them and enter a log entry for each :: file processed. We loop, becuase pgp won't :: deal with wildcards in the file name. FOR /F %%f IN ('DIR /B ZYX*.TAR.PGP') DO (SET _ZYXFILE=%%f) PGP -p %_ZYXFILE% IF %ERRORLEVEL% NEQ 0 (SET _ERR=%ERRORLEVEL% & SET _FILE=%_ZYXFILE% & SET _MSG=There has been a PGP Decryption Error on the Medical Record file, %_ZYXFILE%. NO FILES WERE EXTRACTED. & GOTO PGPERR) ELSE (ECHO DECRYPTED %_ZYXFILE% >> %_LOGFILE% & MOVE %_ZYXFILE% %_OPATH%) :: ///////////////////////////////////////////// :: Now do the same for XYZ FOR /F %%f IN ('DIR /B XYZ*.TAR.PGP') DO (SET _XYZFILE=%%f) PGP -p %_XYZFILE% IF %ERRORLEVEL% NEQ 0 (SET _ERR=%ERRORLEVEL% & SET _FILE=%_XYZFILE% & SET _MSG=There has been a PGP Decryption Error on the Medical Record file, %_XYZFILE%. NO FILES WERE EXTRACTED. & GOTO PGPERR) ELSE (ECHO DECRYPTED %_XYZFILE% >> %_LOGFILE% & MOVE %_XYZFILE% %_OPATH%) :: ////////////////////////////////////////////// :: :: Loop through our tarred files, working on ZYX :: files, then extract them to our working directory :: We loop, because 7z won't deal with wildcards :: in the file name. FOR /F %%f IN ('DIR /B ZYX*.TAR') DO ( FOR /F "tokens=3" %%C IN ('C:\Progra~1\7-zip\7z.exe l %%f ^| FIND /I "files"') DO SET _FCOUNT=%%C "%PROGRAMFILES%"\7-zip\7z.exe x %%f -aoa -o!_OPATH!\ZYX\ IF !ERRORLEVEL! NEQ 0 (SET _ERR=!ERRORLEVEL! & SET _FILE=%%f & SET _MSG=There has been a untar error on the Medical Record file, %%f. & GOTO TARERR) ELSE (ECHO EXTRACTED !_FCOUNT! RECORDS FROM %%f. >> !_LOGFILE! & DEL %%f) ) :: ////////////////////////////////////////////// :: :: Do the same for XYZ files FOR /F %%f IN ('DIR /B XYZ*.TAR') DO ( FOR /F "tokens=3" %%C IN ('C:\Progra~1\7-zip\7z.exe l %%f ^| FIND /I "files"') DO SET _FCOUNT=%%C "%PROGRAMFILES%"\7-zip\7z.exe x %%f -aoa -o%_OPATH%\XYZ\ IF !ERRORLEVEL! NEQ 0 (SET _ERR=!ERRORLEVEL! & SET _FILE=%%f & SET _MSG=There has been a untar error on the Medical Record file, %%f. & GOTO TARERR) ELSE (ECHO EXTRACTED !_FCOUNT! RECORDS FROM %%f. >> !_LOGFILE! & DEL %%f) ) GOTO EOF :PGPERR :: ////////////////////////////////////////////// :: :: Log all PGP errors, send an email, and quit IF %_ERR%==33 ECHO #### %_FILE% CAUSED ERROR - DECOMPRESSION ERROR #### >> %_LOGFILE% IF %_ERR%==32 ECHO #### %_FILE% CAUSED ERROR - DECRYPTION ERROR #### >> %_LOGFILE% IF %_ERR%==31 ECHO #### %_FILE% CAUSED ERROR - PUBLIC KEY DECRYPTION ERROR #### >> %_LOGFILE% IF %_ERR%==30 ECHO #### %_FILE% CAUSED ERROR - SIGNATURE CHECK ERROR #### >> %_LOGFILE% IF %_ERR%==19 ECHO #### %_FILE% CAUSED ERROR - KEY SIGNATURE REMOVAL ERROR #### >> %_LOGFILE% IF %_ERR%==18 ECHO #### %_FILE% CAUSED ERROR - KEY SIGNATURE ERROR OR KEY SIGNATURE REVOKE ERROR #### >> %_LOGFILE% IF %_ERR%==17 ECHO #### %_FILE% CAUSED ERROR - KEYRING CHECK ERROR #### >> %_LOGFILE% IF %_ERR%==16 ECHO #### %_FILE% CAUSED ERROR - KEYRING REMOVAL ERROR #### >> %_LOGFILE% IF %_ERR%==15 ECHO #### %_FILE% CAUSED ERROR - KEYRING VIEW ERROR #### >> %_LOGFILE% IF %_ERR%==14 ECHO #### %_FILE% CAUSED ERROR - KEYRING EDIT ERROR #### >> %_LOGFILE% IF %_ERR%==13 ECHO #### %_FILE% CAUSED ERROR - KEYRING EXTRACT ERROR #### >> %_LOGFILE% IF %_ERR%==12 ECHO #### %_FILE% CAUSED ERROR - KEYRING ADD ERROR #### >> %_LOGFILE% IF %_ERR%==11 ECHO #### %_FILE% CAUSED ERROR - NON-EXISTING KEY ERROR #### >> %_LOGFILE% IF %_ERR%==10 ECHO #### %_FILE% CAUSED ERROR - KEY GENERATION ERROR #### >> %_LOGFILE% IF %_ERR%==7 ECHO #### %_FILE% CAUSED ERROR - OUT OF MEMORY ERROR #### >> %_LOGFILE% IF %_ERR%==6 ECHO #### %_FILE% CAUSED ERROR - PROCESS INTERRUPTED #### >> %_LOGFILE% IF %_ERR%==5 ECHO #### %_FILE% CAUSED ERROR - BAD ARGUMENT #### >> %_LOGFILE% IF %_ERR%==4 ECHO #### %_FILE% CAUSED ERROR - BATCHMODE ERROR #### >> %_LOGFILE% IF %_ERR%==3 ECHO #### %_FILE% CAUSED ERROR - UNKNOWN FILE #### >> %_LOGFILE% IF %_ERR%==2 ECHO #### %_FILE% CAUSED ERROR - FILE NOT FOUND #### >> %_LOGFILE% IF %_ERR%==1 ECHO #### %_FILE% CAUSED ERROR - INVALID FILE #### >> %_LOGFILE% GOTO SENDMAIL :TARERR :: ////////////////////////////////////////////// :: :: Log all tar errors, send an email, and quit IF %_ERR%==255 ECHO #### %_FILE% CAUSED ERROR - USER STOPPED PROCESS #### >> %_LOGFILE% IF %_ERR%==8 ECHO #### %_FILE% CAUSED ERROR - NOT ENOUGH MEMORY #### >> %_LOGFILE% IF %_ERR%==7 ECHO #### %_FILE% CAUSED ERROR - COMMAND LINE ERROR #### >> %_LOGFILE% IF %_ERR%==2 ECHO #### %_FILE% CAUSED ERROR - FATAL ERROR #### >> %_LOGFILE% GOTO SENDMAIL :SENDMAIL :: ////////////////////////////////////////////// :: :: Set the temp file location SET _TEMPMAIL=%TEMP%\TEMPMAIL.%RANDOM%.TXT :: ////////////////////////////////////////////// :: :: Echo the basic headers to the temp file ECHO TO: "Croson, John" ^> %_TEMPMAIL% ECHO FROM: "IHBS Administrator" ^ >> %_TEMPMAIL% ECHO SUBJECT: SCRIPT FAILURE >> %_TEMPMAIL% :: ////////////////////////////////////////////// :: :: Echo the blank line that separates the header from the body text ECHO.>>%_TEMPMAIL% :: ////////////////////////////////////////////// :: :: Echo the body text to the temp file ECHO %_MSG% >> %_TEMPMAIL% ECHO Check %_LOGFILE% for details.>> %_TEMPMAIL% :: ////////////////////////////////////////////// :: :: Move the temp file to the mail pickup directory MOVE %_TEMPMAIL% C:\INETPUB\MAILROOT\PICKUP ENDLOCAL EXIT :EOF START "EMS File Sorting" /MIN WSCRIPT.EXE C:\SCRIPTS\ManageFiles.vbs ENDLOCAL EXIT
Now that the files are decrypted and un-tarred, they must be sorted into an archive directory and a processing directory. This is easier using vb scripting, and WSH objects.
Option Explicit Dim FSO Dim sFolder Dim sFile Dim sWffSrc Dim sSfhSrc Dim sWffArch Dim sSfhArch Dim sWffAqu Dim sSfhAqu Dim sFileColl Dim inFile Dim i, l, arrFileLines() Dim sDate Dim sDay Dim sMonth Dim sYear Dim net Dim sMail Dim sStartTime Dim sFinishTime Dim sNumWffFiles Dim sNumSfhFiles ' /////////////////////////////////////////// ' Get our path environments worked out... sWffSrc = "H:\CLIENT\Medical Records\WFF\xtra\audit\" sSfhSrc = "H:\CLIENT\Medical Records\SFH\xtra\audit\" sWffArch = "H:\CLIENT\Medical Records\WFF\rpt_arch\" sSfhArch = "H:\CLIENT\Medical Records\SFH\rpt_arch\" sWffAqu = "M:\Aquarius\4CLI\WFF\" sSfhAqu = "M:\Aquarius\4CLI\SFH\" ' /////////////////////////////////////////// ' Set the start time sStartTime = Now() ' /////////////////////////////////////////// ' Create the FSO object we need to use Set FSO = CreateObject("Scripting.FileSystemObject") ' /////////////////////////////////////////// ' Before we do anything, make sure we have ' an H and M drive Set net = CreateObject("WScript.Network") If FSO.GetDriveName("M:") = "" Then net.MapNetworkDrive "M:", "\\MYServer\ScanDocs","False" End If If FSO.GetDriveName("H:") = "" Then net.MapNetworkDrive "H:", "\\MYServer\Download","False" End If ' /////////////////////////////////////////// ' Set the folder and file collection Set sFolder = FSO.GetFolder(sWffSrc) Set sFileColl = sFolder.Files ' /////////////////////////////////////////// ' Loop through each file in sWffSrc ' and create an array of lines in each file For Each sFile in sFileColl Set inFile = FSO.OpenTextFile(sWffSrc & sFile.Name, 1) Do Until inFile.AtEndOfStream Redim Preserve arrFileLines(i) arrFileLines(i) = inFile.ReadLine i = i + 1 Loop ' close the file, we don't need it anymore inFile.Close ' /////////////////////////////////////////// ' Now read the contents of the file, from the bottom up, ' since we only need the %date% string to determine where ' the file belongs in our filesystem For l = Ubound(arrFileLines) to LBound(arrFileLines) Step -1 If Left(arrFileLines(l),6) = "%date%" Then sDate = Replace(Mid(arrFileLines(l),7,8), "/", "") sMonth = Left(sDate, 2) sYear = Left(Year(Now), 2) & Right(sDate, 2) sDay = Mid(sDate, 3, 2) ' /////////////////////////////////////////// ' Check for the existence of the archive folders ' /////////////////////////////////////////// ' First check for the year If FSO.FolderExists(sWffArch & sYear) Then 'MsgBox "Folder " & sWffArch & sYear & " exists." ' /////////////////////////////////////////// ' Now check for the month If FSO.FolderExists(sWffArch & sYear & "\" & sMonth & sYear) Then 'MsgBox "Folder " & sWffArch & sYear & "\" & sMonth & sYear & " exists." ' /////////////////////////////////////////// ' Now check for the day If FSO.FolderExists(sWffArch & sYear & "\" & sMonth & sYear & "\" & sMonth & sDay & sYear) Then 'MsgBox "Folder " & sWffArch & sYear & "\" & sMonth & sYear & "\" & sMonth & sDay & sYear & " exists." Else FSO.CreateFolder(sWffArch & sYear & "\" & sMonth & sYear & "\" & sMonth & sDay & sYear) 'MsgBox "Folder " & sWffArch & sYear & "\" & sMonth & sYear & "\" & sMonth & sDay & sYear & " created." End If 'Day check Else ' /////////////////////////////////////////// ' Month doesn't exist, and anything below it ' so create it. FSO.CreateFolder(sWffArch & sYear & "\" & sMonth & sYear) FSO.CreateFolder(sWffArch & sYear & "\" & sMonth & sYear & "\" & sMonth & sDay & sYear) End If 'Month check Else ' /////////////////////////////////////////// ' Year doesn't exist, and nothing below it ' so create it. FSO.CreateFolder(sWffArch & sYear) FSO.CreateFolder(sWffArch & sYear & "\" & sMonth & sYear) FSO.CreateFolder(sWffArch & sYear & "\" & sMonth & sYear & "\" & sMonth & sDay & sYear) End If 'Year check ' /////////////////////////////////////////// ' Now lets copy/move the file, since we've ' created the folders ' /////////////////////////////////////////// ' First copy file to the aquarius folders for indexing ' But make the destination folder if it doesn't exist If FSO.FolderExists(sWffAqu & sMonth & sDay & sYear) Then FSO.CopyFile sWffSrc & sFile.Name, sWffAqu & sMonth & sDay & sYear & "\" Else FSO.CreateFolder(sWffAqu & sMonth & sDay & sYear) FSO.CopyFile sWffSrc & sFile.Name, sWffAqu & sMonth & sDay & sYear & "\" End If ' /////////////////////////////////////////// ' Now copy the file to the archive folder ' and delete the original (move fails if file exists ' and copy overwrites without error). FSO.CopyFile sWffSrc & sFile.Name, sWffArch & sYear & "\" & sMonth & sYear & "\" & sMonth & sDay & sYear & "\" FSO.DeleteFile sWffSrc & sFile.Name ' /////////////////////////////////////////// ' Get out of this loop since we've found %date% Exit For End If '%date% check Next sNumWffFiles = sNumWffFiles + 1 Next ' /////////////////////////////////////////// ' Now do the same thing for the SFH files ' /////////////////////////////////////////// ' /////////////////////////////////////////// ' Clear our objects, and reset them Set sFolder = Nothing Set sFileColl = Nothing ' /////////////////////////////////////////// ' Set the folder and file collection Set sFolder = FSO.GetFolder(sSfhSrc) Set sFileColl = sFolder.Files ' /////////////////////////////////////////// ' Loop through each file in sSfhSrc ' and create an array of lines in each file For Each sFile in sFileColl Set inFile = FSO.OpenTextFile(sSfhSrc & sFile.Name, 1) Do Until inFile.AtEndOfStream Redim Preserve arrFileLines(i) arrFileLines(i) = inFile.ReadLine i = i + 1 Loop ' close the file, we don't need it anymore inFile.Close ' /////////////////////////////////////////// ' Now read the contents of the file, from the bottom up, ' since we only need the %date% string to determine where ' the file belongs in our filesystem For l = Ubound(arrFileLines) to LBound(arrFileLines) Step -1 If Left(arrFileLines(l),6) = "%date%" Then sDate = Replace(Mid(arrFileLines(l),7,8), "/", "") sMonth = Left(sDate, 2) sYear = Left(Year(Now), 2) & Right(sDate, 2) sDay = Mid(sDate, 3, 2) ' /////////////////////////////////////////// ' Check for the existence of the archive folders ' /////////////////////////////////////////// ' First check for the year If FSO.FolderExists(sSfhArch & sYear) Then ' /////////////////////////////////////////// ' Now check for the month If FSO.FolderExists(sSfhArch & sYear & "\" & sMonth & sYear) Then ' /////////////////////////////////////////// ' Now check for the day If FSO.FolderExists(sSfhArch & sYear & "\" & sMonth & sYear & "\" & sMonth & sDay & sYear) Then Else FSO.CreateFolder(sSfhArch & sYear & "\" & sMonth & sYear & "\" & sMonth & sDay & sYear) End If 'Day check Else ' /////////////////////////////////////////// ' Month doesn't exist, and anything below it ' so create it. FSO.CreateFolder(sSfhArch & sYear & "\" & sMonth & sYear) FSO.CreateFolder(sSfhArch & sYear & "\" & sMonth & sYear & "\" & sMonth & sDay & sYear) End If 'Month check Else ' /////////////////////////////////////////// ' Year doesn't exist, and nothing below it ' so create it. FSO.CreateFolder(sSfhArch & sYear) FSO.CreateFolder(sSfhArch & sYear & "\" & sMonth & sYear) FSO.CreateFolder(sSfhArch & sYear & "\" & sMonth & sYear & "\" & sMonth & sDay & sYear) End If 'Year check ' /////////////////////////////////////////// ' Now lets copy/move the file, since we've ' created the folders ' /////////////////////////////////////////// ' First copy file to the aquarius folders for indexing ' But make the destination folder if it doesn't exist If FSO.FolderExists(sSfhAqu & sMonth & sDay & sYear) Then FSO.CopyFile sSfhSrc & sFile.Name, sSfhAqu & sMonth & sDay & sYear & "\" Else FSO.CreateFolder(sSfhAqu & sMonth & sDay & sYear) FSO.CopyFile sSfhSrc & sFile.Name, sSfhAqu & sMonth & sDay & sYear & "\" End If ' /////////////////////////////////////////// ' Now copy the file to the archive folder 'MsgBox "File " & sSfhSrc & sFile.Name & vbNewLine & " moved to " & vbNewLine & sSfhArch & sYear & "\" & sMonth & sYear & "\" & sMonth & sDay & sYear & "." FSO.CopyFile sSfhSrc & sFile.Name, sSfhArch & sYear & "\" & sMonth & sYear & "\" & sMonth & sDay & sYear & "\" FSO.DeleteFile sSfhSrc & sFile.Name ' /////////////////////////////////////////// ' Get out of this loop since we've found %date% Exit For End If '%date% check Next sNumSfhFiles = sNumSfhFiles + 1 Next ' /////////////////////////////////////////// ' Set the finish time sFinishTime = Now() ' ////////////////////////////// ' Now send an email notification that the ' files are ready. Set sMail = FSO.CreateTextFile("C:\Temp\TEMPMAIL.TXT", True) sMail.WriteLine("TO: " & Chr(34) & "JOHN CROSON" & Chr(34) & "<john.croson@ihbsonline.com>," & Chr(34) & "John Croson" & Chr(34) & "<pcnorb@yahoo.com>") sMail.WriteLine("FROM: " & Chr(34) & "IHBS Administrator" & Chr(34) & "<admin@ihbsonline.com>") sMail.WriteLine("SUBJECT: EMS EMR File Sorting") sMail.WriteBlankLines(1) sMail.WriteLine("Electronic Medical Record sorting is finished.") sMail.WriteBlankLines(1) sMail.WriteLine("Start time was " & sStartTime & ". Finish time was " & sFinishTime & ".") sMail.WriteLine("The number of WFF files was " & sNumWffFiles & ". The number of SFH files was " & sNumSfhFiles & ".") sMail.Close FSO.MoveFile "C:\Temp\TEMPMAIL.TXT", "C:\Inetpub\MAILROOT\PICKUP\"
Comments