Tag Archives: DIR

Loops in the Batch files with examples

This article is from our Febooti archive, it was relevant then, and I think that it is still relevant today (a few details changed). Previous article: Replaceable parameters in batch file.

WinXP-dir-p

The very simple loop in the batch script can be created by using GOTO command and a label. The syntax is – GOTO followed by the name of the line/label. The label is defined by the colon character at the beginning of the line. The label can be above or below GOTO statement. The code fragment below shows example of infinite loop.

:: LOOP.BAT
:: A batch file example using a infinite loop.
:TOP
DIR
GOTO TOP

The first two lines are remark statements giving the details of the program including the filename and a brief description of the program. The third line marks the line (creates a label) that a GOTO command can possibly link to. The fourth line will perform a directory listing. This is just an example command to illustrate a task that can be performed using the batch loop. You might like to indent the DIR command to emphasize that it is inside a loop. The last line is the command to jump to the line with the name TOP and to continue processing. Since there is no IF statement to test some condition and possibly to jump out of this loop, the batch file will continue to run indefinitely or until you press the CTRL+C or the CTRL+Break combinations on your keyboard.

Loop.bat output under Windows 8.1, Ctrl+C pressed
Loop.bat output under Windows 8.1, Ctrl+C pressed

:: LOOP2.BAT
:: Loop reads multiple replaceable parameters
:: with a SHIFT command inside the loop.
@Echo Off
:TOP
Copy %1 E:\
Shift
Goto TOP

LOOP2.BAT is a more useful program that copies a list of files specified after batch file to the E: drive. When the LOOP2.BAT program runs out of files to copy, the program will not stop. The batch script will attempt to continue until you use CTRL+C or CTRL+Break to terminate the program. To stop the batch script automatically, you need to add IF statement, but we will not discuss it here.

The first three lines are remark statements indicating the name of the batch file and a brief description of the batch file. The fourth line turns off command echoing. Remember that the @ sign turns off the echoing of the ECHO OFF command. The fifth line is the label that the GOTO command will jump to. The sixth line copies the file defined by the first replaceable parameter to the E: (usually flash drive) drive. The seventh line utilizes the SHIFT command to replace %0 with %1, %1 with %2, and so forth. The last line, GOTO TOP, is used to jump to the line labeled TOP and continue processing. Obviously the downside to this looping structure is that it needs more control. As mentioned above, one way to add this control is through the IF statement.

Next article: IF statement in DOS batch file.

This article is from our Febooti archive, it was relevant then, and I think that it is still relevant today (a few details changed).

Update Feb 21, 2015. Added link to the next article.

MS-DOS (cmd.exe) prompt basic commands

This article is from our Febooti archive, it was relevant then, and I think that it is still relevant today (a few details changed).

Since most of users do not use command prompt as their primary interaction with the operating system then we will go over the pure basics. These commands can also be used in a batch files. Because a batch file is a way of executing frequently entered DOS commands quicker, it would be a good idea to go over some command line basics.

In the previous article there is instruction how to access the command prompt – simply click the Start button (or Windows key on the keyboard) in the lower left hand corner of the screen followed by clicking on the Run… option (or simply typing cmd.exe on newer Windows). When you do this, a small box will appear in the lower left hand corner of the screen. To access the command prompt simply type cmd or command.

cmd.exe
cmd.exe

Now that we have a command prompt up, we will open a program. Let’s start with the Windows calculator. In the black screen you should see something to the extent of C:\>. You might have more to it or you might simply have C:\>. Either way to access the Windows Calculator you simply type calc at this screen. Then hit the enter key on your keyboard. The Windows calculator will pop-up immediately. This is how you are able to open any program from the DOS prompt (technically any program, program’s working folder must be in the path environment variable).

C:\>calc

Windows Calculator
Windows Calculator

One of the most commonly used DOS commands is DIR. The DIR command allows you to view a list of files that are on the disk, and also, to view which files and folders are on what disk. You are also able to view total file count, count of the directories, and all the sizes. You will also find the date and time column useful.

C:\>dir

dir command
dir command

At this point you probably have a bunch of text on your screen right now. What to do? Thankfully, DOS has a very handy command called CLS. Type CLS into your command prompt and hit enter right now. It clears up the screen. You will find this command very useful when playing with your command prompt.

C:\>cls

Back to the DIR command. If that list is too long for you, the DOS allows you to type DIR /p. Yes, just add a /p to the end of that. At this point remember that some of the DOS commands, batch specific commands, and filenames are in capital letters. DOS commands and / or parameters may or may not be case-sensitive.

C:\>dir /p

Performing the command above will display the directory listing but will pause after each screen of information. You will then be told to Press any key to continue… and when you press any key, the next group of information, if any, will be shown.

To see a list of filenames only put a /w as an alternative to the /p. The w stands for Wide and gives you a column name-only list display.

C:\>dir /w

dir /w
dir /w

You are able to view any drive on your computer. To view the listing for a floppy drive enter:
C:\>dir a:

To see the contents of your CD / DVD / Memory Card:
C:\>dir d:

The letter A was the most commonly used drive letter for the floppy drive. This works for other forms of removable storage as well such as flash drives and usually starts with letter D.

Note about dir /p. More universal way of ‘pausing’ long lists, is to use DOS command more. Try to enter the following and see whats happen.

c:\>dir | more

And to see more parameters (or also known as arguments or switches) use the question mark:

c:\dir /?

Next article tomorrow. Click here for the previous article – MS-DOS (cmd.exe) prompt introduction.

This article is from our Febooti archive, it was relevant then, and I think that it is still relevant today (a few details changed).

Edit Sep 19, 2014: added link to the Next article.