Tuesday, January 8, 2008

Batch program to copy file with Date & Time - Archiving

Recently was looking for a simple batch file to archive some of my file with date and time stamp added to it. Compiled a simple script to do the job and it looks like:

d:

Set Source="D:\mySourcePath"
Set Target="D:\myTargetPath"
Set Date=%date:~10,4%%date:~4,2%%date:~7,2%
Set Time=%time:~0,2%%time:~3,2%

REM myFile.log
copy /y %Source%\myFile.log %Target%\myFile_%Date%_%Time%.log
Del /q %Source%\myFile.log

Love coding!

Friday, January 4, 2008

Serialize objects using SoapFormatter

A while ago we’ve seen how to serialize and deserialize objects using XMLSerializer. Somebody asked recently so thought of showing how to do it as SoapFormatter. Here’s the code and you get the idea. One thing to remember is to add reference to Soap Formatters from System.Runtime.Serialization.Formatters.Soap component.

public static string SerializeObjectSoapFormatter(Object objToSerialize)

{

string serialXML = "";

SoapFormatter soapFormatter = new SoapFormatter();

MemoryStream mStream = new MemoryStream();

soapFormatter.Serialize(mStream, objToSerialize);

byte[] buffer = mStream.GetBuffer();

mStream.Close();

UTF8Encoding encoding = new UTF8Encoding();

serialXML = encoding.GetString(buffer);

return serialXML;

}

Love coding!