Rob van der Woude's Scripting Pages

News Archives 2009 Q1

 

[ Back to the current news page... ]

 

March 22, 2009 • I added the core functionality of yesterday's GConv.vbs to the VBScript Scripting Techniques section.
March 21, 2009 • Google Calculator can be used for unit conversions (e.g. metres to yards etc.). Internet Explorer can be automated to retrieve web pages. So why not combine these features? Behold GConv.vbs, a script that allows us to perform unit conversions on the command line.
March 19, 2009 • A new command has been added to my Short command line tips page:

TYPE input_filename | MORE /E /P > output_filename

This command converts "isolated" linefeeds in (Unix) text files to carriage return/linefeed pairs.

• Carlos M. did some research on the hitherto undocumented or poorly documented file attributes substitution of the FOR command. The results of his research can be found here.

Thanks Carlos
March 13, 2009 • As a spin-off of a project I added a page containing a number of conversion routines (i.e. convert temperatures from °F to °C, distances from nautical miles to kilometers, etc.).
But make sure to double check before using the results to plan your next Antarctic expedition, especially considering the date this page was made...
March 2, 2009 • Leo Gutierrez Ramirez found a way to add any (language) style comments to SET /P commands:

@ECHO OFF
SET /P "sum=2+2: " # Add your comments here
SET sum


You may use any or no comment characters, as everything after the closing doublequote seems to be ignored.

Thanks Leo
February 23, 2009 • Nushu sent me a message with a trick to combine a batch file and other scripting languages in a single file without the need to create temporary files.
His example uses AutoIt, but it can also be used for KiXtart scripts and .REG files!

Thanks Nushu

• Carlos M. found a way to increase the number of variables available in a FOR /F loop.
This may be useful to handle longer lists in a single FOR /F loop, instead of having to nest several loops and/or subroutines.

Thanks Carlos
February 22, 2009 • Justin discovered an undocumented feature of the REN command:

REN testfile.txt *s

will rename testfile.txt to tes.
Compare that to:

REN testfile.txt *a

which will rename testfile.txt to testfile.txta.

Thanks Justin

• Because of Justin's information on the REN command, I decided it was time to give the command a page of its own.
February 20, 2009 • I am rewriting my old DATE/TIME in NT page. The new pages are listed in the batch techniques page, Time & Date section:
• I also added a page listing techniques to add or remove leading zeroes in NT batch.
February 18, 2009 • Denis St-Pierre wrote to me about hours of debugging caused by VBScript's default ByRef reference for function parameters.
Denis wrote a test script to familiarize yourself with the concept and with VBScript's behaviour. This may be particularly important for those who learned scripting with a different language, since many if not most languages have a different default behaviour.
I also added Denis' findings to my VBScript debugging page.

Thanks Denis
February 17, 2009 • I promised you a What's New replacement, here it is: What's New!
You can select the time span, and the files are listed by their last modified date, in reverse order.

• Google likes to receive a sitemap to crawl a site more accurately. So recently I started writing a sitemap generator script in VBScript, but after a while decided it was a lot easier in batch, and wrote Sitemap.bat. It does have one serious limitation: it cannot handle "high ASCII" nor Unicode characters in file names.
February 16, 2009 • Unfortunately FreeFind's What's New service doesn't support PHP based sites.
That's why I terminated my FreeFind account.
You'll have to make do with this News page for a while.
I'm working on my own "What's New" replacement.

Sorry for the inconvenience.
February 14, 2009 • Jiri Hofreiter from Prague (Czech Republic) found a better, simpler solution to the third batch challenge. I suppose that makes him a batch guru, and puts me out of work 😉

Thanks Jiri

• While on the subject of batch gurus, Ildar Shaimordanov from Russia sent me an ingenious way to add leading zeroes to dates and times.
He described it in one of his articles, but I'm glad he also sent me an e-mail in English 🤓

What it boils down to is that you can replace the leading space by a zero:

SET Now=%Time: =0%
SET Today=%Date: =0%


