About Me

Tuesday, November 29, 2011

Batch file to delete files older than N days

Most of us remember times when we used MS-DOS commands, Norton Commander and staff like that.
Experience from that period of time is still usefull. From time to time i have to create bat files that does some boring things for me.
Its great to find something new in that area.
Today, thankfully to  guys from the stackoverflow, I found really easy way how to delete files older then N days.
I never know about forfiles command, its really awesome :

forfiles /P c:\DBbackup\CT\LogShipping\ /S /M *.* /D -1 /C "cmd /c del @path"

That simple command deletes (cmd /c del @path) all files (*.*) from a path (c:\DBbackup\CT\LogShipping\) that older than one day (/D -1)

In case if you want to remind a list of available DOS commands and find ones you never know, read a list of  "An A-Z Index of the Windows CMD command line"

P.S. In case if you get errors like "Invalid argument/option - '@path'" try to remove double quotes around a path if you have any.


Monday, November 28, 2011

How to use LINQ with DataTable

LINQ came into .NET developers life and at present moment I can't imagine my life without it. Actually, I can imagine, but it will be horrible.

Few days ago I had to modify some old project that has ADO.NET code in it.
Unfortunately we still have to work with all that datasets and datatables from time to time.
However, we can make life easier if start to use LINQ for operating with that. Its really easy.

First of all you need to add a reference to System.Data.DatasetExtensions library from a project where you going to use LINQ.
That library, as you may see from the name, contains some extension methods that allows to write LINQ queries directly on DataTables.

public DeviceFamilyInfo Get(int deviceFamilyId)
{
 DataSet rawFamilyInfo = DBMethods.GetDeviceFamily(deviceFamilyId);
 EnumerableRowCollection tblDevices = rawFamilyInfo.Tables[1].AsEnumerable();

 List>DeviceInfo> devices = GetDevicesList(tblDevices);

 return rawFamilyInfo.Tables[0].AsEnumerable()
  .Select(f =< 
   new DeviceFamilyInfo(devices)
    {
     Id = (int) f["Id"], 
     FamilyName = (string) f["Name"]
    }).First();
}

As you may see, the secret in converting datatable into IEnumerable by calling of AsEnumerable() method from the referenced library.
After that conversion, you can do with your data whatever you want. In my case, i just wrote a simple mapper that converts a DataTable into  instance of DeviceFamilyInfo class

Convert datatable into entity with BLToolkit

BLToolkit is a very good library that can make life easier when working with ADO.NET.
I used it for one project as a mapper that converts a data from a database into objects.

Normally, you have to pass a procedure name to the method of the BLToolkit library with params, specify what type you need to return and that's all.
However, few days ago, i needed more complicated thing.
Instead of making BLToolkit to do an SQL request, i did it by myself and get the DataTable.
It was needed to get some specific data from a DataTable that can't be converted into the existing type.
After that I expected that I can convert the result DataTable into list of Entities

Fortunately BLTollkit is able not only to make a conversion of  SQL requests on the fly, but has methods that allows to pass a DataTable into it, specify a desired type and get a result collection.

public IList<IScoutProfileInfo> SearchScouts(string toSearch, out int totalItems)
{
 totalItems = 0;
 using (DbManager db = new DbManager())
 {
  DataTable dataTable = db.SetSpCommand("usp_ScoutProfile_Search", toSearch).ExecuteDataTable();
  var enumerableRowCollection = dataTable.AsEnumerable();

  if (enumerableRowCollection.Any())
  {
   totalItems = (int) enumerableRowCollection.First()["TotalItems"];
  }

  MappingSchema schema = new MappingSchema();
  return schema.MapDataTableToList<ScoutProfileInfo>(dataTable).OfType<IScoutProfileInfo<().ToList();
 }
}

As you may see, the MappingSchema.MapDataTableToList does the trick.