Category: DOS

Set An IP Address From The Command Prompt In Windows

19 November, 2008 | DOS | No comments

Rather than use the old connection properties dialog in Windows you can open up a command prompt and use the netsh to set up all sorts of network specific settings. The most useful part of this is that you can create a bat file that will allow you to quickly change your local IP address very quickly.

To see a list of the network connections available you can use the following command.

netsh interface show interface

This runs the netsh program, in the interface context, and shows the interfaces available. There are lots of other contexts to chose from, just type netsh and then ? to see a list of commands. If you type in netsh and hit enter key you will see the prompt change to netsh meaning that you are in that program mode. Just type exit or bye to exit.

You can also ask for information about individual interfaces by using the name parameter.

netsh interface show interface name="Local Area Connection"

To inform a network interface that you want to change its IP address you need to call it by the network alias. Local Area Connection is usually the default, but if you have more than one network port (wired or wireless) then you should double check this.

To set the IP address of a network alias to run DHCP use the following.

netsh interface ip set address "Local Area Connection" dhcp

To set the IP address manually use the following.

netsh interface ip set address "Local Area Connection" static 192.168.0.10 255.255.255.0 192.168.0.1 1

The static parameter needs three items. The IP that the connection is to be set to, the network mask and the gateway.

In this way, if you travel a lot and want to turn on or off DHCP at the touch of a button then you can create a series of bat files that use these commands.

Loop Through All Files In A Directory With DOS Batch

16 September, 2008 | DOS | No comments

DOS Batch is the Windows equivalent of shell scripting and can be used to perform all sorts of different actions. Anything that you type into a DOS prompt on a Windows machine can be used in a bat file to quickly do something that you would otherwise have to repeat many times over.

To create a bat file just make a file and give it the extension "bat". If you run a DOS prompt and navigate to the directory that the bat file exists in you can type the name of the file to get it to do certain actions. If you called your file "action.bat" you can run it by typing "action" or "action.bat".

Starting with a simple example, if you want to print the contents of a file to screen then you need the type command, followed by the file.

type file.txt

However, this puts a lot of rubbish on the screen. If you wanted to create a backup of that file then you would write the following.

type file.txt > file_back.txt

This takes the contents of one file and puts it in another.

To loop through every file in a directory you need to use the following line.

FOR %%i IN (*.*) DO echo %%i

This code will loop through the contents of a directory and print out each file name to screen. This will also list the bat file that you put in the directory so another solution might be to run the bat file from the directory above and use the following code.

FOR %%i IN (directory\*.*) DO echo %%i

The following snippet of code takes the previous example and does something useful. It loops through the directory and puts every file name that it finds into a file called list.txt.

FOR %%i IN (directory\*.*) DO echo %%i >> list.txt

The >> symbol will append any content to the file, so for every iteration of the loop the list.txt file gets one line bigger, until all of the files have been listed. In an example directory the following would be seen in the list.txt file.

directory\directory.html
directory\match.xls
directory\directory.xls
directory\file.txt
directory\file_back.txt