Something about batch programming

The simplest idea of how to write a batch file is: Figure out how you would type the commands at a DOS prompt, then type them, one per line, in a text file — and you’ve written your batch file.

Commandline Arguements {%x}

Variables can be inserted into a batch structure in the form of command line arguements. These are in the form %1, %2, etc. To populate the variables, type the desired values after the batch file name when executing it.

DOS Environment Variable Names {%nn%}

DOS environment variables also can be used as variables in batch files. For example:

COPY %windir%\filename a:

Where does one get a list of DOS environment variables? I have never found a comprehensive list; but a partial but lengthy list of existing environment variables can be gotten by typing SET at a command prompt.

And here’s the really cool part! You can make them up as you go along, and assign them as you wish (as long as you don’t grab one that has a legitimate assigned value, such as, say, %windir%, the Windows directory name!). Pick a name, populate it with the SET command by any means known to you (including having one batch file run a process that includes setting one, and then another batch file using it), then use it by placing the name between flanking % signs. Environment variables remain until overwritten, or until a reboot. (If you set them in a DOS window, they will end when that session is closed.)

If you precede an environment variable setting with the SETLOCAL command (on a line of its own), then environment variable changes are local to the batch file. They do not exist for any other process and they do not survive the completion of the batch file’s execution. You can turn this setting off by issuing an ENDLOCAL command later in the batch file.

Silly example: To change the current logged drive to D:, do the following:

SET GONEXT=D:
%GONEXT%

More practical example: You want to copy a file to the desktop of each user the next time they log into Windows. Each user logs into a different user profile, and the Desktop folder is in a unique location for each user. (The folder name will, of course, vary on non-English versions of Windows.) For a file called MYFILE.TXT, you can do this as follows on Windows 2000 or XP computers by using an environment variable %userprofile% which gives the path to the root of a given user’s profile:

COPY MYFILE.TXT %userprofile%\Desktop

START Command

The START command can launch a Windows program either by specifying the program name (and its command-line parameters), or by specifying a data file name that is associated with a particular program (one that would automatically launch if you clicked on it in Windows).

For example, if you have NOTEPAD.EXE associated with all TXT files, then you could open the file SOME.TXT in any of the following four ways:

NOTEPAD SOME.TXT
SOME.TXT
START NOTEPAD.EXE SOME.TXT
START SOME.TXT

Why use one or the other? Well, sometimes you may have to use one particular form to get a result — depending, for example, on how the particular program is coded. Though the first form usually will work, you may want, for example, to write a more general batch file to open any particular program and associated file — without knowing what the requirements of all such files might be. You could, then, write a general batch file line such as START %1% %2%.

One particular use of the START command is to launch the default browser and go directly to a URL, for example: START http://google.com

You may use any of four command line parameters with the START command. These go after the word START, but before the program name:

/minimized or /m
/maximized or /max
/restored or /r
/wait or /w

The first three determine the screen status in which the program opens. The last one forces the batch file to halt processing until the called program has finished executing. (This can be useful, for example, if you are loading multiple items in your windows Startup folder, and the nature of the programs require that one be finished before the next starts loading. Put them all in a single batch file, using the /wait parameter, and only put a shortcut to the batch file in the Startup folder.) Command line parameters of the START command can be combined in a single line. Example:

START /max /wait NOTEPAD.EXE SOME.TXT

IF and IF NOT Commands

There are three variations of the IF and IF NOT commands.

* IF EXIST: Execute the commandline only if a particular file exists:

IF EXIST some.txt COPY c:/some.dll %windir%/SYSTEM/some.dll
* Compare two text strings, and execute the commandline only if they are identical.

IF %HostWinBootDrv%==C SET WinDir=C:\WINDOWS
* Error testing: Check the exit code of the most recently run program. If it is equal to or greater than the number specified, execute the command:

IF ERRORLEVEL 4 ERASE trashfile.tmp /P

GOTO Command

You can set a label in a batch file by beginning a line with a colon. You can then go directly to that label with the GOTO command. The GOTO command searches both forward and backward in the batch file; that is, it simply goes to the label location, regardless of where it is in the file.

For example, in my batch file for removing the Happy99 virus, UNHAPPY.BAT, the following code was used to make sure a file was not deleted unless the original form of it (backed up by the virus under the name WSOCK32.SKA) is present:

IF NOT EXIST WSOCK32.SKA GOTO SavedIt
DEL WSOCK32.DLL
RENAME WSOCK32.SKA WSOCK32.DLL
:SavedIt

FOR Command

The syntax for this command is: FOR variable in (set list) DO command

The variable must be in the form of one alphabetic character preceeded by %%; e.g., %%v.

NOTE: The %% is necessary because this is in a batch file which, otherwise, would give a special meaning to a single %. However, if you run the FOR command outside of a batch file, simply from the system prompt, just use a single % in the variable name. (Tip from Steve Wisdom)

The set list is enclosed within parentheses. These values will be assigned to the variable successively. You can use any text enclosed in quotes, batch file commandline parameters, environment variables, or DOS file wildcard expressions.

The command can be any valid command that otherwise could be entered into the batch file as a line of its own. example:

FOR %%D in (SYSTEM, COMMAND, SHELLNEW, “Start Menu”) DO DIR “%windir%\%%D” /W

More Information on These Commands

Each of these options (START, IF, GOTO, FOR) is an actual DOS command. At a system prompt, type the command name followed by /? to get further help on these items.

