Skip to content

Instantly share code, notes, and snippets.

@Daenyth
Created December 16, 2009 15:22
Show Gist options
  • Save Daenyth/257909 to your computer and use it in GitHub Desktop.
Save Daenyth/257909 to your computer and use it in GitHub Desktop.
:: Written by Daenyth
:: Moves a given pdf file from Y:\ to the appropriate place in H:\
:: Lines starting with :: are comments.
:: Lines starting with : are labels.
@echo off
:: Allows the use of !name! notation for variables
SETLOCAL EnableDelayedExpansion EnableExtensions
:: filename=first parameter passed to the script
set filename=%1
:: Remove the "Y:\ bit
set filename=!filename:~4!
:: Remove the " from the end
set filename=!filename:"=!
:: Remove the .pdf file extension so I can generate a folder from their name
set basename=!filename:.pdf=!
:: Get current date so we know where to log to
for /f "tokens=2-4 delims=/ " %%g in ('date /t') do set date=%%i-%%g-%%h
set logdir="C:\Documents and Settings\administrator.BHC\My Documents\Scanned Patient Backups\"!date!
set logfile=!logdir!\"PDF Sorter Log.txt"
:: Create the log directory if it doesn't already exist
if not exist !logdir! (
mkdir !logdir!
echo ==^> Created logging directory: !logdir! >> !logfile!
)
echo. >> !logfile!
echo ==^> move_pdf_scan.bat called with %1; filename='!filename!', basename='!basename!' >> !logfile!
echo -^> %DATE% %TIME% >> !logfile!
if not exist "Y:\!filename!" (
echo -^> "Y:\!filename!" does not exist, aborting! >> !logfile!
GOTO:eof
)
:: Jump to the label that matches the first letter of the filename
goto !filename:~0,1!
:: Each label grouping below represents the folders that are under H:\
:: targetdir stores the folder name so that we know where to move the pdf
:A
:B
set targetdir=A-B
goto MoveFile
:C
:D
set targetdir=C-D
goto MoveFile
:E
:F
set targetdir=E-F
goto MoveFile
:G
:H
:I
:J
set targetdir=G-J
goto MoveFile
:K
:L
set targetdir=K-L
goto MoveFile
:M
:N
set targetdir=M-N
goto MoveFile
:O
:P
:Q
:R
set targetdir=O-R
goto MoveFile
:S
:T
set targetdir=S-T
goto MoveFile
:U
:V
:W
:X
:Y
:Z
set targetdir=U-Z
goto MoveFile
:MoveFile
:: clientdir is the name of the patient's folder
set clientdir=H:\%targetdir%\!basename!
echo -^> Client directory (clientdir) set to "!clientdir!" >> !logfile!
:: Copy the Client Template folder into the correct area by patient name, back up the pdf, move it, and delete the original
echo -^> Copying Client Template to clientdir >> !logfile!
if not exist "H:\Client Template" (
echo -^> Client Template doesn't seem to exist, aborting! >> !logfile!
GOTO:eof
)
xcopy /V /E /I H:\"Client Template" "!clientdir!" 1>> !logfile! 2>&1
:: Back up PDF in case something goes horribly wrong
echo -^> Copying Registration PDF to backup area (!logdir!) >> !logfile!
copy /V /Y Y:\"!filename!" !logdir! 1>> !logfile! 2>&1
:: Copy PDF to the patient's folder
echo -^> Copying Registration PDF to clientdir >> !logfile!
copy /V /Y Y:\"!filename!" "!clientdir!"\Registration 1>> !logfile! 2>&1
:: Delete the scanned PDF
echo -^> Deleting original PDF file >> !logfile!
del "Y:\!filename!" 1>> !logfile! 2>&1
echo ==^> Done >> !logfile!
echo. >> !logfile!
GOTO:eof
:: Written by Daenyth
:: Moves all scanned pdfs into their appropriate sorted folders.
:: Lines starting with :: are comments.
@echo off
:: Check that our helper script exists before continuing
if not exist "%~f0\..\move_pdf_scan.bat" (
echo ERROR: move_pdf_scan.bat does not exist! Make sure it's in the same folder as this script.
GOTO:eof
)
:: Calls the move_pdf_scan file as a subroutine for each pdf file in Y:\
FOR %%p IN (Y:\*.pdf) DO call "%~f0\..\move_pdf_scan.bat" "%%p"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment