Posted in C#, Dotnet, General, Tools

Copy files from latest modified folders – Batch script

Hello Reader,

Part of a tool making process, i had a sub task, where i need to copy certain files from a few (in this case 3 in numbers) directories from recently modified directories (date-time descending sort) onto another path where again a latest single directory needs to be searched, under which the predefined files are copied to.

In other words and detailed: A build machine runs its scheduled job, creates few files (if failure) in a 3 different folders: components/modules wise, the names are not in fixed structure. This central repository shall have daily 3 folders added into it.

Hence, i was in need of a batch script where it gets the latest (descending sorted) modified 3 folders and inside each of them, search for few files (pre-defined), when found, does a blind copy onto a destination path: where again with a date-time descending sort, latest folder is found (just 1); which i consider as Output folder of all logs.

Later on, another tool looks for these few pre-defined files to determine the failure condition of the build and generates few reports. WIth few hours of looking all over google, i found very minimal solution available nor suitable as per my needs and then reading quite a good documents of windows commands and understanding it, i could come up with below script.


Code description:

Line 4, is required for the below FOR loop where in we need to retain the state of the variables and update it all through the loop.

Line 12, is the loop where in i am using /f argument to iterate over the out of the command executed in the loop header, in this case its DIR command. In this case, i need only the Folders which are non-hidden and non-system folders needs to be sorted in descending order based on modified time. Hence the argument /ad-h-s /TW /O-d. More details can be found in the help document of Dir command.
Line 12, loop shall first find out the single latest directory in the destination path where a files needs to be copied out.

Line 19, loop sorts the source directory contents (folders only) in descending order based on modified datetime.  Then picks up first 3 top directories from it and copies files (if present) inside these 3 folders to destination directory found from loop in Line 12.
Since the state of the loopcount is retained via delayedexpansion concept, the accessing of the variable is done with !<variable>! than the normal %<variable>% way at Line 21. If done via traditional way, then the variable value when accessed wont be having the latest value in it.

Cheers! Zen

Posted in C#, Dotnet, Tools

Windows cannot find devenv error – Win 7

Dear Reader,

Today on my Win 7 (VM install guest on Zenwalk Linux) VS 2012 RC expired. Hence i had to uninstall it. Unfortunately after it uninstalled, it did not clean up all the paths and links and corrected the changed paths of some too, in this case devenv command path (Beta build yea!). Though i restarted the VM twice, still the problem persisted. Hence i could not launch VS 2010 from Start -> Run or even from command prompt.

After googling for a while, i did end up with nothing apparently. So searching to fix this issue had to fiddle with the stupid windows registry.

So if you also face the same issue, here is the path for it:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\devenv.exe\

Short and Hope it helps.

Happy Coding,

Posted in C#, Dotnet, Tools

Disabling Reserved Checkout option in IBM Clearcase

Dear All,

Tired of making sure you have not done any RESERVED checked out in your development branch whenever you wish to check out a file every time? Or you have always got irritated to uncheck the option (manually) Reserved in the Clear case Checkout dialog whenever you wish to check out a file especially when all your team members share same branch for development.

Do not worry, you can permanently disable automatic reserved check out in the clear case plug-in of Visual studio as shown in the below screen shot. Just uncheck Reserved option as highlighted in the below screen.

Steps to do:

  1. Go to Visual studio -> Tools -> Options
  2. Select Rational Clear Case -> General (As shown below)
  3. Click Clearcase User options button as shown below.

Hope it helps.

Thanks & Happy Coding:)

Posted in C#, CodeProject, Dotnet, Tools

BoxCop – Static Assembly Analyzer tool

Dear Reader,

In this article I am going to explain about a small tool which I have developed as my personal project for a couple of weekends in between my busy schedules. This small tool, which I like to call it as BoxCop is basically a static analyzer just like FxCop. I conceived this idea when I was working with FxCop for a quite some time at work. I would not even claim that this tool is any where similar to FxCop in terms of supported feature.

A Bit History:

Basically I was working with FxCop for a week or so at work place because I am supposed to fix all the FxCop violations in our production code. While doing so, I found many codes which were doing a lot of boxing operations (confirmed by looking into IL most of the time). We already know that this operation is really a big pain if used extensively. So at that time I started to look if FxCop does support this rule so that we can refactor it so that boxing is minimized. But I did not find any, may be fxcop purpose is not for this? No idea.

You might have noticed from my past blog articles that I do spend a lot of time looking at IL for learning purposes. So I was doing the same and I got the idea of developing this small (tiny) tool which could save at least as small as 1% of a programmers/reviewers time.

Please download either of below packages:

Source code : BoxCop (Source code) – DotNET 3.5

Binaries: BoxCop DotNet 3.5 Binaries

Note: The source code package has been uploaded in .doc format due to wordpress issues. Please save it as .zip package or change the extension from .doc to .zip of the above files when downloaded.


I started to think of developing a small tool (tiny you may call) which could help me to find my mistakes itself in my code in the first place. So as said already boxing is one of those mistakes (not always though) I might do in my code.

You might argue now that boxing is not always harm. Yes it’s not, if you use it wisely. Many times it so happens that we programmer due to many reasons write code urgently and finish off the task but fail to see if what we did is really worthy. You might think that these flaws are easily caught when you ask your code to be reviewed by some body. But not always effective I would say. Because your reviewer is not the person who has written the code in the first place, the reviewer may try to understand your code as much as possible, but due to his/her reasons some things may be overlooked.

