January 31, 2019

Batch file script - Get date-time value in string variable

Batch file is an important tool to automate different tasks, e.g. Copying files/folders, Archiving, Taking backups, Running builds and lot more... Often times we need to create date-time stamp in a string variable for different purposes. For example, I want to write a batch script which will copy a file to a destination folder, but before copying in destination folder I want it to append the date time stamp in target file name. In this post we will see how to pepare date time stamp as string variable in batch file. Here is the script.


::----------------------------------------------------------------------------------------------
::prepare date-string
::----------------------------------------------------------------------------------------------

:: Get day from current date, in variable name 'day'
SET day=%date:~7,2%

:: Get month from current date, in variable name 'month'
SET month=%date:~4,2%

:: Get year from current date, in variable name 'year'
SET year=%date:~10,4%

:: Set year+month+date for date-string, in variable name 'myDateFormat' 
SET myDateFormat=%day%-%month%-%year%

:: Trim myDateFormat to remove white spaces
SET myDateFormat=%myDateFormat: =%

::----------------------------------------------------------------------------------------------
::prepare time-string
::----------------------------------------------------------------------------------------------

:: Get hour from current time, in variable name 'hour'
SET hour=%time:~0,2%

:: Get minute from current time, in variable name 'minute'
SET minute=%time:~3,2%

:: Get second from current time, in variable name 'sec'
SET sec=%time:~6,2% 

:: Set hour+minute+second for time-string, in variable name 'myTimeFormat'
SET myTimeFormat=%hour%-%minute%-%sec%

:: Trim myTimeFormat to remove white spaces
SET myTimeFormat=%myTimeFormat: =%

::----------------------------------------------------------------------------------------------
::set targetFileName with string literal, and also append the date and time strings at the end.
::----------------------------------------------------------------------------------------------

SET targetFileName=CRM System Log-%myDateFormat%-%myTimeFormat%.txt

::copy file, using the variable targetFileName 
copy "C:\Test\CRM System Log.txt" "C:\Test\Destination\%targetFileName%"

I hope this helps some of you who get stuck with a similar problem.

January 24, 2019

WCF - Set nametable character count quota

If you are receiving large size of string from WCF service, you may encounter maximum quota exceeding error.

The maximum nametable character count quota (16384) has been exceeded while reading XML data. The nametable is a data structure used to store strings encountered 
during XML processing – long XML documents with non-repeating element names, attribute names and attribute values may trigger this quota. This quota may be increased by changing 
the MaxNameTableCharCount property on the XmlDictionaryReaderQuotas object used when creating the XML reader. 

To fix this issue you have to change readerQuotas settings in the binding tag.

<binding> 
 <readerQuotas 
        maxDepth="2147483647" 
        maxStringContentLength="2147483647"
        maxArrayLength="2147483647" 
        maxBytesPerRead="4096"
        maxNameTableCharCount="2147483647"
        maxStringContentLength="2147483647"
  />
</binding>

The readerQuota settings are used to limit the bindings. If a request exceeds any of these limits the WCF service will automatically reject the request, to help prevent Denial-of-Service (DoS) attacks.

The readQuota limits can be set on both server and client. This also allows clients to be protected against forged service responses.

I hope this helps some of you who get stuck with a similar problem.

January 13, 2019

WCF Test Client - How to increase MaxReceivedMessageSize

The WCF Test Client shipped with Visual Studio, is useful tool to quickly test and check results for a WCF Service call. However I encounter a scenario where my service call is returning larger result-set that is exceeding the default message size of 65536, and it starts giving me this error message.

The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element.

In the last post Increase message size quota for incoming messages, we have seen how to increase the message size from client application's config file. But you might facing problem in searching for a way to configure message size for WCF Test Client when calling a service method. There is an option available to configure message size from WCF Test Client, but it seems to be slightly hidden, because this is not at very prominent place where user can find it easily.

After you Add Service in WCF Test Client, you can find there is a Config File node at the end of the service tree. Right click on Config File node, and select Edit with ScvConfigEditor from the context menu.

Edit with ScvConfigEditor

You will get the configuration editor dialog, just go to the Bindings node, expand it, and select the required binding settings. Here you can change the MaxReceivedMessageSize or any other settings you may need.

Edit with ScvConfigEditor Bidings