MS-DOS (Batch File) Shorts

30 November -0001
MS-DOS (Batch File) Shorts

I wrote this article to cover some shortcuts, tips, and batch files I've had to use over time at the MS-DOS command prompt. All these solutions are tested on Windows XP SP3 so your mileage may vary with a different flavor of Windows.

* How can I count the number of files in a directory?

The easiest way to do this is to simply utilize the regular output of the DIR command and pipe it through the FIND command. By utilizing:

dir | find "File"

You should see a listing of how many files are in a directory and their total size. If you just want a number of files, however, you'll have to use a more creative method. Using the following batch file you can easily count the number of files in a directory though:

@echo off
REM ***********************************************
REM * File counting script                        *
REM * By: Justin Klein Keane <> *
REM ***********************************************

set startDir = %CD%

if(%1)==('help') GOTO USAGE
IF (%1)==() ( 
	set work_dir=%cd% 
) ELSE ( 
	set work_dir=%1 

cd /d %1

SET counter=0

REM * First get a list of the files
dir /B /A:-D %1 > dirs.txt

REM * Next loop over the text file and rename them
FOR /F %%i IN (dirs.txt) DO (
	set /A Counter += 1
echo %Counter%

REM * Clean up
cd /d %startDir%


echo filecnt.bat
echo By Justin C. Klein Keane ^<^>
echo _
echo Usage filecnt [dir]


This may be a little more than you're looking for, but it will work.

* How can I create a new text file?

The simplest way to create a new file in MS-DOS is with the redirect command. You basically want to redirect nothing into a new text file. You can utilize:

echo. > empty.txt

* How can I set a variable? How can I echo, or retrieve, that variable's value?

Setting a variable in a batch file or on the command line is quite simple. To do this all you have to use is:

set variable=value

Where "variable" is the variable name and "value" is the value you want. So if you wanted the variable "foo" to be assigned the value "bar" you can use:

set foo=bar

You can access the value of the variable by referencing the variable name surrounded by percent signs. For instance, if you type

echo %foo%

You should see the word "bar" echoed back onto the screen.

* Save the current working directory in a variable (or set any command output to a variable)?

To set the current working directory name into a variable all you have to do is utilize the same syntax as above, but replace the value of the variable with another variable, in this case the command 'cd' like such:

set mydir=%cd%

* Change permissions of a file or folder?

You can change the attributes of a file or folder using the dos command 'attrib'. Changing actual user or group permissions utilizes the 'cacls' command. For instance, to list the attributes in your current directory just type 'attrib'. To add the read only attribute to a file you can use:

attrib +R file.txt

To remove the read-only attribute just use:

attrib -R file.txt

And so on. You can look at the possible options for the command using:

help attrib

To list a file's permissions using 'cacls' (Control Access Control ListS) you can use:

cacls file.txt

To add read and write permission to the file use for a user:

cacls file.txt /G username:R

You can revoke or edit permissions as well. To see all the options just type 'cacls' in your command prompt.

* Get a file's last update time?

Simply use:

dir /TA

* Show the time?

To show the date use:

echo %date%

To show the time use:

echo %time%

You can string them both together with:

echo %date% %time%

* Change into a directory (cd) with a space in the name?

To change directories with a name you have to quote the name. For instance:

cd C:\"Documents and Settings" 

will allow you to change into that directory despite the spaces in the name.

* Change to a different drive?

Changing your working directory to a different drive is a bit of a hassle. Simply using:


should change you to the new directory

* Resolve (or look up) an IP address?

You can resolve an IP address to a name using the 'nslookup' command. This command queries whatever DNS server you have set in your networking connection information. You can see this by using the 'ipconfig /all' command. The nslookup command is very powerful so it's worth looking at the documentation, but at it's simplest you can use:


or you could use

nslookup www.domain.tld

Be aware that the first result that appears is server information for the DNS server nslookup is querying, the second set of line is the result of the query. For instance:

Server:  dns.domain.tld


The destination is listed as the 'Name' and 'Address'

* View the ports open on a computer?

To view open ports on your local computer just use the netstat command. To view all open ports in numerical format (so things like ftp show up as 21) use:

netstat -a -n

If you want to look at open ports on a remote computer you need to use a port mapping tool like NMAP, there are no native MS-DOS commands you can use other than perhaps telnet (although telnetting to every possible port is a little pain-staking and I would recommend NMAP).

* Mount a network volume or share?

To mount a network volume you have to utilize the 'net use' command. Let's say you want to mount the share at named 'foo' to your local X:\ drive. To do this you would use:

C:\>net use x: \\\foo

You may need to specify a username and password to access the share. In this case you would use:

C:\>net use x: \\\foo /USER:[username]

Where you replaced [username] with the appropriate user name. This will prompt you for a password before the share is mounted.

* Rename all directories in a folder so that one character is replaced with another?

The easiest way to accomplish this task is to use a batch file such as this one:

@echo off
REM ***********************************************
REM * Directory renaming script                   *
REM * By: Justin Klein Keane < *
REM ***********************************************
REM * This batch file will fail with quoted directories!
REM * This line is important or the loop vars won't work
setlocal EnableDelayedExpansion

set startDir = %CD%

if(%1)==() GOTO USAGE
if(%2)==() GOTO USAGE
if(%3)==() GOTO USAGE

cd /d %1

REM * First get a list of the directories
dir /B /A:D %1 > dirs.txt

REM * Next loop over the text file and rename them
FOR /F %%i IN (dirs.txt) DO (
	set filename=%%i
	set output=!filename:%2=%3!
	ren !filename! !output!
REM * Clean up
del dirs.txt
cd /d %startDir%


echo rendir.bat
echo By Justin C. Klein Keane ^<^>
echo _
echo Usage rendir [dir] [search] [replace]