It’s always pain to review an old code which your suppose to do refactoring on it because it’s not documented properly? The original programmer is no more available? ,etc. But you got to do it.

Any ways, I do agree that boxing is not the only reason for refactoring a code. But it could be one of those pitfalls you might look for.

How it works:

The application analyses the assembly which is given as input and its associated PDB file in searching for boxing instructions in IL as well as related source code information in the PDB file. This tool searched for all the methods defined in each module or types in an assembly and for each of those methods collected, its equivalent pdb information is fetched. Once both of the data is collected, it aggregates the result as per required format which is displayed over UI.

 Let me show you few screenshots of the application:

The above screenshot shows the basic UI window of this tool. First the user has to select an assembly by clicking the browse button.

Once the .NET assembly (exe or dll) is selected, the selected file path is shown on the UI. Hovering mouse on the path shows the complete path (if path is too long) as shown in the above image.

Once assembly is selected, you need to then press analyze button. Once it is clicked, the tool analyzes the assemblies for boxing instructions. The tool automatically tries to look for the pdb information for the selected assembly in the same location internally. In case of pdb is missing, the tool reports an error as shown:

Upon selecting correct assembly with valid pdb information, the tool successfully analyses the assembly for all the boxing instructions for each method in each module and type in an assembly. Once analyses are done, the UI list view displays all the information needed as shown:


As you can see from the above image, the list view component on the UI displays detailed information about boxing instructions usage in your C# code.

The first column simply displays a serial number, second displays Namespace.Class information, third column displays Method name and the last column displays information about source code path and line, column information appended to it.

You can export the above UI output to a csv format by right clicking on the listview component as shown in below image and selecting Export to CSV menu item:


Upon selecting the context menu, a save file dialog is displayed as shown:

When Save is clicked, the report is exported and a success message box is displayed to the user.

In case of any exceptions or crashes generated by this application, a log is created with file name  BoxCop.log present in the same location as of this application.

As of now, it supports only target assemblies built for 32 bit .NET 3.5 or earlier frameworks. Any non compatible assemblies selected, the tools displays an error as shown below:

Before using this tool, please make sure following steps are taken care off:

  • .NET 3.5 framework is installed but not 3.5 client profile
  • All dependency assemblies for the targeted assemblies present in the same directory as the targeted assembly.
  • All pdb files are present in the same directory as the assemblies and are updated.

Internal Details:

This section explains in detail about the code parts which this tool uses internally. So if you are non-techie, it may no more interest you.

This tool has 3 parts in it:

  1. BoxCop
  2. ILCodeAnalyzerLib
  3. PDBDataReader

BoxCop: This module is responsible for all the UI components associated with this tool.

This module has following components in it:

  • BoxCopForm: UI form which is only responsible for displaying UI components and handling related events from the components.
  • AnalyzeAssembly: This component is responsible for invoking all related methods for analyzing assemblies and getting pdb information from related modules.
  • AnalyzedData: This component is just a helper component to hold all the collected data which is used to display in the UI.

ILCodeAnalyzerLib: This module is responsible for analyzing, reading all the IL instructions from the assembly.

This module has following components:

  • AssemblyLoader: This component just loads the selected .NET assembly into memory via reflection. It also checks for availability of the pdb file associated with this assemblyThe above method loads the assembly into memory. If there is any problem related to it, an error is sent back. It also checks for pdb information in the same location as of assembly.
  • AssemblyReader: This component just finds all the methods available in the assembly modules and types. Once it is collected a list is built to hold all the information for further analysis.The above code gets all the method in an assembly module and type wise. Once all methods are found, it builds up a MethodInfo list having all the details about a method. This list is later used to get further information about each method.
  • ILInstructionFormat: This component is just a helper class which just holds all the required format of IL instructions.
  • MethodBodyAnalyzer: As the name suggests, this component just analyzes all the method body having MSIL instructions. It reads the method body code as a byte array and then converts the byte array to an appropriate IL instruction sets.The above method reads and analyzes the IL byte array. Each opcode which is in byte value is identified from a standard dictionary of opcodes value based on singlebyte or double byte. Single byte instructions are like nop, break, ldarg, etc. where as double byte instructions are ceq, stloc, etc.
  • OpCodesBuilder: This component is a singleton class which exposes 2 dictionary which holds single byte and double byte MSIL

PDBDataReader: This module is responsible for reading the program debug data base file (.pdb) of an assembly. It uses OLE32 native calls to read this file.

Following are the components in this module:

  • PDBReader: This component is responsible for invoking ole32 native calls to fetch the source information from pdb file for specific method. This component collects all the data viz. Source Lines, Columns, End lines, Source code location, etc.
  • NativePDBReaderWrapper: This component wraps all the native calls to ole32.dll. The native calls are used to get all the source information which is stored in the custom format file called .pdb (Program Database).

Hope you liked it. Your comments/votes are much appreciated.

Happy Coding. Thanks.

Following are few activities which I will be carrying it out in next couple of days if possible:

To Do:

  • Improve design of the classes where ever necessary.
  • Add features if needed and if feasible
  • Make it bug free😉
  • Central repositories of error strings either in app.config or resource.
  • Unit test cases, although i have done few system tests to test the tool. Its working fine for all production code.
  • Compiler warnings and FxCop fixes if any.

Feel free to:

  • Submit bug via blog comments or email. Do not forget to provide as much details as possible.
  • Improving the design/code. Kindly, keep me updated on the same.
  • Fixing bug yourself. Again keep me updated.
  • Any better ways to do? Add a comment.


Thanks to the following article authors which helped me alot in building this tool.