Redirection examples

Redirection to temporary batch files

Show free disk space in DOS (COMMAND.COM), the hard way

Recently I was asked if it would be possible to show the free space of a disk using only batch files, preferably using numbers only.

Well, of course.
Basically, all we need to show free disk space is the DIR command, which ends with a disk summary like this:

36 file(s) 107.432 bytes used
178.895.872 bytes free

Create a file called DISKFREE.DAT containing just one line and without a carriage return/line feed at the end of the line.
One way to create such a file, if you are not sure if your editor can create it, is by using the COPY CON command:

@ECHO OFF
IF "%2"=="K" SET DISKFREE=%1000
IF NOT "%2"=="K" SET DISKFREE=%1
IF "%2"=="K" ECHO %1 %2
IF NOT "%2"=="K" ECHO %1

As you may have noticed, the "%%1" in DISKFREE.BAT has resulted in "%1" in DISKFRE3.BAT.
Had we used "%1" in DISKFREE.BAT then "%1" would have been interpreted by DISKFREE.BAT before redirection to DISKFRE3.BAT.
This is prevented by using "%%1" in DISKFREE.BAT.
DISKFRE3.BAT sets the variable %DISKFREE% to %1 multiplied by 1000 instead of adding a "K" (multiplier by 1024) to it, so that the variable %DISKFREE% will contain numbers only.
The ECHO command will show a "K" if applicable.

Then DISKFREE.DAT is copied to DISKFRE2.BAT and the last line from the DIR command is appended to it:

When DISKFRE2 is called, it will call DISKFRE2.BAT in turn, which will ignore the spaces preceding the first parameter, and thus display only the amount of free disk space in bytes.

If you want to check if there is enough free space to install your files, DOS batch files won't get you much further than telling you the order of magnitude of the amount of free space.
Using Ziff Davis' BATCHMAN would be a lot easier.
Try it, it's a great utility combining lots of features still missing in DOS.

Note:

Search the examples for batch files that list free space in Windows NT 4 and later.
For Windows XP use this simple WMIC example.

lock single user programs

Logoff disconnected Windows NT 4 Terminal Server ICA clients

To remotely logoff a disconnected user from an NT Terminal Server "farm" (several servers transparantly clustered together) you need to know which server that user was logged onto, and his/her session ID.
To find the session ID the QUERY USER command can be used.
With the resulting session ID the user can be remotely logged off.
If the user logged on immediately after his/her session crashed, problems may arise with remote logoff.
This situation can be recognized by different numbers for the connection and session ID.
The connection ID takes the form ica-16#nn (for 16 bit clients, that is), whereas the session ID is a plain number.
If the session ID does not equal the number nn in the connection ID, the user should first logon, and logoff again, himself.

The following batch file takes into account all that, and will only logoff a user if he/she is disconnected and if the connection and session ID's match:

you can define what you mean by "relevant programs". For example, adding

| FIND /I /V "winword.exe"

between explorer.exe and winfile.exe will even kill sessions with MS-Word active.
Be carefull, though: this may cause loss of unsaved data!

Check if you are connected to the internet

Works with Dial Up Networking as well as Proxies

(tested in Windows 95 and 98 only)

@ECHO OFF
REM Create temporary batch file
ECHO @ECHO OFF> REPLY.BAT
ECHO SET connect=1>>REPLY.BAT
ECHO Checking connection, please wait...
ECHO @ECHO OFF> CHKCONN.BAT
PING 194.109.6.66 | FIND "Reply from " >>CHKCONN.BAT
REM Set "default" value, which may or may not be altered by CHKCONN.BAT
SET connect=0
CALL CHKCONN.BAT
DEL CHKCONN.BAT
DEL REPLY.BAT
IF "%connect%"=="0" ECHO You have NO active connection to the internet
IF "%connect%"=="1" ECHO You have an active connection to the internet

The following enhanced (simpler) version doesn't need redirection to temporary batch files at all, but uses FIND's return codes instead (enhancement by "Jawade", one of alt.msdos.batch's contributors).
Use the "old" version to learn the use of temporary batch files only.

@ECHO OFF
ECHO Checking connection, please wait...
PING 194.109.6.66 | FIND "Reply from " > NUL
IF NOT ERRORLEVEL 1 ECHO You have an active connection to the internet
IF ERRORLEVEL 1 ECHO You have NO active connection to the internet