Note that there may be particular capabilities that show up in one version of Windows, but not in another. For example, though DOS per se may well be dead in Windows XP, the commandline functions people most often associate with DOS are not dead at all! (We just don’t call them “DOS commands” anymore; we call them “command prompt commands”. But they’re the same thing.) In some cases, these commands have been made more powerful in Windows XP. In particular, if Win XP Command Extensions are enabled, each of these four has very greatly enhanced capabilities (see Win XP Help & Support to learn about Command Extensions). Take advantage of the opportunity to explore each of them with the /? help flag.

SOME EXAMPLES (that don’t even require Command Extensions): START has new flags that let you set the priority (/LOW, /NORMAL, /HIGH, etc.) of the application you are launching. IF has an ELSE option than greatly expands its power, but which requires a somewhat complicated syntax sometimes (which the /? help text covers reasonably well).

Since these START, IF, GOTO, and FOR are actual OS commands, they can be used from a system prompt just like DIR, COPY, or any other DOS command. This means that they can be used outside of a batch file as well. There are small differences or issues that you can easily discover in use, and discussion of which would go beyond the purpose of the present page. For anyone comfortable working at a DOS system prompt, this should present no significant problem.

[ad#s-hr-strip]

All about Telnet

Introduction:

Telnet is used to connect to remote machines through emulation. This means that it can connect to a server, any server instantly because of it’s ability to emulate (copy) remote computers.

In Windows, Telnet can be found by going to Start -> Run and then typing telnet. The application should then pop up. If you don’t have Windows, there are many Telnet alternatives for mac, linux etc. Since I don’t know any,search the internet for Telnet for Linux or Telnet for Mac and you should find something instantly.

If you have a PC and are using Windows 98/95, 2000, Me, or XP continue…

As you can see, Telnet has 4 menu options. These are Connect, Edit, Terminal, and Help.

If you click Connect, you should see more options. Here they are.

Connect:

Remote System..

Disconnect

Exit

Names of servers you have been on.

To connect to a server, click Remote System…

This will then take you to a dialog box that has these fields:

Hostname:

Port:

TermType:

There should also be a Connect button and a Cancel Button.

Lets go over these & what you should put in them.

-Hostname-

The Hostname can be the address of the website your going to be connecting to:

http://www.computerzworld.com

or it can be the IP address. Keep in mind that you can only connect to servers and not to clients (other PCs)

an IP Address looks like this:

100.101.102.103

-Port-

Port can bb either the port number you wanna connect to on the server (Port 80 is the Internet, Port 23 is Simple Mail Protocol then there are some more ports for different things.

or you can keep it Telnet and see what happens.

-TermType-

TermType is the type of Terminal telnet will be acting like.

This doesn’t matter most of the time, so you can keep it to default or try other ones if you want.

When you have all this set up, you can Click Connect & see what happens.

Note: Sometimes many servers don’t like anon people just logging on, so be careful when logging onto servers, because sometimes it might be some big company that likes bullying people or just SOMEONE who likes bullying people and then they might want to find you or trace you or something and I know for a fact you don’t need that happening.

Another Note: When you go to a server and type something, such as a login name or a password, you might not see anything even though you are typing. this is a feature that telnet or the other server uses. It’s for some kind of safety measure.

Ok that’s all for the Connect Menu, lets go over the Terminal menu.

The terminal menu should have 3 options:

Prefernces

Start Logging

Stop Logging.

-Prefernces-

If you go to this, a dialog box will come up with various options. Here, you can change the text color & background color of the program, and you can enable Local Echo, which shows you everything you’ve typed, Blinking cursor if you want the cursor to blink check it, if not uncheck, Block cursor if you want the cursor to be shown as a block, VT100 Arrows, don’t worry about these they are useless, Buffer Size, this allows you to set the number of lines of text you want to be shown before the screen starts to scroll, The Terminal emualtion type, always have this set on the default unless you know what the second one is and you know what your doing.

-Start Logging-

This logs everything you do on Telnet in a log file on your computer.

-Stop Logging-

This will stop logging processes.

[ad#s-hr-strip]

Convert a parttion Fat32 to NTFS by CMD

To convert a partition FAT16 or FAT32 to NFTS is easy.
Quit all applications you are running

Go to Start — Run and then type cmd. Enter this to the command line MS-DOS.

Type: convert X: / FS: NTFS (where x is the drive letter to convert), and begin the process of conversion. this is done, reboot your system.

You should not make any other operation during conversion to avoid system problems.
[ad#s-hr-strip]

Copying data from DOS / console mode program screens

If you’re still using DOS or console mode (also refereed to as character mode) programs that doesn’t provide a way to export data, you maybe looking for a way to capture what’s displayed on such programs. Try this:

* Start a DOS box or a Command Prompt (DOS box) inside Windows.

“Start | Programs | Command Prompt / DOS”

* Run the character mode program that you want to import data from, and go to the screen with the data you want to capture.
* Right click the title bar of the DOS box
* Select “Edit | Mark”
* Click and drag the mouse until you select the data area that you want to copy. Press ENTER when you’ve finished selecting.
* Switch to the Windows program that you want to import the just copied data to, and select its paste function.

[ad#s-hr-strip]

Microsoft File Compare

In MS-DOS 3.30 and later and all versions of Microsoft Windows come with Microsoft File Compare as the command fc. Whenever fc encounters a difference between the two files it is presented in the following format:

***** old.txt deleted lines 
***** NEW.TXT inserted lines ***** 

Note that fc capitalizes the filename of the second file. This format is not useful as input for other computer programs. Line notations are missing, but more problematic for an algorithm is the output of filenames by fc which is indistinguishable from an inserted or deleted line beginning with five asterisks and a space. Such a line may occur in a file that was produced by or discusses fc itself.

This is obsoleted by WinDiff which first shipped as part of the NT Resource Kit Tools.

[ad#s-hr-strip]