Saturday, January 23, 2016

First steps to write batch files in windows



I'm sure that nearly every windows user have seen files with extension .bat . What are the they, What do they do, How can we make them.

They are actually one type of files that exist in windows operating system and those files can be used to get some work done by OS.

Lets see how we can create those files with simple example.

@echo off
title My First Batch File
echo Hello!
pause


  • Don't worry about the first line @echo off (If you want check out more details from here)
  • title My First Batch File this line gives a title to our batch file
  • echo Hello! line print the word "hello!"
  • pause this will pause the program for user inputs
Now save this file with any name you want with .bat extension and double click on the created batch file to execute what you programmed.

Note:Make sure when you save set Save as type to "All types".


Friday, January 22, 2016

Split string by another string in C#

Normally developers use String.Split method to split a string by a character. An Overloaded version of same method can be used to split a string by string in C#.


string data = "THExxQUICKxxBROWNxxFOX"; 
return data.Split(new string[] { "xx" }, StringSplitOptions.None);

  • A string array should be passed as first parameter. 
  • In second parameter we can specify whether the substrings include empty array elements. etc
In this example string "xx" is passed but multiple string values can be passed as we want

string value = "The handsome, energetic, young dog was playing with his smaller, more lethargic litter mate."; 
string[] separators = {",", ".", "!", "?", ";", ":", " "}; 
string[] words = value.Split(separators, StringSplitOptions.RemoveEmptyEntries); 
 foreach (var word in words) 
  Console.WriteLine(word);

more details can be found here.

Thursday, January 21, 2016

Best method to extract text from PDF in asp.net C#


There are many ways to extract text from PDF.
  • Microsoft IFilter interface and Adobe IFilter implementation
  • iTextSharp
  • PDFBox
Among those solutions using PDFBox is the best according to my experiences because,
  • Using Adobe IFilter implementation need to have lot of other things set up in running machine like,
    • Windows 2000 or later
    • Adobe Acrobat or Reader 7.0.5+
    • IFilter COM wrapper class
  • iTextSharp is opensource but can't be used in commercial products.
So the best solution is using PDFBox. It doesn't need other things installed on your machine.

PDFBox is a java PDF library and there is a .net version of PDFBox (It can be downloaded from here


Here is an example how to use PDFBox


using org.apache.pdfbox.pdmodel;
using org.apache.pdfbox.util;

// ...

private static string ExtractTextFromPdf(string path)
{
  PDDocument doc = null;
  try {
    doc = PDDocument.load(path)
    PDFTextStripper stripper = new PDFTextStripper();
    return stripper.getText(doc);
  }
  finally {
    if (doc != null) {
      doc.close();
    }
  }
}  

NOTE:
  1. Following dll files of PDFBox library should be added to bin file of your project
    • commons-logging.dll
    • fontbox-1.8.9.dll
    • IKVM.OpenJDK.Text.dll
    • IKVM.OpenJDK.Util.dll
    • IKVM.Runtime.dll
  2. Reference for following dll s should be added
    • IKVM.OpenJDK.Core.dll
    • IKVM.OpenJDK.SwingAWT.dll
    • pdfbox-1.8.9.dll

Deleting Files in ASP.NET C#

Simplest way to delete files in asp.net C#

if ((System.IO.File.Exists(fileName)))
                {
                    System.IO.File.Delete(fileName);
}


This is a simple code snippet in asp.net that can be used to delete files from any directory.
In here variable "fileName" is the specific path to the file.

Ex: If want to delete a file called "hello.pdf" in "upload" folder which is in my C drive, file name should be full path to that file "C:\upload\hello.pdf"


Monday, January 11, 2016

What is Windows Power Shell & how it differs from CMD

Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework.
If it sounds complicated you can think it as same as the CMD prompt but with more advance tools.
PowerShell is a much more powerful command-line shell and scripting language than the Command Prompt is. It gives Windows system administrators a useful command-line environment.

There are some fabulous things that can be done using Power Shell
  • perform your favorite CMD tasks
  • Kill processes in power shell instead of using task manager
  • Export NTFS file permossions
  • Play with power sell 2.0
  • Background a time consuming task
  • Report all of the USB devices installed
( Read more about these tasks)

There are few main differences between cmd prompt and power shell
  • Power sell uses a different set of commands called cmdlets.Many system administration tasks are exposed via Power Shell cmdlets
  • PowerShell makes use of pipes, just as Linux and other Unix-like systems do. 
  • You can create complex scripts for managing Windows systems much more easily than you could with the Command Prompt.
So here is why people use Power Sell over cmd prompt
  • Cmd prompt is limited, can’t access many Windows system administration features.
  • It is is more difficult to compose complex scripts with 
PowerShell is a new environment for Windows system administrators that allows them to use a more modern command-line environment to manage Windows.

Saturday, December 5, 2015

How to use google fonts in your html

When we creating a web page one of the main problem we face is using fonts we want in our web page. That is not easy because in order to display the correct font in viewer's browser that font should be installed in that person's device.
Otherwise our web page will not be displayed properly in viewer's device.
Using google fonts is the best solution for that.

How to use Google fonts

  1. Type the name of font that you need and find it (In example below finding "Bree Serif" font is shown)
  2. Change the appearance of your font (bold, italic etc)
  3. Click add to collection.
  4. After adding all fonts you want by following step 1 to 3 repeatedly click on use button.
  5. In the next interface scroll down a bit and find the CSS link for your fonts and include it in your html as a style reference (It will render those fonts from that link)


Now you can use font you wanted as <FONT FACE="Bree Serif">Bree Serif font</FONT>
Because of Google fonts you don't need to worry about viewer's installed font styles. You can use what ever font you need and make an attractive web pages.

Feel free to add a comment if something was not clear to you or you have something to add.

Thursday, December 3, 2015

How to use a long URL using bit.ly URL shortner

Some times we have to deal with very long URLs and that is very annoying. Here is a simple solution foe that. This not only shorten your URL but provide many other supports such as keep track of number of clicks on the links.

Here you go.
You can download this cool video by this link

Optimize you working enviorenment : Single command to create & move to a directory in linux (C Shell, Bash)

Usually move to a directory just after creating is bit of a anxious task specially if the directory name is too long. mkdir long-name-of...