Rob van der Woude's Scripting Pages

News Archives 2011 Q1

 

[ Back to the current news page... ]

 

March 31, 2011 • As you may have noticed, I started a new C# section on this site. For now, it contains only some C# samples I wrote.
March 28, 2011 IsRedirected.exe is a small batch utility, written in C#. It returns an errorlevel based on the redirection of StdIn (+2), StdOut (+4), and StdErr (+8).
E.g. VER | IsRedirected.exe > logfile.txt will return "errorlevel" 6 (2+4, redirected StdIn + StdOut), and write VER's output to logfile.txt.
The optional /I, /O and /E switches will return only the result for StdIn, StdOut and StdErr respectively.
The C# code to detect redirection, by Hans Passant, was found on StackOverflow.com.
Tested in Windows 7 SP1 only.
March 27, 2011 • I fixed a bug in GetTitle.exe: in some cases invalid command line parameters would lead to system error messages instead of my custom error messages.
I also hardcoded the version number and description in the source code again.
March 25, 2011 • Justin Goldspring found a bug (or actually a design error) in GetTitle.exe: the old version would return the window title of the active window instead of the current window's title.
He also sent the code to correct the error, so I updated both GetTitle.exe and GetTitleXP.exe.

Thanks Justin
March 21, 2011 • I updated GetTitle.exe: its new optional /NP switch can be used to remove the "Administrator:" prefix on UAC enabled systems.
Viewing the source code you may notice that it reuses most of the code from IsElevated, to prevent it from trying to remove a prefix if it is not running with elevated privileges.
The new /NP switch would lead to unexpected results in XP, so for use in XP I included the old version (renamed to GetTitleXP) in the ZIP file.
As of this C# project, I use Matt Williamson's AssemblyInfo class to display the correct version number in the executable's help text.
To use the executable in a batch file, use the following code:

TITLE GetTitle test %Random%
FOR /F "tokens=*" %%A IN ('GETTITLE.EXE /NP') DO SET WindowTitle=%%A
SET WindowTitle
March 18, 2011 UTC2Date.vbs converts Unix time (UTC) to the current user's default date/time format.
Date2UTC.vbs converts any date/time to Unix time.
These scripts complete the new "product range".
March 17, 2011 Date2ISO.vbs converts any date/time to ISO date/time.
ISO2Date.vbs converts ISO date/time to the current user's default date/time format.
March 16, 2011 • My new UTC2ISO.vbs converts Unix time (UTC) to ISO date/time.
ISO2UTC.vbs does the reverse.
March 15, 2011 • I have been testing the many scripts I received after publishing a real-life batch challenge to write a batch that returns the title of its own window.
Though all do what they are supposed to do when run in the only instance of CMD.EXE, some fail when multiple instances of CMD.EXE are active, and most of them fail when multiple instances of the script itself are active.
Another challenge is the fact that in Vista and Windows 7, the title is prefixed with "Administrator: " if running with administrator credentials (which is usually required to run CMD.EXE at all).
I wondered if getting the title would be that difficult in C# (it isn't), so I Googled for "C# get window title" and found the information I needed on CSharpHelp.com.
The resulting source code and executable are available for download.
To use the executable in a batch file, try the following code:

TITLE GetTitle test %Random%
FOR /F "tokens=*" %%A IN ('GETTITLE.EXE') DO SET WindowTitle=%%A
SET WindowTitle

I will continue testing and debugging the scripts I received, and will publish the results and the names of the winners soon.
March 14, 2011 • Jiri Hofreiter sent me his Shuffle.bat, a batch file that shuffles the order of the lines in a text file.

Thanks Jiri
March 13, 2011 • Derived from Num2Bin.bat: Num2Oct.bat and Num2Hex.bat.
Guess what, they convert decimal, octal or hexadecimal numbers to octal and hexadecimal respectively.
All three are limited by CMD.EXE's 32-bit integer limit.
March 9, 2011 • Remember the challenges we faced to have a batch file create a valid Unicode header when converting ASCII files to Unicode?
Carlos M. came with a brilliant solution:

CHCP 1252 >NUL
CMD.EXE /D /A /C (SET/P=ÿþ)<NUL >  unicode_file
CMD.EXE /D /U /C TYPE ascii_file >> unicode_file


Without CHCP 1252, the SET /P command would ignore the first character of the header (0xFF), that is where earlier attempts to use SET /P failed.

View and download his A2U.bat for more details.

Thanks Carlos
March 1, 2011 • Another "spin-off": Num2Bin.bat. As its name suggests, it converts decimal, hexadecimal or octal numbers to binary.

• A "minor" bugfix for WeekPHP.bat: it no longer fails on forward slashes in the date argument.
February 28, 2011 • I updated ASCII2Uc.bat, the batch file to convert ASCII text files to valid Unicode. The batch file will now work in 64-bit Windows too.
In 64-bit Windows versions, it uses either an "external" header file (if it exists) or ECHO to create the 2-byte header for the Unicode file. Unfortunately, ECHO inserts an extra line break before the original text.

• Stan Littlefield tested my Week.vbs against the date 2003-12-29. It returns 53, but should have returned 1. There seems to be a bug in WSH's DatePart( ) function.
Fortunately, Stan also sent me some PHP code that does function correctly, so I created a wrapper batch file: WeekPHP.bat.

Thanks Stan
February 10, 2011 • While at it, I updated Week.vbs: it will now correctly display ISO week numbers, for any given date.
February 9, 2011 • OK, it took a while, but I finally managed to get the ISO week numbers correct in DatePart.cmd.
The challenge was to calculate the start of week 1, which always contains the first Thursday of the year.
Week 0 means week 52 or 53 of the previous year.
Use parameter ww to get the ISO week number for the current date.

• The technique used in DatePart.cmd to calculate the ISO week number for the current date is cloned in WeekXP.bat.
Because the day of the week is required to correctly calculate the ISO week number, it can be used for the current date only.
February 7, 2011 DatePart.cmd has been updated once more: the y parameter now returns the day of the year (like the VBScript DatePart( ) function does), and the new l parameter (L) returns 1 if the current year is a leap year, or 0 if it is not.
I'm working on the week number.
February 6, 2011 ReadXML.bat has been updated: the optional new /T switch will set the output format to plain text (default is XML).
February 4, 2011 • Denis St-Pierre sent me a tip on logging MSI (un)installs by adding some registry keys.
On the command line and in scripts you can use the /L switch, of course, but what if an MSI is embedded in an EXE, or when you click an MSI file?

Thanks Denis
February 2, 2011 • Another minor update for DatePart.cmd: besides showing the result on screen and storing it in an environment variable, it now returns it as errorlevel as well.
January 31, 2011 • A new page has been added to this site: Boolean Logic in Batch Files.
It discusses several techniques to combine multiple IF conditions with emulated OR, AND or XOR operators.
January 24, 2011 • I fixed a PHP error in the FAQ: the page now loads completely instead of stopping at the Use the date in a directory or file name section.
The cause was some PHP code that worked on the previous Windows server but not on the current Linux server.
Sorry for the inconvenience.
January 23, 2011 • A minor update for DatePart.cmd: a bug where it would sometimes return empty values has been fixed, and the parameter y can now also be used besides yyyy.
January 22, 2011 Cut.bat is my humble attempt at emulating the UNIX cut command.
To handle piped standard input it uses techniques not unlike those used in ReadLine.bat.

• I removed the separate pages for the CUT, TEE and WHICH commands, as they can now all be viewed on the restyled UNIX ports page.

• For those of you who make too many typos: my 404 error handling page has been partly rewritten. If all went well (and I did test thoroughly to make sure it would) you won't notice any difference except maybe a performance improvement (error page loading faster).
In techno-babble: (almost all) redirection is now handled on the server-side instead of the client-side.
January 19, 2011 ReadLine.bat, as its name suggests, reads a line from its standard input.
Unlike its OS/2 namesake, this batch file allows you to choose which line to read, or even to concatenate all lines into a single line.
The result is stored in an environment variable %ReadLine%.
Especially when concatenating all lines, keep in mind that there are OS imposed limits to environment variable size and command line length (both 8KB - 1 byte in Windows 7 64-bit).
January 18, 2011 • The new DatePart.cmd Version 2 returns the specified part (e.g. year, day, hour, etcetera) of the current date or time on screen and in an environment variable %DatePart%.
Its predecessor, DatePart.bat Version 1, was just a wrapper for the VBScript DatePart( ) function, the new version uses WMIC instead.

