Rob van der Woude's Scripting Pages

News Archives 2007 Q4

 

[ Back to the current news page... ]

 

December 30, 2007 DrivUsed.bat is a batch file that uses XP's FSUTIL command to list all drive letters in use. I added some extra code to list the available drive letters as well.
The screen output looks like this:

Drive letters in use      A: C: D: E: F: G: H: I: J: K: N: O:
Available drive letters   B: L: M: P: Q: R: S: T: U: V: W: X: Y: Z:


Both lists will also be available in the environment variables %DRIVES% and %AVAILABLE%.
December 28, 2007 At login time 12 shortcuts in my Startup folder compete for computer resources with the starting firewall, antivirus software and other services. Windows XP is a multi-tasking OS, but still, starting programs one at a time is much more efficient and will take less time till the last one is started.
So I wrote DelayRun.vbs and modified most of the shortcuts' command lines into something like wscript.exe d:\path\delayrun.vbs 90 "C:\Program Files\Prog\myprog.exe" to delay starting of, in this case, myprog.exe with 90 seconds. For each shortcut I specified a different delay time.
It really works, I can now start working on my computer much sooner after login.
By the way, don't use this for "lightweight" programs, only for the "big guys" that consume lots of CPU resources while starting, like OpenOffice's quickstart tool. After all, WSCRIPT.EXE itself will consume some CPU resources too.

I found a new command to be added to my RUNDLL32 collection:

RUNDLL32.EXE ZIPFLDR.DLL,RouteTheCall zipfile.ZIP

This (XP) command opens a .ZIP file as a compressed folder in Explorer. Unfortunately, there seems to be no (native) batch command to copy files to or extract files from the .ZIP file (though it can be done in VBScript, see my VBScript Techniques: ZIP files page).
December 24, 2007 For a colleague I wrote CanonRep.vbs, a script to report the total number of prints for a Canon IR3320 printer.
It does so by reading the reporting page from the printer's built-in webserver and using a regular expression to extract the values for some of the totals.
I'm not sure if it will work on other Canon printer models too.
December 21, 2007 A "space bug" in my TrueName.bat was reported by John Taylor: if the true path to a mapped or SUBSTituted drive contained spaces it would fail.
This bug has been eliminated in the new version of TrueName.bat.

Thanks John

After creating CapsLock.exe in Visual Studio 2005 Express Edition, NumLock.exe was, of course, the next logical step:

    Const vbKeyNumlock = 144 '0x90 NumLock, see msdn2.microsoft.com/en-us/library/0z084th3(VS.71).aspx

    Private Declare Function SetKeyboardState Lib "user32" ( ByVal lppbKeyState As Byte ) As Long
    Private Declare Function GetKeyState Lib "user32" ( ByVal nVirtKey As Long ) As Integer
    Private Declare Sub ExitProcess Lib "kernel32" ( ByVal uExitCode As Long )

    Dim lNumLock As Long

    Sub Main()
        lNumLock = GetKeyState( vbKeyNumlock ) = 1
        ExitProcess( lNumLock * -1 )
    End Sub

Usage:

NUMLOCK.EXE
IF ERRORLEVEL 1 ECHO NumLock is ON
December 19, 2007 CapsLock keeps puzzling me.
KeyLocks.bat or KeyLocks.pl cannot always be used in corporate environments because they use the DEBUG command, which is usually blocked.
CapsLock.vbs needs either Microsoft Office or Corel WordPerfect Office, which isn't always available, and is a bit of an "overkill" for reading the CapsLock status.
Then there is the Console.CapsLock Property in the .NET Framework, but I couldn't get it working in VBScript.
So I finally decided to use "heavy artillery" and created CapsLock.exe in Visual Studio 2005 Express Edition.
CapsLock.exe is a true 32-bits console application which does nothing but return an errorlevel 1 if CapsLock is ON. It requires the .NET Framework 2.0.
It has been tested on my own computer only, so be careful, use it at your own risk.

In case you're interested, here comes the Visual Basic code (not VBScript!):

    Const vbKeyCapital = 20 '0x14 CapsLock, see msdn.microsoft.com/en-us/library/0z084th3(VS.71).aspx

    Private Declare Function SetKeyboardState Lib "user32" ( ByVal lppbKeyState As Byte ) As Long
    Private Declare Function GetKeyState Lib "user32" ( ByVal nVirtKey As Long ) As Integer
    Private Declare Sub ExitProcess Lib "kernel32" ( ByVal uExitCode As Long )

    Dim lCapsLock As Long

    Sub Main()
        lCapsLock = GetKeyState( vbKeyCapital ) = 1
        ExitProcess( lCapsLock * -1 )
    End Sub

