George V. Reilly

Launching 32-bit applications from batchfiles on Win64

I've been running the 64-bit version of Windows 7 RC since June. It's been quite painless on the whole.

One wrinkle that I ran into was with some batchfiles which launch ap­pli­ca­tions in %Pro­gram­Files% (normally C:\Program Files). Due to the magic WOW64 redirector, 32-bit ap­pli­ca­tions are actually installed into %Pro­gram­Files(x86)%—normally C:\Program Files (x86)—instead of %Pro­gram­Files%. This is trans­par­ent to the 32-bit ap­pli­ca­tions, which think they're running in %Pro­gram­Files% (C:\Program Files).

However, the cmd.exe shell is 64-bit (unless you make a special effort to run the 32-bit cmd.exe in SysWOW64), so batchfiles see the 64-bit %Pro­gram­Files% which contains 64-bit ap­pli­ca­tions.

Hence, a batchfile that launches an installed 32-bit ap­pli­ca­tion on Win64 must use %Pro­gram­Files(x86)%, not %Pro­gram­Files%.

It sounds continue.

Python Batchfile Wrapper, Redux

Batchfile Wrapper

I've made some sig­nif­i­cant changes to my Python Batchfile Wrapper. The main virtue of this wrapper is that it finds python.exe and invokes it on the associated Python script, ensuring that input redi­rec­tion works.

I've also adapted py2bat to work with my wrapper. I'm calling my version py2cmd.

Here's my latest batch file, which is shorter than its pre­de­ces­sor.

To use it, place it in the same directory as the Python script you want to run and give it the same basename; i.e., d:\some\path or oth­er\ex­am­ple.cmd will run d:\some\path or oth­er\ex­am­ple.py.

@echo off
setlocal
set PythonExe=
set PythonExeFlags=-u

for %%i in (cmd bat exe) do (
    for %%j in (python.%%i) do 
continue.

Python Batchfile Wrapper

I've been getting into Python lately. One problem that I've en­coun­tered under Windows, is that input redi­rec­tion doesn't work if you use the .py file as­so­ci­a­tion to run the script; e.g.:

C:\> foo.py < input.txt

There's a well-known input redi­rec­tion bug. The fix is to explicitly use python.exe to run the script.

A related problem for me was that stdin was opened as a text file, not a binary file, so \r bytes were being discarded from binary input files. The fix is to run python.exe -u (unbuffered binary input and output).

I didn't want to hardcode the path to python.exe in a batch file, so I came up with the continue.

TIME stamping

I'm a command-line dinosaur. Vim (Vi IMproved) is my favorite text editor. And I write quite a few little batch files.

Here are a few useful tricks that work with cmd.exe on Windows XP.

Time­stamped filename

Sometimes I want to create a file whose name includes the current date and time. By combining the magic %DATE% and %TIME% en­vi­ron­ment variables, with for /f and a little bit of string sub­sti­tu­tion, I can create that filename.

REM
REM "Tue 06/14/2005" -> "06/14/2005"
REM
for /f "tokens=2"              %%i in ("%DATE%") do set MDY=%%i
REM
REM "06/14/2005" -> "2005-06-14"
REM
for /f "delims=/ tokens=1,2,3" %%i in 
continue.