Ehsanul Haque

Welcome to my personal site & blog

22 June
0Comments

How to delete a locked file in Windows?

Sometimes when you try to delete a file on Windows it says file cannot be delete as it is used by another program; you must close the program to delete the file. Well, almost all the time you actually know which program to close in order to delete the file (also rename the file). But today when I tried to delete a file (which was not open in any program nor did I use this file for over three months) Windows was showing the message. You cannot really say which program or process is using it. So I downloaded Process Explorer from Microsoft’s site which you can use to see which process or program is using the file you are trying to delete.

After you run the Process Explorer go to “Find” menu and click on “Find Handle or DLL …” (Ctrl+F). Type in the file name (full or partial) you are having trouble deleting or renaming and hit Search. Process Explorer will list all the processes or programs using this file. When I searched for the file I was having problem with it showed that the “Explorer” is using it! So what can I do in that case?

From “Task Manager” if I kill the “Explorer” process I cannot browse to the folder so using “Task Manager” I ran “Command Prompt” and from command line I changed directory to the desired location. Typed the command to delete the file “del filename.ext”. I was able to delete the file and then using “Task Manager” I started “Explorer” again. It was easy but “Process Manager” made it easy to find the process. So many hidden stuff that we have on Windows!

13 March
1Comment

“sed”: Find and Replace Command on Linux Command Line

Sometimes we use IDE to find and replace strings in our file(s). IDEs have features to search for a keyword in all the files in a directory and replace with a given string. When you’re on Linux and using command line you can easily do this using the command “sed”.

According to the linux manual (man sed) “sed” is a stream editor which is used to perform basic text transformations on an input stream. With this command you can efficiently find and replace text in one or multiple files or you can keep the original file and make a new file with the replacement text.

To find and replace text in the original file the command is:

sed -i 's/find/replace/g' original.php

In the command above, “-i” option instructs to edit file(s) in place. “s” command attempts to match the pattern space against the supplied “find” string (which is a regular expression pattern). If the match is successful, then that portion of the pattern space which was matched is replaced with “replace” text. “g” is used to specify a global change, i.e. the string will be changed everywhere in the file(s) where it finds them. As mentioned earlier the “find” string is basically a regular expression pattern, and the replacement may contain special character (&) or escapes (\1 through \9) to refer to the corresponding matching sub-expressions in the “find” regular expression.

To find text in a file and output to a different file with the replacement the command is:

sed 's/find/replace/g' original.php > replaced.php

In the last command if you do not specify the output filename, it will simply show the file content on the screen with replaced string. There’s another command (similar to “sed”) which can be used for the same purpose is from perl.

perl -pi -e 's/find/replace/g' original.php

This command will find the word “find” and replace with the word “replace” in original.php. For this command “-p” option assume a loop like “while (<>) { …. }” (which is similar to the option -n but it prints the lines as well). The following option “i” performs the same task as it does for “sed”. Then “-e” says anything after it is a online of program. According to the manual several “-e program” can be used.

15 May
2Comments

Setup sub-domain on the localhost

When I work on my local machine on a website, I usually setup the virtual host on a sub-domain setup on the localhost. I feel comfortable using a sub-domain rather than setting up an Alias. I’ve seen many people know about it, but there are many who don’t, and this is for them.

So what do you have to do to setup a sub-domain on the localhost? It is not a rocket science but one must know where to do it, especially on a Windows box.

On Windows OS browse to {Drive on which your Windows OS is installed}:/{WINDOWS/WINNT}/system32/drivers/etc/. Open “hosts” file into a text editor, and you will see something similar to this:

# Copyright (c) 1993-1999 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

127.0.0.1       localhost

The last line represents the hop any request to http://localhost/ will go through. The IP 127.0.0.1 is used (usually) for you localhost, and going to http://localhost/ will access the files under the root of directory setup for it. Now if you want to setup a sub-domain called “myself”, i.e. http://myself.localhost/, then you add the following line at the end of the file.

127.0.0.1       myself.localhost

On a Linux box, “hosts” file is located under /etc/. You will require “root” access to edit this file or the user access who has priviledge to edit this file. Similar to Windows system, add the following line into the file:

127.0.0.1       myself.localhost

You don’t have to restart your web server for this change to take effect. But you will have to setup the virtual host to point to this sub-domain, which will require you to restart the web server.

Thank You.

15 May
3Comments

Redirect with .htaccess file

Ever wanted to redirect users from one domain to other or one sub-domain to other without compromising the the file path or query string variables? I’ve come across the requirement when I published my WordPress blog.

For testing, I initially installed WordPress on a testing sub-domain, where I imported all the blog entries and added blog-roll links. After doing so, PHPClasses.org picked up my entries for Grab Yahoo, LinkMeIn and MySpace profile updater as track-back links. Later when I switched to my main sub-domain, I’d to find a way to keep the track-back links as is, and redirect users to the proper locations. The trick was done by .htaccess file.

After reading the apache’s documentation on mod_rewrite module, I’ve successfully implemented it. If you want people accessing http://sub1.yourdomain.com/2/12/some-post-of-yours/ to go to http://sub2.yourdomain.com/2/12/some-post-of-yours/ then in the root of sub1 (first sub-domain) create a .htaccess file and add the following lines:

RewriteEngine On
RewriteRule (.*) http://sub2.yourdomain.com/$1

I have not read much and not sure if there’s any other option to do this more efficiently; but this trick has served my purpose.

24 August
4Comments

New functionality for Grab Yahoo

I have added a new functionality for the Grab Yahoo class. Soon after I released the patch, I figured out that I could add this new functionality which did not take me much time to implement. This new feature will allow you to grab your Yahoo! Calendar in an array format.

Using Yahoo’s calendar export feature, I grab the CSV file and parse it to form an array. This was a very simple addition as it uses same method as Address Book parsing. Only thing that was different was the way I had to parse the CSV, which Yahoo creates for Outlook. I don’t have any further plan to upgrade this class but do have other plans to build similar web service based classes.

Please download the latest version of grab yahoo class (v.1.3) here.


Thank you.