Archive for the ‘DOS/Shell’ Category

touch with DOS commands

Tuesday, 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

Sunday, 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.

Mute on the Command Line

Thursday, 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

Thursday, 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

Sunday, 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

Create file in DOS edit/copy con

Tuesday, 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

Hardlinks in Windows (junctions)

Wednesday, November 15th, 2006

Hardlinks are something that I always thought were not supported in windows. Now with the NTFS file system hard links can be used.

In dos the fsutil command can be used to create a hardlink from a specific file. Using a command like the following:

fsutil hardlink create newFile.txt originalFile.txt

From what I can tell this does not support hardlinking a directory. In order to hardlink a directory you would have to purchase the windows 2000 resource kit and use the linkd program to do this.

Although, now there is another program created by Sysinternals/microsoft called junction which gives the functionality to hardlink a file or directory.

To top it off for everyone who likes gui programs there is a shell extension which also gives this ability which can be found here.

Dos Prompt Modifications

Thursday, October 26th, 2006

As I tend to use a dos a lot I decided it might be nice to modify the way dos looks. Below is the result of my modifications. I basically just changed the font color to green and made the working directory appear one line higher than normal.

Dos

Here are the steps if you would like to modify your dos prompt:

  1. To change the color of DOS go to /HKEY_CURRENT_USER/Software/Microsoft/Command Processory/ in your registry and modify the DefaultColor to be 0a
  2. To make the working directory appear one line higher, right click on “My Computer” go to “Properties” and then select the “advanced tab”. Now, click the “Environment Variables” button. Under “User variables” click the “new” button and put “PROMPT” (without the quotes) in the “variable name” text box and “$P$_$+$G” (without the quotes) in the “variable value” text box. This change will not take effect until the computer has been restarted.