touch with DOS commands

March 20th, 2007

I often use Cygwin to get access to a few linux style commands that I need. The command touch is one of those commands. In case your not familiar with it it will create/update the creation and modification times of a file to the current time. If the file specified does not exist it will also create an empty file.

The problem I recently encountered though was I needed the touch functionality but on a computer that did not have Cygwin installed. Of course I could have just put together a quick little C# or Java program to do the trick but I still would have to install the app onto this other computer.

The solution to this delimia was to use the following dos command which will create an empty file:
copy /y nul file.txt

Count number of lines in a file using DOS

February 25th, 2007

Every once in a while I get on a computer and I need to count the number of lines in a file. My first instinct is to open my text editor (editplus) and hit ctrl+end to get to the bottom of the document. Then I can view the status bar which will tell me the line number. This works fine when I am on my computer but not when I am on another computer that does not have editplus installed.

My next option might be to open this file in notepad and do the exact same thing. This will work fine if the file is not to large. The problem is I often deal with very large files. I need a quicker way to produce the same results.

This is where DOS comes into play. I can use the following command and let DOS quickly tell me the number of lines in the file.

findstr /R /N "^" file.txt

This command will output every line with a line number in front of it but will still take a long time given a very large file. The solution to this is to take this command one step further.

findstr /R /N "^" file.txt | find /C ":"

Now the output will only be the number of lines that are contained in the file.Again, this command could be taken a step further to tell you how many lines are in the file that contain a certain string.

findstr /R /N "^.*certainString.*$" file.txt | find /c ":"

I'm sure there are many other great uses for find and findstr. If you have found one please post comment.

Regular Expression Search and Replace via Command Line (Editplus)

December 14th, 2006

Editplus is definitely my favorite text editor but it does not entirely support Regular Expression. Below is an example of when Editplus has failed me:

Editplus-regex

So, I used the regular expression .*<table.*\n.*<tr>\n.*<td>. This unexpectedly returned everything from the beginning of the file down to the <td>. Basically it seems to have a little bit of trouble with multi-line regular expressions.

My solution to this problem was to develop a little regular expression command line program and add it as an Editplus user tool. An added benefit to creating this application is I can now do Regular Expression search and replace via the command line. At the time of creating this application I did not know about Powershell as that would have provided the same functionality. But this little application works so I will stick with it for now.

So, how does the search and replace program work? If you type SearchReplace.exe at the command line you will get the following:

Usage: searchReplace.exe "regular expression" "replacement string" file.txt

Pretty simple ehh?  Note: if you happen to have double quotes in your regular expression they will need to be escaped.

Now to set this up with Editplus just go to Tool —> Preferences —> User Tools and put in the following values:

Command: SearchReplace.exe
Argument: "$(Prompt)" "$(Prompt)" $(FileName)
Initial: $(FileDir)
Check: Run as text filter

When this user tool is run the first pop up box will be where you put the regular expression and the second will be the replacement string.

At this point I have to give a little warning that I have not thoroughly tested this program and I am not liable for any data loss caused by this program. If you happen to encounter any problems let me know. 

Determine File Encoding

December 14th, 2006

I ran into a problem where I wasn’t sure if I could trust the encoding that my text editor was displaying. I also wanted to check the encoding on about 10,000 files and determine if it was not a specific encoding. So, I created a little program to tell me the encoding of a file. This in combination with powershell allowed me to loop recursively through all the files in a directory and create a list of the files that were not the correct encoding.

A Good portion of the code that I used for this application was available on the another site that describes how to determine the Encoding of a Unicode file.

The application can be downloaded below:

DOWNLOAD: Determine File Encoding (583 Downloads)

Mute on the Command Line

December 14th, 2006

I found a nice little utility to mute computer speakers at the following site:

http://magic.aladdin.cs.cmu.edu/2005/08/01/command-line-mute/

It is now very easy for me to mute/un-mute my speakers since I have this setup with SlickRun. Basically, while working on my computer I hit ctrl+space start to type mute and hit enter. I now have toggled the sound on/off on my computer speakers. Great Little Program!

Sleep/Pause on the command line

December 14th, 2006

A quick simple way to make the command line stop executing for a certain amount of time is by put

Everyone once in a while I need a way to stop the command line from executing for a certain number of seconds. By putting the 2 below lines in to a sleep.bat file I am able to create this functionality.

@ping 127.0.0.1 -n 2 -w 1000 > nul
@ping 127.0.0.1 -n %1% -w 1000> nul

In order to use this I just type "sleep 10" which will mimic a pause for 10 seconds (really it runs a ping in the background for 10 seconds).

Intro to Windows Power Shell

December 10th, 2006

I am just starting to familarize myself with windows Powershell. So far it sounds very powerful!

I often find myself using DOS to create quick scripts that will automate some process. Over time I have found DOS to be fairly weak when compared to a Linux shell. My solution to this problem was to download and install CYGWIN and then place the path to the files in the path. This provided me with many of the commands available to a linux shell in a DOS prompt.

From what I can tell Powershell has many of these commands available as well as a lot more functionality. It will allow you to work directly with objects. I am still a newbie at using the Powershell but I think it will be well worth the time to develop this skill.

Below are a few links to help get started using the Powershell:

Download the Powershell
Powershell Documentation
Powershell Blog
1st Chapter of Powershell in Action

Formatting SQL

December 7th, 2006