Usage:

CAPSLOCK.EXE
IF ERRORLEVEL 1 ECHO CapsLock is ON
December 15, 2007 I still haven't found a way yet to set the CapsLock status in VBScript, though I did find a way to read the status using Microsoft Word or Corel WordPerfect: CapsLock.vbs.

While playing with the DEBUG command, I found a way to reduce the amount of code to read the CapsLock status, resulting in an update of KeyLocks.bat, plus a whole new use for a small utility that reads a keystroke and returns the ASCII value, resulting in Asc.bat, a "port" of many scripting languages' Asc( ) function.
December 14, 2007 While working on an unattended installation using KiXtart's SendKeys( ) function to enter a password during a personal certificate installation, I found out that SendKeys( ) really sends true keystrokes, and that the CapsLock status determines if a character will be received in upper case or lower case.
For passwords, that is unacceptable.

So I wrote CapsOff.kix, a script that checks the current CapsLock state and if ON, toggles it OFF again.
It does so by sending a single letter keystroke to itself, and then checking if it is received in upper case.

I know I could have used my own KeyLocks.bat, but in this case the password had to be entered under the user's credentials, which blocked the use of DEBUG.EXE.
CapsOff.kix works well even with limited permissions.

I haven't found a way yet to use the same technique in VBScript, but I'm working on it.
December 8, 2007 I have been off-line for a while because the flu got me. But as you can tell, I more or less survived.

Today I wrote a VBScript version of the WHICH command: Which.vbs. Unlike my other WHICH versions, this one searches the command interpreter's list of internal commands first, thus better emulating the command interpreter's behaviour.

I also updated Dec2Hex.bat, a batch file that uses a "poorly documented" feature of the PING command to convert decimal numbers to hexadecimal. Unlike the first versions, this new version is independent of Windows' language. I also added some extra command line checks, which require FINDSTR.
When optimizing the code for Dec2Hex.bat, I noticed that I might as well drop the use of PING and FINDSTR and use only CMD.EXE's internal commands. The price to pay is the input range: PING can handle numbers 0 .. 4,294,967,294 (0xFFFFFFFE or 232 - 2), CMD.EXE's SET can handle only half of that range (0 .. 2,147,483,647 or 0x7FFFFFFF or 231 - 1).
That is why I decided to "fork" the development, and name the new "internal commands only" version DecToHex.bat. So now you have a choice.
By the way: the reason PING cannot handle 0xFFFFFFFF is that this is the DHCP broadcast address!
December 1, 2007 Today I wrote a batch file that returns the status of the CapsLock, NumLock and ScrollLock keys: KeyLocks.bat.
It uses the (16-bits) DOS DEBUG command to read this status, and then returns it on screen, in environment variables and as an errorlevel.
You can use command line arguments to display and return the status of one specific key only, without arguments the status for each of them is shown and the errorlevel will be a combination of the status of all three key locks (OFF=0, CapsLock ON=64, NumLock ON=32, ScrollLock ON=16, all added together).

While at it, I also wrote a Perl version: KeyLocks.pl.
Strange as it may seem, Perl handles capturing Standard Output of 16-bits commands much better than batch or VBScript do.
However, it won't store the results in environment variables like the batch file does.
November 29, 2007 Converting date formats still is a challenge in batch files.
Today I wrote DateFmt.bat; it will convert the current date to any date format specified, and can also be used to display just a portion of the date, like the current day of the month, or the current month or year, or any combination.
The command line options to specify the date format are language specific, and will be displayed on screen when the batch file is called without any command line arguments.
The result will be displayed on screen and stored in an environment variable %DATEFMT%
November 25, 2007 Today I got this e-mail message from Tim Watson-Bartlam, and I think it may be useful to visitors of this site:

The other day I discovered some new tools which are only supplied in Windows Server 2003; although when I copied them to my XP box all the ones I have tried work. Not all the tools listed are useful for batch file programmers but many are and Microsoft have brought Choice out of retirement and encapsulated the FOR /F command in Forfiles.exe

The URL is: technet.microsoft.com/en-us/library/cc738152(WS.10).aspx
It looks like Microsoft integrated some popular Resource Kit utilities in Windows Server 2003.

Thanks, Tim.
November 21, 2007 Several updates today:

I updated Txt2Src.vbs, because it would give garbled results when run in WSCRIPT.EXE.
The updated version will restart itself in CSCRIPT.EXE when it finds itself running in WSCRIPT.EXE.