Note the spaces following the colons; they are essential!
So if you have a US system with M/D/YYYY date format, today's %DATE% value would probably be  2/14/2009 (with a leading space), and Ildar's code would convert that to 02/14/2009.
I'm not sure about a week ago, though, as I don't have a US system myself. You will have to decide for yourself if this trick can be used on your system(s).

Another great trick by Ildar is the way he ignores a leading day of the week when parsing the date to get it in YYYYMMDD format.
Where I use:

FOR %%A IN (%Date%) DO SET Today=%%A

to strip off the leading weekday and then start parsing the date:

FOR /F "tokens=1-3 delims=-/" %%A IN ("%Today%") DO SET YYYYMMDD=%%C%%A%%B

(code valid for MM/DD/YYYY format, adjust the order of %%A and %%B if necessary), and I still have to account for missing leading zeroes, Ildar just starts parsing from the end of the string backwards. Since he already replaced all spaces by zeroes, this will do the trick perfectly:

SET Today=%Date: =0%
SET YYYYMMDD=%Today:~-4%%Date:~-7,2%%Date:~-10,2%


(code for DD*MM*YYYY format). It is also completely independent of the delimiter used.
Perfect!

Thanks Ildar
February 12, 2009 • A new page has been added to the VBScript Scripting Techniques section: Convert ASCII to UTF-8.
It contains a sample function using ADODB.Stream to convert a "plain" ASCII file to UTF-8 encoding. It does not correctly convert from "standard" Unicode to UTF-8.
The code is based on a forum post by JTMar.

Thanks JTMar
February 9, 2009 • I added a new page to the VBScript Scripting Techniques section: Environment Variables.
Most VBScript users will be familiar with the WSH Shell's ExpandEnvironmentStrings method to read environment variables on the local computer, but how about setting or listing environment variables, from local and remote computers, in VBScript?
At least now you know where to look for more information.
The WMI sample code on this page was derived from a sample by Robbie Allen, author of the "Windows Server Cookbook" (ISBN 978-0596006334).

Thanks Robbie

• The reason I had been looking at setting environment variables was my new SetDate.vbs, a script that, when scheduled daily at midnight and at system startup, sets a couple of environment variables with the current day, month, year, week, quarter, weekday and day of the year. These variables can then be used by batch files to name log files, backup directories, etc.
These environment variables are (for today, Friday, April 26, 2024):

Date.Day=26
Date.DoW=6
Date.DoY=117
Date.Month=04
Date.Quarter=2
Date.Week=17
Date.Year=2024


Note that (the copies of) the environment variables keep their value within a session, once started, even if the date changes during that session. So the date variables available in a batch file/session are those of the date at the moment the batch file/session started!

To all webmasters who have deeplinked to my site: please update the urls (".html" becomes ".php")!
For the next couple of weeks redirection pages will automatically lead visitors to the new pages, but I intend to remove these redirection pages by the end of this month.

• For the few visitors that have JavaScript disabled: I rewrote the <noscript> menu at the foot of each page; thanks to a little CSS it now looks more like the "real" JavaScript based menu, though without the cascading menus. For a complete list of available links, the <noscript> menu features a "Full Menu" choice at the bottom of the menu block.
February 8, 2009 • Now that the "no change period" is over, a new chapter in the VBScript Scripting Techniques section was added: Windows Installer.
It contains two sample scripts by Adriaan Westra.