Today I found a nice little Java tool to help format long SQL queries. It has many options on how the string will be formatted. It will also take sql input types such as:

  • SQL
  • DB2/UDB
  • Oracle
  • MSAccess
  • SQL Server
  • Sybase
  • MYSQL
  • PostgreSQL
  • Informix

It then can take this input SQL and format it into a different type such as:

  • SQL
  • Java StringBuffer
  • Java String
  • C# StringBuilder
  • PHP String
  • ASP StringBuilder
  • VB String
  • VB StringBuilder
  • Concatenated SQL
  • HTML Code

This tool really has a lot of options and is very handy when trying to read/edit a long SQL query.

format sql screenshot

Java to C# Comparison

November 22nd, 2006

The first high-level language that I learned was Java. Of course I shortly found that there was a need to know C# too. So, as I started  to learn C# I kept a list of the differences between C# and Java (as can m be seen below). 
 
Class
--------
    Java: class GMTTime extends Time implements ICloneable{...}
    C#: class GMTTime: Time, ICloneable{...}
Constructor
-----------
    Java: GMTTime(){
              this(System.DateTime.Now);
          }
    C#:GMTTime():this(System.DateTime.Now){...}
       GMTTime(long millis): base(millis){...}//if base not specified default constructor
                                              //called

Java            =       C#
---------------------------
    Finalizer     =       destructors //spedifies code that will execute once an object is no
                                    //longer usable
    Package       =       namespace
    import        =       using
    StringBuffer  =       StringBuilder
   
    indexer: allows a class to be accessed as an array
    struct: lightweight class, value type instead of reference type (like class)

Method Inheritance
-------------------
    class BaseClass{
        //virtual is required  to allow this method to be overridden by a sub class
        virtual long GetTime(){...}
    }

    class childClass: BaseClass {
        //override allows an object to be a base class (BaseClass) but will still use
        //this method
        override long GetTime(){...}
    }

    class childClass2: BaseClass {
        //new overwrites the method but uses the method of the class that the calling
        //object is.
        new long GetTme(){...}
    }

Java Final
----------
    class = sealed //can not be inherited
    method = no virtual
    variable = const or readonly

Accessibility
-------------
    public = visible to all
    protected = only derived classes
    private = only withen given class
    internal = visible only to project/assembly/jar file
    protected internal = visible only to project/assembly/jar file and derived classes
    default = private

Getters/Setters
---------------
    //new C# way...old way used getter and setter methods
    class Circle {
         private int radius;
         public int Radius {
              get {return radius;}
              set {radius = value;}
         }

         public int Area {
              get { return 3.14 * radius * radius; }
         }
    }

    class otherClass{
        Circle c = new Circle();
        c.Radius = 3;//Note: no need to call a setter method.
        int area = c.Area;//Note no need to call a getter method
    }

synchronous = responds immediately
asynchronous = responds when ready to.

How to do events in C#
-----------------------------
1. Manually Trigger Event
    //namespace level, also specifies signature for event/handling methods
    public delegate void MyEventHandler();
2. Create an Event (requires a delegate)
    public event MyEventHandler TriggerIt;//class level
3. Register the event handlers with the event
    obj.TriggerIt += new MyEventHandler(obj.method);
4. Manually Trigger Event
    obj.TriggerIt();

// Declare the delegate handler for the event:
public delegate void MyEventHandler();//determines signitures of handling methods.

class TestEvent{
    // Declare the event implemented by MyEventHandler.
    public event MyEventHandler TriggerIt;

    public void MyMethod1(){
        System.Console.WriteLine("Hello!");
    }
    public void MyMethod2(){
        System.Console.WriteLine("Hello again!");
    }
    public void MyMethod3(){
        System.Console.WriteLine("Good-bye!");
    }

    static void Main(){
        TestEvent myEvent = new TestEvent();

        // Subscribe to the event by associating the handlers with the events:
        myEvent.TriggerIt += new MyEventHandler(myEvent.MyMethod1);
        myEvent.TriggerIt += new MyEventHandler(myEvent.MyMethod2);
        myEvent.TriggerIt += new MyEventHandler(myEvent.MyMethod3);
        // Trigger the event:
        myEvent.TriggerIt();

       
        // Unsuscribe from the the event by removing the handler from the event:
        myEvent.TriggerIt -= new MyEventHandler(myEvent.MyMethod2);
        System.Console.WriteLine("\"Hello again!\" unsubscribed from the event.");
        // Trigger the new event:
        myEvent.TriggerIt();
    }
}

typeof, GetType, is
----------------------
    Type s = typeof(String);
    Type s = "my String".GetType();
    "my string" is String //returns true

Array
-----
    MyArray[column,row]
    int[] arr = new int[]{1,2,3);
    int[] arr = {1,2,3};
    foreach(int i in arr){
        Console.WriteLine("Value is {0}",i);
    }

Create file in DOS edit/copy con

November 21st, 2006

When working in DOS sometimes I have the need to quickly create a file. Normally I would type “edit test.txt” which would open test.txt if it exists or create it if it doesn’t exist. This works well if I need to edit an existing file or type a lot of data. Note: use the alt key to navigate the menu in “edit” 

SP32-20061121-123114

edit test.txt

If I just need a quick file with only a few lines of text there is another method to quickly create a file. By typing “copy con test.txt”. It will then give a blank line to start typing the text that belongs in the file. Each time enter is hit it starts a new line and you will not be able to go back to the previous line. When you are finished typing the text hit CTRL+Z and hit enter. It will then create the file.

copy con test.txt