The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,861 other subscribers

Archive for the ‘Batch-Files’ Category

The CD pseudo environment variable in batch file: do not overwrite it with a real one!

Posted by jpluimers on 2014/10/08

I never realized you could overwrite the CD pseudo environment variable. If you do, the automatic value of the pseudo variable will not be udpated any more:

You have at some point set the CD variable explicitly. If you do this it will no longer automatically reflect the current working directory. To undo this, set it to empty:

set CD=

Thanks Jonathan and for explaining this in both your answers.

Thanks to another answer by Endoro I now also know of the %=C:% pseudo variable (you have one per drive letter) that indicate the current directory per drive letter.

–jeroen

via: batch file – When is the CD environment variable updated? – Stack Overflow.

Posted in Batch-Files, Development, Power User, Scripting, Software Development, Windows, Windows 7, Windows 8, Windows Server 2000, Windows Server 2003, Windows Server 2003 R2, Windows Server 2008, Windows Server 2008 R2, Windows Vista, Windows XP | 2 Comments »

Great answer on “windows – What encoding/code page is cmd.exe using” (via: Stack Overflow)

Posted by jpluimers on 2014/10/06

I just found this [Wayback] great answer (which by now regrettably is deleted; the previous Wayback link still has it) by [Wayback] Јοеу a.k.a. Johannes Rössel on [Wayback] What encoding/code page is cmd.exe using.

The whole answer is worth reading, so I won’t quote only some bits.

Edit 20210609: the answer now has been replaced by an even more detailed answer [Wayback] by [Wayback] andrewdotn. Also recommended reading. The summary of the new answer is this:

The moral of the story?

  • type can print UTF-16LE files with a BOM regardless of your current codepage
  • Win32 programs can be programmed to output Unicode to the console, using WriteConsoleW.
  • Other programs which set the codepage and adjust their output encoding accordingly can print Unicode on the console regardless of what the codepage was when the program started
  • For everything else you will have to mess around with chcp, and will probably still get weird output.

–jeroen

via:   windows – What encoding/code page is cmd.exe using – Stack Overflow.

Posted in Batch-Files, Development, Encoding, Power User, Scripting, Software Development, Windows, Windows 7, Windows 8, Windows Server 2000, Windows Server 2003, Windows Server 2003 R2, Windows Server 2008, Windows Server 2008 R2, Windows Vista, Windows XP | Leave a Comment »

which.bat (in case I don’t have CygWin on a system)

Posted by jpluimers on 2014/07/08

Small batch file that shows you the files on the path. It is a simple version that does not take into account built-in commands of cmd.exe, DOSKEY macros or PATHEXT environment variable: it just matches a name of an executable. Rob van der Woude has a complete which.bat (text version here) that does take into account all of the above. This is the poor man’s version:

@echo off
for %%f in (%1) do @echo. %%~$PATH:f
goto :eof

It uses this little trick from the FOR command:

%~$PATH:I Searches the directories listed in the PATH environment variable and expands %I to the fully qualified name of the first one found. If the environment variable name is not defined or the file is not found by the search, this modifier expands to the empty string.

–jeroen

Posted in Batch-Files, Development, Scripting, Software Development | Leave a Comment »

Windows: programmatically setting date/time stamps of files

Posted by jpluimers on 2014/07/01

For DOS programs, date and time stamps were used to mark versions of files. For instance, Turbo Pascal 6.0, had a 06:00 time stamp on every file.

You can still do this in Windows, but need to watch for a couple of things:

  • daylight saving time
  • more than one time stamp per file

There are various ways to do it. Besides a graphical Attribute Changer at www.petges.lu (thanks User Randolf Richardson), these are console approaches via How can I change the timestamp on a file?:
Read the rest of this entry »

Posted in *nix, Apple, Batch-Files, Cygwin, Development, Linux, Mac, Mac OS X / OS X / MacOS, Mac OS X 10.4 Tiger, Mac OS X 10.5 Leopard, Mac OS X 10.6 Snow Leopard, Mac OS X 10.7 Lion, MacBook, MacBook Retina, MacBook-Air, MacBook-Pro, OS X 10.8 Mountain Lion, Power User, PowerShell, Scripting, Software Development, SuSE Linux, Windows, Windows 7, Windows 8, Windows Server 2000, Windows Server 2003, Windows Server 2003 R2, Windows Server 2008, Windows Server 2008 R2, Windows Vista, Windows XP | 2 Comments »

