The Power of......Batch Scripts!

downtapdowntap Member Posts: 18
edited 2014-05-26 in NAV Tips & Tricks
Now, this may be common knowledge for all you veterans out there, but I didn't find too much information on the topic.

Some of us may have loads and loads of documents/images/files outside of Navision that may need to be compiled or retrieved for other functions. For example, my company has loads of images that get digitally delivered to our clients on a daily basis.

Before, we used to grab and compile these manually before sending them on to our clients. But, with all the data of what needs to be grabbed existing within Navision, I knew there had to be an easier way.

Please note, all code has been simplified to the minimal needed. Expand upon as necessary.

What I first did, was create a processing only report to create the batch file.

OnPreDataItem:
DocNo := "Document No.";

WITH OutFile DO BEGIN
  TEXTMODE := TRUE;
  WRITEMODE := TRUE;
  CREATE(STRSUBSTNO('FullFilePath\BatchScripts\%1.bat', DocNo));
  CREATEOUTSTREAM(OutFileStream);
END;
OnAfterGetRecord:
BatchText := STRSUBSTNO(Text001);
OutFileStream.WRITETEXT(STRSUBSTNO('%1%2',BatchText,CRLF));

OnPostDataItem:
OutFile.CLOSE;
SHELL(STRSUBSTNO(''FullFilePath\BatchScripts\%1.bat', DocNo));

The images to grab are organized by a specific Document No., hence using that as the common variable to grab the data. I'm using text constants for the script for ease, but you may not find that necessary. The use of the SHELL command will run the batch file automatically after creating it. In an advanced application of this, I have several functions running on every record. Those all simply need to be called in OnAfterGetRecord. To state the obvious, for any script that needs to run only once at the beginning, call it on the OnPreDataItem. And for any script that needs to run only once at the end, call it on OnPostDataItem. For instance in my application of this creates a batch file as such:
pushd \\MyBatchFileLocation\
mkdir \Images\2009-12-23
mkdir \ImagePacks\2009-12-23\DocNo
copy \OriginalImageLocation1\*.tif \NewImageLocation\
copy \OriginalImageLocation2\*.tif \NewImageLocation\
copy \OriginalImageLocation3\*.tif \NewImageLocation\
copy \OriginalImageLocation4\*.tif \NewImageLocation\
copy \OriginalImageLocation5\*.tif \NewImageLocation\
copy \OriginalImageLocation6\*.tif \NewImageLocation\
copy \OriginalImageLocation7\*.tif \NewImageLocation\
copy \OriginalImageLocation8\*.tif \NewImageLocation\
copy \OriginalImageLocation9\*.tif \NewImageLocation\
7za a -tzip /NewImageLocation/DocNo.zip /NewImageLocation/*

DocNo=The document no. from Navision I'm compiling images from. This script for us, will take individual groups of images that are stored by date in separate places and copy them all to a new central location, so that they can all be zipped up into one package easily. (Note, I am using the 7-zip utility to zip the images) It may not be necessary to copy all the files, but for us it is necessary.

I know this may not be a common need, but being resourceful and coming up with a process utilizing Navision's data power and a little outside of the box thinking, has saved us a tremendous amount of time.

Another quick example we use by utilizing pdftoolkit:
pushd \\MyBatchFileLocation\
mkdir \ImagePacks\2010-01-14 
pdftk /PDFLocation1/Doc1.pdf /2ndPDFLocation/*.pdf  output /ImagePacks/DocNo.pdf
pdftk /PDFLocation2/Doc1.pdf /2ndPDFLocation/*.pdf  output /ImagePacks/DocNo.pdf
pdftk /PDFLocation3/Doc1.pdf /2ndPDFLocation/*.pdf  output /ImagePacks/DocNo.pdf
pdftk /PDFLocation4/Doc1.pdf /2ndPDFLocation/*.pdf  output /ImagePacks/DocNo.pdf
pdftk /PDFLocation5/Doc1.pdf /2ndPDFLocation/*.pdf  output /ImagePacks/DocNo.pdf
pdftk /PDFLocation6/Doc1.pdf /2ndPDFLocation/*.pdf  output /ImagePacks/DocNo.pdf
pdftk /PDFLocation7/Doc1.pdf /2ndPDFLocation/*.pdf  output /ImagePacks/DocNo.pdf

I've obviously simplified this a bunch, but same premise as before. With pdftoolkit, am able to grab individual pdf images, or groups of pdf images and create a cingular pdf. This process for us is great to process multiple document numbers all at once.

Comments

  • kinekine Member Posts: 12,562
    Just one hint: Do not use .bat extension but .cmd. Bat was used for old 16bit applications. .cmd is for 32/64 bit. But yes, it works with both, just wanted to extend your knowledge... ;-)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • downtapdowntap Member Posts: 18
    Did not know that. Good to know!
  • tinoruijstinoruijs Member Posts: 1,226
    Good post! Thanks!

    Tino Ruijs
    Microsoft Dynamics NAV specialist
Sign In or Register to comment.