Skip to main content

Microsoft Dynamics NAV - Get File Count from a Folder using Dotnet.

Hi All,

Today I want to discuss a code re-write that I had to do on a recent project about file handling.

Scenario -
PDF will be saved in a shared path whenever a purchase invoice is printed. To Generate File Name we were using Purchase Invoice No. and then extending that with an integer.

So if Purchase Invoice (PRO001) Report will be printed for the fifth time, the shared path will already have following files -

PRO001-1.pdf
PRO001-2.pdf
PRO001-3.pdf
PRO001-4.pdf

And Next file name should be PRO001-5.pdf.

In Pre 2009 R2 we don't have Dotnet DataType and the code that is written using File Data Type.

The Code is filtering-
1. The Shared Path Passed as Var to the function.
2. Set the type of files of Type is a FILE.
3. Then filter the File Name with the filter passed (in our case it will be passed as PRO001-%)


Then It counts the Files in the Folder.

But in my case, it was having Performance issue as the folder size is huge and filtering and then count was taking somewhere 03-05 minutes. To Enhance that code we tried to look into re-write it using DOTNET and the performance was WOW!

Here is what we did with Dotnet to get the file count using DOTNET Class. 

The Code is -
1. Generates a Filter String with the File Name filter passed.
    ** Just a reminder for DOTNET we don't require @ to filter as it's already case insensitive.

2. The Call the function inside Directory calls to get files from - Folder with Filter and Search Options set as to filter only files in current directory.


Here are the Dotnet Variables Used - 
Directory   - System.IO.Directory.'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

Search Options - System.IO.SearchOption.'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'


Additionally, if you require counting all files in folder and sub-folders then we just need to change the SearchOptions enumerations to AllDirectories as commented in above code.

Share your thoughts about it. I have so many rewrites where I use Dotnet to get performance or only option to rewrite in new versions. If you have a scenario that we can discuss in future articles please put also that as the comment.

If you want to use the code, please download an object from the on-drive. 
File Name - Microsoft Dynamics NAV - #SauravNav_Dotnet_File_Count_Codeunit_50000

Please share your views. Happy to hear all of them !! Stay connected will be back soon.

Regards,
Saurav Dhyani
www.sauravdhyani.com

Comments

Popular posts from this blog

VIEW SERVER STATE permission on SQL Server?

Hi all, Sometime While trying to Login into a database we face an error message as shown below. --------------------------- Microsoft Dynamics NAV Classic --------------------------- You cannot start Microsoft Dynamics NAV Classic because you do not have the VIEW SERVER STATE permission on SQL Server. Contact your system administrator. --------------------------- OK    ---------------------------

RTC Report It is not possible to instantiate the Visual Studio bridge.

Hi all, As a Navision developers I have Multiple Versions of Navision running in single Machine. As discussed Earlier in the post how to run multiple Version of RTC in single machine. So my machine have following details for RTC Versions - NAV 2009 R2    - is installed. NAV 2009 SP1  - is copied at C:\Program Files (x86)\Microsoft Dynamics NAV\60\NAV 6.0 SP1 IN\ NAV 2009         - is copied at C:\Program Files (x86)\Microsoft Dynamics NAV\60\NAV 6.0 IN\ This approach has been working great for execution of Classic and RTC Clients. However, after installing Dynamics NAV 2009 R2, if i tried to view the Layout for an NAV 2009 SP1 Report i was getting the following error: ---------------------------  Microsoft Dynamics NAV Classic  ---------------------------  It is not possible to instantiate  the Visual Studio bridge.  ---------------------------  OK    --------------------------- After searchi...

NAV 2013 R2 - Cumulative update 12 Released.

Hi all, Please find below the details of  Cumulative Update 12 released for Microsoft Dynamics NAV 2013 R2. Title - Cumulative Update 12 for Microsoft Dynamics NAV 2013 R2 Build No. - 38053 Release Date - October, 2014 Local Version Included - AU, AT, BE, CH, DE, DK, ES, FI, FR, IS, IT, NA, NL, NO, NZ, SE, UK, RU Download Link Note: Implementing this cumulative update will require a database conversion unless you have already implemented update rollup 5.