Windows consoles: a small list

Posted by jpluimers on 2014/06/19

Many people confuse a shell with a console.

They are distinct: the shell executes commands, and the console hooks up video and keyboard to them.

Some products (like Take Command Console, of which Noah Coad is a huge fan) combine the two.

Some shells you can use for Windows: Read the rest of this entry »

Posted in Batch-Files, CommandLine, Development, PowerShell, PowerShell, Scripting, Software Development | Leave a Comment »

Windows shells: a bit of history

Posted by jpluimers on 2014/06/18

I’ve a long history in DOS/Windows 9x COMMAND.COM and Windows cmd.exe shell programming.

The switch to PowerShell is steep, but for me it is worth it: it has so much more functionality than cmd.exe, and taps right into the .NET ECO system.

If you look for something intermediate, you might want to consider TCC. Formerly TCC was known as 4NT, which has its roots in 4DOS (I totally loved 4DOS back when cmd wasn’t there yet).

A small overview: Read the rest of this entry »

Posted in Batch-Files, CommandLine, Development, Power User, PowerShell, PowerShell, Scripting, Software Development, Windows | Leave a Comment »

using tf to list the workspaces for the current directory in Team Foundation System

Posted by jpluimers on 2014/05/28

Since Visual Studio is always in a 32-bit directory, I needed the SetProgramFilesX86 batch file from

Windows batch file to set ProgramFilesX86 directory for 32-bit program files on x86 and x64 systems.

Here is a small batch file to list workspaces by any user for the current directory (assuming that directory is mapped to a workspace):

call "%~dp0SetProgramFilesX86.bat"
:: assume Visual Studio 2005:
::"%ProgramFilesX86%\Microsoft Visual Studio 8\Common7\IDE\tf" workspaces /owner:* /computer:* 
:: assume Visual Studio 2010:
"%ProgramFilesX86%\Microsoft Visual Studio 10.0\Common7\IDE\tf" workspaces /owner:* /computer:*

It uses TF, and assumes default locations for various Visual Studio versions based on the internal version number.

A generic TF batch file that finds the TF is this one:

call "%~dp0SetProgramFilesX86.bat"
:: assume Visual Studio 2005:
::"%ProgramFilesX86%\Microsoft Visual Studio 8\Common7\IDE\tf" %*
:: assume Visual Studio 2010:
"%ProgramFilesX86%\Microsoft Visual Studio 10.0\Common7\IDE\tf" %*

–jeroen

Posted in Batch-Files, Development, Scripting, Software Development | 1 Comment »

Windows batch file to set ProgramFilesX86 directory for 32-bit program files on x86 and x64 systems (via: Stack Overflow)

Posted by jpluimers on 2014/05/27

Every once in a while you need to execute a binaryfrom a batch file that will always be available 32-bits.
Examples include development tools (that usually are x86 hosts talking or encapsulating managed environments, and x64 debuggers) or office versions for which certain plugins are not available in x64 versions.

That’s why I wrote the below batch file to fill the ProgramFilesX86 environment variable to point to the 32-bit Program Files directory on any system (x86, x64, or one that does not make a distinction).

Determining processor architecture

The batch file makes use of the flags mentioned in HOWTO: Detect Process Bitness – David Wang – Site Home – MSDN Blogs:

Notes:

  • There are 3 distinct cases and two outcomes
  • We default to x86 just in case we run on a system that has none of the flags defined (for instance on legacy systems).
Environment Variable \ Program Bitness 32bit Native 64bit Native WOW64
PROCESSOR_ARCHITECTURE x86 AMD64 x86
PROCESSOR_ARCHITEW6432 undefined undefined AMD64

Source of 32-bit program files

The place of the 32-bit program files directory is determined by the environment variables mentioned in WOW64 Implementation Details (Windows):