I also updated DefOpen.bat: its "Command Prompt Here" will now handle UNC paths in Windows 2000 and later, long file names in Windows 2000, and the script no longer tries to add "Open With Notepad" in XP or later, as this didn't work anyway.

I also updated Reboot.vbs and ShutDown.vbs to correct a typo in the on screen help text and to improve command line parsing.
November 20, 2007 I already had to update SendClip.vbs, as it would return the short file names in Windows 2000.
After replacing "%1" with "%L" in the regsitered command lines, the script will now return long file names in Windows 2000 as well as in XP.
November 19, 2007 Added context menu entry for files and folders My wife often sends me the fully qualified paths to some files that I need to proof-read for her. However, she sometimes forgets part of the path or misspells the file name, so then I need to search her harddisk remotely to find the files.

That's why I wrote SendClip.vbs, a script to send text strings to the clipboard.
Add to that the script's ability to register itself in Explorer's context menu for files and folders, and my infinite optimism (some might say naivety), and I expect I will never have to search again.

SENDCLIP.VBS /Register will add a menu entry "Send full path to clipboard" to Explorer's context menu for all files and folders.
Do not remove the script afterwards, or you'll get an error message when you try to send a path to the clipboard.
If you ever want to move the script to a different folder you can safely do so, and then register it once more, which will overwrite the old location with the new one in the registry.

SENDCLIP.VBS /Unregister will remove the menu entry again.
You will need to use this command before deleting the script.

SENDCLIP.VBS "Any text you want" will send the text to the clipboard. This may be useful in batch files.
Note the double quotes, they are necessary because the script accepts only a single command line argument, and a string in double quotes is treated as a single argument.
November 17, 2007 John Taylor sent me an excellent tip on how to read only the first line of a text file and save it in an environment variable using SET /P.

Thanks, John.
November 11, 2007 After reading this article by Scott Dunn I wrote NoAutoRun.bat, a batch file which completely blocks Windows XP's AutoRun feature.

Microsoft just released Windows PowerShell 2.0 Community Technology Preview (CTP).
November 10, 2007 I added several NETDOM commands to my "Admin One-Liners".
November 7, 2007 I wrote LogBatch.bat, a batch file to help debug other batch files.
It runs a modified copy of the batch file to be tested (it just removes every ECHO OFF command) and redirects this modified batch file's Standard Output and Standard Error to a log file.
October 26, 2007 I updated the User Messages page, and also added IELogin2.vbs, which can be used to generate a login dialog box with masked password input in batch files.
October 26, 2007 I uploaded an update to Foto2Web.bat, a batch file that creates HTML pages and thumbnails and resizes pictures to display them on a web site.
The update was written by Tim Smith, who rewrote several lines to make the batch file work with IrfanView 3.99 and later.

Thanks, Tim.
October 24, 2007 I updated runNhide.vbs, a script to run batch files (or "console" commands) in a hidden window. The script now has "standard" command line parsing.
October 20, 2007 I wrote Bin2Dec.bat, a batch file that converts binary numbers to decimal, and returns the decimal number on screen, in an environment variable and as return code ("errorlevel").
While testing this batch file, I also noted that Windows XP's CMD.EXE can handle 32-bit numbers, including negative numbers, as opposed to NT's 16-bit positive integer limit. Interesting...
October 19, 2007 Brad Kunst found an error in my PrintPDF.bat: I forgot to remove some debugging code, which made the batch file show what it was supposed to do, instead of actually doing it.

Thanks, Brad
October 17, 2007 I added a new page: Short Command Line Tips with really short samples of frequently asked batch functionality.
October 14, 2007 I added a new page to my VBScript Scripting Techniques section: a DelTree like subroutine to remove folders, whether empty or not. It has minimal safety features: there is no prompt for confirmation, but at least it won't delete root directories (which would effectively wipe an entire disk), nor the Windows, System32 or My Documents folders.
October 9, 2007 I added several books by Carolyn Z. Gillay and Bette A. Peat to my batch & command line books page.

Thanks for the "anonymous" tip
October 3, 2007 The final release of KiXtart 2010 4.60 is available now on www.kixtart.org.
October 1, 2007 Since the functionality is not available in XP's User Accounts control panel applet, nor in the NET.EXE command, I wrote PwdNoExp.vbs, a VBScript to set a local account's password to "never expires". Based on a Microsoft TechNet Script Center sample, my version accepts a user name on the command line.

 

Archived News pages
Archived news from 2007 Q3
Archived news from 2007 Q2
Archived news from 2007 Q1

 


page last modified: 2018-04-16; loaded in 0.0026 seconds