Thanks Adriaan
February 7, 2009 • Finally, I managed to get 2 old pages up-to-date (at least as far as these pages' technique is concerned, not their content): the Batch HowTos page and the FOR loops interactive demo.
Both depended so much on frames and JavaScript that they could not be migrated to the "new look" in July 2007, but they have now been completely rewritten in PHP.
I think the FOR loops interactive demo especially may still be of interest for everyone who wants to expand her or his understanding of FOR loops.
February 6, 2009 • Done!
All HTML pages have been replaced by PHP.
I'll be testing all pages once more, if you encounter any problem loading a page, please send a message to moc.eduowrednavbor@ofni .

• Those of you who subscribed with ChangeDetection to monitor my pages, you may receive a notification for each page you subscribed for, and that will be the last ones. You will need to resubscribe to the new pages, that now have a .php extension instead of .html. But you may want to consider delaying resubscribing, as I will do a lot of tweaking the next couple of days. And why not (re)subscribe only for this News page?

• A note on Web2PDF, the "Save page as PDF" button below the menu: examining its output I found that it does not use all my CSS code for printing. If you use "Print" from the menu to print a page to a local PDF printer, you'll notice the left page margin is not as wide as on screen, and all background colors are ignored, and all "outward" links are followed by their URLs. Web2PDF also uses the narrower left margin, but it does print background colors and does not print the URLs for outward links. So now you have a choice.
February 5, 2009 • I'm almost done converting all pages of this site from HTML to PHP.
You can have a sneak preview if you like. Note that links to sources won't work in this sneak preview.
Right now you won't find any spectacular changes or novelties, my first priority is to get everything going and end the "no-change period".
One novelty: you can now save any page as PDF, thanks to Web2PDF.
Another, less obvious change is the improved page load time. To state it more accurately: the page starts displaying while it is still being loaded because a greater part of the "non-content code" has been moved to the end of the pages.
February 1, 2009 • The next couple of days I will be converting all pages of this site from HTML to PHP.
You may not notice it at all, unless you subscribed to ChangeDetection to monitor one or more of these pages; in that case you'll get at least one e-mail notification for every page you monitor.
Sorry for the inconvenience, but I trust it will prove worthwhile.
January 29, 2009 • Today I updated WANIP.bat and WANIP.vbs, two scripts to display your computer's WAN IP address.
They both use this PHP one-liner:

<?php print $_SERVER['REMOTE_ADDR']; ?>
January 24, 2009 • For the foreseeable future this will be my final DEBUG based "clock spin-off": IsRTCSet.bat.
It too reads the CMOS, this time to determine if the Real Time Clock was set. If true, it will return an errorlevel 0, if false an errorlevel 1.
January 22, 2009 • Just for fun I wrote FlopDOS.bat, a "true" MS-DOS batch file that retrieves the floppy drive configuration from the CMOS (using DEBUG).
Do not use it in CMD.EXE (Windows NT 4 or later), or if you want to test it use COMMAND /E:4096 /C FLOPDOS.BAT. You may have to tweak the batch file to make it work in specific DOS versions — or languages. No guarantees!
For NT 4 and later the preferred method is using Floppy.bat.
January 20, 2009 • One of the most frequently asked questions (by visitors of this site) is how to create files or directories with the current date and/or time in their names.
As explained in the date/time section of this site there are many ways to get the current date in a variable that can be used in file and directory names.
The biggest disadvantage of using batch techniques to read date and time is the dependency of regional or international settings of the local computer: delimiter, order, day of week or no day of week, they all depend on these local settings. As demonstrated by my SortDate.bat and SortTime.bat, it requires lots of code to convert date and time to a fixed format.

I figured there must be an easier way. After all, "true" scripting languages like KiXtart and VBScript use (several) fixed date and time formats.
Then suddenly it dawned on me that the CMOS (BIOS) settings also use fixed date and time formats. And I used DEBUG before to read from the BIOS, so I gave it a try.
With help from The Starman's DEBUG Tutorial and PLASMA Online's CMOS Register Reference I found I could use the following DEBUG commands:

-o 70 0E
-i 71
09               (if greater than 7F, clock was not set after loss of battery power)
-o 70 32
-i 71
20               (century)
-o 70 09
-i 71
09               (year)
-o 70 08
-i 71
01               (month)
-o 70 07
-i 71
20               (day)
-o 70 06
-i 71
03               (day of the week)
-o 70 04
-i 71
10               (hours)
-o 70 02
-i 71
06               (minutes)
-o 70 00
-i 71
42               (seconds)
-
-q


I used this technique in 3 new batch files:
  • Now.bat, which displays the current time in HHmmss format (with an optional delimiter of choice)
  • Today.bat, which displays the current date in YYYYMMDD format (or DDMMYYYY, with optionally the day of the week as number or string, and an optional delimiter of choice)
  • TodayNow.bat, which displays both date and time in the fixed YYYYMMDDHHmmss format without delimiters.


• Using the same DEBUG technique described before, Floppy.bat reads the floppy drives configuration from the CMOS. This information is next to impossible to read using WMI.
January 12, 2009 • Some time ago, I noticed that several old batch files, using REG.EXE (version 2), no longer worked with version 3.0, which is native in XP.
When Tony Cardalda wrote to me that iDate.bat didn't work, I decided it was about time to set this right.

One could, of course, check the version of REG.EXE and then decide on the batch code to be used.
Unfortunately, Vista's version of REG.EXE doesn't seem to display its version number at all.
However, every version of REG.EXE so far seems to write its error messages to Standard Error, which means that FOR /F will ignore those error messages.
This made me decide to use a Q&D workaround: call both the old style and new style REG commands, and let FOR /F capture the output of the correct one:

:: For REG.EXE 3.0 (Windows XP) and later versions
FOR /F "tokens=3" %%A IN ('REG QUERY "HKEY_CURRENT_USER\Control Panel\International" /v iDate 2ˆ>NUL') DO SET iDate=%%A
:: For earlier REG.EXE versions
FOR /F "tokens=3" %%A IN ('REG QUERY "HKEY_CURRENT_USER\Control Panel\International\iDate" 2circ;>NUL') DO SET iDate=%%A
ECHO HKEY_CURRENT_USER\Control Panel\International\iDate=%iDate%


In Windows XP and Vista, the first REG command will correctly read the value from the registry and set the value of environment variable iDate accordingly; the second REG command however will fail, and FOR /F will "capture" nothing (and leave environment variable iDate alone).
In Windows NT 4 with Resource Kit and Windows 2000 the first REG command will fail, whereas the second one will correctly set iDate.
In either case, the end result is that environment variable iDate contains the requested registry value.
January 10, 2009 FfxCAC.bat will clear all Firefox cache directories in all local profiles.
In Windows XP it will abort if Firefox is still active in memory, in older Windows versions it will just fail to completely flush the current user's cache if Firefox is still active.
January 9, 2009 • I updated my page on login scripts: I added paragraphs on using login scripts to modify user settings, and some best practices.
January 8, 2009 • My new XL2CSV.vbs uses Excel automation to convert an Excel sheet to CSV.

• A new command was added to my NT admin one-liners page:
FOR /F %%A IN ('REG Query \\remotecomputer\HKU ˆ| FINDSTR /R /B /C:"HKEY_USERS\\S-1-5-[0-9][0-9]-[0-9-]*$"') DO (
    FOR /F "tokens=3 delims=\" %%B IN ('REG Query "\\remotecomputer\%%A\Volatile Environment"') DO (
        SET LoggedinUser=%%B
    )
)

It can be used to check who is logged in on remotecomputer.
January 4, 2009 • I'm working on a page on login scripts.
Though I intend to add much more information, I think it is ready for a "sneak preview" by now.

The page contains code snippets in NT batch, KiXtart and VBScript, but buttons are available to hide snippets for a specific language.

In case you want to deeplink for a specific language:
The buttons' functionality can be "automated" by adding parameters to the URL, e.g. ?nokix hides the KiXtart code, ?NoBatNoVbs hides all code but KiXtart.
The parameters aren't case sensitive and can be combined in any order, and you may use nobatch or even IDon'tWantNoBatchFiles; the JavaScript code searches for the substrings nobat, nokix and novbs in the entire parameter string following the question mark.
January 1, 2009 • I wish you all a happy 2009.
Have fun!

 

Archived News pages
Archived news from 2008
Archived news from 2007

 


page last modified: 2022-11-04; loaded in 0.0069 seconds