Process Environment variables
64-bit process PROCESSOR_ARCHITECTURE=AMD64 or PROCESSOR_ARCHITECTURE=IA64
ProgramFiles=%ProgramFiles%
ProgramW6432=%ProgramFiles%
CommonProgramFiles=%CommonProgramFiles%
CommonProgramW6432=%CommonProgramFiles%
32-bit process PROCESSOR_ARCHITECTURE=x86
PROCESSOR_ARCHITEW6432=%PROCESSOR_ARCHITECTURE%
ProgramFiles=%ProgramFiles(x86)%
ProgramW6432=%ProgramFiles%
CommonProgramFiles=%CommonProgramFiles(x86)%
CommonProgramW6432=%CommonProgramFiles%
Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP: The ProgramW6432 and CommonProgramW6432 environment variables were added starting with Windows 7 and Windows Server 2008 R2.

Boolean logic and case insensitive compare in batch files

Many things in batch files can be tricky including these two:

The batch file

Finally: the batch file: Read the rest of this entry »

Posted in Batch-Files, Development, Scripting, Software Development | 4 Comments »

Using dsquery and dsget to get computer information from a domain

Posted by jpluimers on 2014/05/13

This article is a very brief example of how to use dsquery/dsget/find to get computer information from in the active direrctory of a domain.

The main aim for myself is to condense the information here, and have some links for background information.

If you have the right credentials then the below batch file works very well.

It uses these tools:

  • dsquery to query the active directory on your domain controller for the existence and Distinguished Name (or ID/path) of various objects (in this example dsquery computer to check if a computer exists in a domain)
  • dsget which can get you various detail information about an object (for instance dsget computer used in this example))
  • find to raise the correct errorlevel (and indicate if we indeed found a CN – or Common Name – from a distinguished name)

The ds* tools do not raise any errorlevel, so that’s what find is used for.

Further reading: Read the rest of this entry »

Posted in Batch-Files, Development, Power User, Scripting, Software Development, Windows, Windows 7, Windows 8, Windows Server 2000, Windows Server 2003, Windows Server 2003 R2, Windows Server 2008, Windows Server 2008 R2, Windows Vista, Windows XP | Leave a Comment »

Fix by Christian S. – Moers for “KB2686509 repeatedly fails with Error code 0x8007F0F4” (via: Microsoft Community)

Posted by jpluimers on 2014/05/07

Thanks Christian S. – Moers for the fix for KB2686509 repeatedly failing and the answer below.

I’ve used it as the Microsof FixIt offered at You may receive a “0x8007F0F4” error code when you try to install updates from the Windows Update Web site or from the Microsoft Update Web site did not work on several systems. The fix by Christian did work on all those systems.

Note: it is possible you get the same error for KB2676562: MS12-034: Description of the security update for Windows kernel-mode drivers: May 8, 2012, as it is related to KB2686509: MS12-034: Description of the security update for CVE-2012-0181 in Windows XP and Windows Server 2003: May 8, 2012.

One of the symptoms is that your system contains the file %windor%\faultykeyboard.log containing a list of  missing keyboard layout DLL files or KBD files (one of my machines had these missing: kbdjpn.dll and kbdkor.dll).

The cause is that KB2686509 can have problems with registry keys stored here:

  • HKLM\SYSTEM\CurrentControlSet\Control\Keyboard Layout
  • HKLM\SYSTEM\CurrentControlSet\Control\Keyboard Layouts

Christian wrote a batch file to fix it (named BatchFixKB2686509.cmd) which works splendid on the systems I had this error.

The batch file uses regedit /e to export parts of the registry, then writes a small .reg file to clear the keys and imports them with regedit /S, waits for you so you can install the update, then writes back the saved registry data.
So basically, it automatically performs the manual steps described at KB2686509 – Failure Due to Upgrade from Windows ME or 98 to Windows XP – TechNet Articles – United States (English) – TechNet Wiki.

His answer: Read the rest of this entry »

Posted in Batch-Files, Development, Keyboards and Keyboard Shortcuts, Power User, Scripting, Software Development, Windows, Windows XP | Leave a Comment »