• Another minor update of Which.bat: some previously omitted commands were added to the list of known internal commands.
January 17, 2011 • I added some simple techniques to detect if Windows runs in a Virtual machine.
They are not suited to detect BluePills, only "friendly" virtualization, as most of them require the (MS) virtualization integration tools to be installed in the VM.
January 13, 2011 • "SmartGenius" sent me a great tip on more undocumented dynamic environment variables.
I used his research to write Chr.bat, a batch file to display the ASCII character(s) specified by number.
E.g. CHR 65 will display A, and CHR 84 104 97 110 107 115 32 34 83 109 97 114 116 71 101 110 105 117 115 34 will display Thanks "SmartGenius".
Unfortunately, to write characters 32 (space), 34 (doublequote) or 61 (equal sign) required a dirty trick: a caret followed by a backspace and then the actual character will be displayed. That will look ugly when redirected, though on screen you won't notice. If this will be a problem, either use the old version of the batch file (included in the ZIP file) which simply ignores these characters, or just don't use these 3 characters.

CHR 84 104 97 110 107 115 32 34 83 109 97 114 116 71 101 110 105 117 115 34
January 12, 2011 • Many scripts I write were created because I needed them myself.
Edit.bat is no exception. It opens a file in the editor registered for its file type, the same way the START command opens a file with its registered program.
The batch file requires Windows XP or later, but it can easily be modified to work in Windows NT 4 or 2000 by modifying the REG Query commands (and verifying that REG.EXE is available).

• Steve Robertson reported a bug in Hardware.hta version 4.20: in Windows XP it would not list any network adapters, because the WMI query used Win32_NetworkAdapter's "PhysicalAdapter" property, which was introduced in Vista and is not supported in XP.
This has been fixed in the updated version 4.21.

Thanks Steve
January 11, 2011 • Paul Cramer sent me a detailed list of VER output values.
That is a welcome addition to this site.

Thanks Paul

• A minor update of Which.bat: if SysInternals' STRINGS.EXE is available, it will be used to verify if the specified command is one of CMD.EXE's internal commands; otherwise the batch file will only check the specified command against a list of "all" known internal commands.
January 10, 2011 Which.bat has been updated: it will now also "locate" CMD.EXE's internal commands added in Vista and Windows 7, plus several internal commands the old batch file did not recognize (e.g. VER, APPEND, etcetera).
January 6, 2011 WGet.vbs is a new VBScript derived from WGetTxt.vbs.
WGet.vbs will show a web page (as text) with the HTML formatting, WGetTxt.vbs will display it without HTML formatting.
January 5, 2011 • I needed to check if a minimum Windows version requirement was met, so I created WinVer2.bat.
The list of VER output strings was found at Wikipedia.
January 4, 2011 • I might have known: as soon as I wrote I couldn't find a reliable batch file command to detect if a process runs with elevated privileges in Vista or Windows 7, someone would tell me there is a way.
Ron Guggisberg mailed me a very simple but so far fool-proof tip: run OPENFILES and check the errorlevel (0 if running with elevated privileges, 1 if not).
Works great, even in XP!

Thanks Ron
January 3, 2011 • Happy New Year!
May 2011 bring you peace and prosperity.

• Back to business.
I got quite some high quality reactions to December's real-life batch challenge (how to read the title of a batch file's own window).
I'll get back on the subject soon, presenting the solutions found (yes, we do have working solutions).

• My script to save the text only of a web page to an ASCII file, WGetTxt.vbs, has been updated.
Writing to file has become optional, you may also choose to display the text on screen (making it easier to parse the text in batch files using FOR /F).

• I added a link to Dirk Paehl's MUPDF to my File Viewers page.
Besides a small footprint PDF viewer the package contains a (command line) PDF to image converter and 5 other PDF utilities.

• I needed to find out if a batch file was running with elevated privileges or not in Windows 7.
Since I couldn't find a reliable way to do this in batch, I wrote IsElevated in C#.
Just run it without arguments; it will return (errorlevel) 0 if it is running with elevated privileges, 1 if not, 2 if started with command line arguments.
Most of the source code comes from StackOverflow.com — thanks Steven Schermerhorn.
It hasn't been tested in other Windows versions yet (in fact I have done little testing at all), but theoretically it should check if the current user is in the Administrators group in XP.

 

Archived News pages
Archived news from 2010
Archived news from 2009
Archived news from 2008
Archived news from 2007

 


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