0

I have python installed on a school computer, but whenever I reboot, it of course loses the file associations and such. I have been building a batch file to try help me work around this problem, but I am stuck on the ftype command.

assoc .py=PythonFile
ftype PythonFile="H:\profile\programs\Python34\python.exe" "%1" "%*"

something is going wrong, because when I open a python file I get the error: H:\profile\programs\Python34\python.exe: can't find '__main__' module in '' I dont understand this. I tried %0 in the ftype command but I think it associated python file with the batch file itself, because when I ran any python file, I got

  File "init.cmd", line 1
    @echo off
            ^
SyntaxError: invalid syntax

by the way, I read this answer. it didnt help. Simple ftype command not working

(if there is an alternative in powershell, I am open! :) thank you in advance!
happy holidays!

Michael
  • 436
  • 3
  • 11
  • You are putting this into a batch file, and your path does not contain spaces, so does it work if you remove all of the double quotes? – WombatPM Dec 18 '17 at 17:39
  • It is not your computer, speak to your IT team and ask them about a solution or recommendation which meets the in place security policies. Also are you aware that `%*` also contains `%1` and that if ran from a batch file those percent characters should be doubled. – Compo Dec 18 '17 at 17:52
  • Ok thank you. I will see if these work :) – Michael Dec 18 '17 at 18:03
  • I wasnt aware that %1 contained all the arguments, including %1, though, would it also include %0, which is the batch file itself? – Michael Dec 18 '17 at 18:04
  • removing the quotes didnt work, and as i said below, when I replaced %1 %* with %%1 %%*, it worked only for very simple programs. – Michael Dec 18 '17 at 18:09
  • You *need* the quotes at least for `"%%1"`. Use `"%%1" %*`, not `%%1 %%*`. – DodgyCodeException Dec 18 '17 at 18:11
  • @Compo these are not batch arguments. They have to be entered verbatim into the registry for current user file association. The pattern `progname "%1" %*` is correct (see e.g. `FTYPE VBSFile`). But of course it has to be done as `"%%1"` inside a batch file. – DodgyCodeException Dec 18 '17 at 18:14
  • I dont need quotes between %*? – Michael Dec 18 '17 at 18:17
  • I'm not entirely sure but it seems that you don't. Several other file types, such as .jar and .vbs, just use `"%1" %*` as parameters, so I guess Python is the same. The `%*` is hardly ever used. It's only used when you drag & drop another file onto a .py file, and you want the Python script in the .py file to use the dropped file as a parameter. – DodgyCodeException Dec 18 '17 at 18:27
  • Ok, thanks. I kind of find all this file association stuff very confusing, but thanks for the help! – Michael Dec 19 '17 at 03:02

1 Answers1

1

The % character has special meaning inside batch files, so you need to "escape" them by using double %%:

@echo off
assoc .py=PythonFile
ftype PythonFile="H:\profile\programs\Python34\python.exe" "%%1" "%%*"
DodgyCodeException
  • 5,963
  • 3
  • 21
  • 42
  • I will try this, but I thought that %1 got replaced with the file I wanted to open, so escaping them would just have them be normal arguments, like %1 was a file i wanted to open – Michael Dec 18 '17 at 18:03
  • this worked for simple python programs, but more complicated ones, for some reason , I got some strange error. – Michael Dec 18 '17 at 18:05
  • @Michael you run your batch file once (when you login). At that point, you don't want the batch file to interpret `%1` as though it was its own parameter. You just want to put `"%1"` verbatim into the registry. That's why you have to double up the `%`. Later, when you click on a .py file, Explorer will see the `"%1"` and substitute you file name. – DodgyCodeException Dec 18 '17 at 18:08
  • sorry, I fixed the problem. I just had to put quotes between `"%%1"` so that any spaces in the files are OK. it was just a coincidence that they were more complicated files :) thanks for all your help! – Michael Dec 18 '17 at 18:15
  • 2
    `assoc` and `ftype` only modify the "HKLM\Software\Classes" filetype and "open" action. If Python is installed for the current user (i.e. .py is associated under "HKCU\Software\Classes"), these commands have no effect. Also, the shell caches the association, or locks in the user choice, in its "FileExts" key, which will also prevent the `assoc` command from having any effect, in which case the `ProgID` edited via `ftype` may not be used (e.g. user "open with" actions may have associated .py files with a "py_auto_file" or "Applications\python.exe" ProgID that's cached or locked in the shell). – Eryk Sun Dec 18 '17 at 20:04