1

I need to write a batch script to associate .py files with pythonw.exe. So I need to do two things: 1) find the path of the pythonw.exe, and then associate .py files, or specifically afile.py with pythonw.exe.

I know that I can find the path to pythonw.exe like this:

for %i in (python.exe) do @echo. %~$PATH:i

But the above command does not work from a batch file -- but rather it only works fro a command line.

I also know that I can use

assoc 

to associate file extensions with executables.

How do I put this all together into one batch file?

1 Answers1

1

First use %% instead of % inside batch files:

for %%i in (python.exe) do @echo. %%~$PATH:i

Then, use ftype to associate a command or a executable to a file type. After that, use assoc to associate the extension .py to the file type created before with ftype.

Your code should look like this:

@echo off
for %%i in (pythonw.exe) do set "pypath=%%~$PATH:i"
ftype PythonFile="%pypath%"
assoc .py=PythonwFile
Rafael
  • 3,042
  • 3
  • 20
  • 36
  • Thanks this looks promising. However I do get an error running the bat file. C:\Users\vincent\Desktop\chopin_proj\install>.\associate_python.bat File type 'PythonFile' not found or no open command associated with it. Access is denied. Error occurred while processing: .py. – Vincent Mayeski Oct 15 '14 at 03:10
  • It solves the "Access is denied" issue. But the problem is that it does not seem to change the association. The thing is that the original association is with Enthought Canopy. I'm trying to change the association to pythonw.exe as I'm distributing an app. So after instaling Enthought Canopy double clicking on a python file just brings up the canopy editor. – Vincent Mayeski Oct 15 '14 at 03:35
  • Can you check whether `pypath` variable is empty after `for` command? – Rafael Oct 15 '14 at 03:44
  • It looks good. It does find pythonw.exe ... perhaps this needs a reboot but I cant ask people to reboot their workstations after downloading my python program. My question is really about distributing python programs more than anything. I prob should have phrased it that way. – Vincent Mayeski Oct 15 '14 at 03:57