How to set file association in Windows Explorer?

290

Solution 1

On my system (Win7 x64, Anaconda2 and Anaconda3 installed), I needed to modify

[HKEY_CLASSES_ROOT\py_auto_file\shell\open\command]

by putting something like

"C:\Anaconda3\python.exe" "%1" %*

in the default entry.

By the way, %* is useful for passing optional arguments to your Python scripts.

Solution 2

New versions of Windows require Registry editing since the file association control panel does not allow you to specify parameter passing. One source suggests using this in a text file saved as "foo.reg" and opened by Windows:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Perl\shell\Open\command] @="\"C:\path\to\python.exe\" \"%1\" %*"

This should cause Windows to pass the filename to the interpreter.

Source: https://stackoverflow.com/questions/444388/how-can-i-pass-command-line-arguments-via-file-association-in-vista-64

Share:
290
DarkLeader
Author by

DarkLeader

Updated on September 18, 2022

Comments

  • DarkLeader
    DarkLeader over 1 year

    i have the following function:

    int run_func(command history[MAX_INPUT_SIZE], char** args, int capacity) {
        int need_to_wait = 1;
        int i = 0;
        char* arg = args[0];
        int status;
        while (arg != NULL) {
            if (strcmp(arg, "&") == 0) {
                need_to_wait = 0;
                break;
            }
            arg = args[i++];
        }
        pid_t wait_pid;
        pid_t pid = fork();
        int res;
        if (pid == 0) {
            res = execvp(args[0], args);
            if (res == -1) {
                printf("exec failed\n");
                fflush(stdout);
                return 0;
            }
        } else if (pid < 0) {
            printf("fork failed\n");
            fflush(stdout);
            return 0;
        } else {
            if (need_to_wait){
                do {
                    wait_pid = waitpid(pid, &status, 0);
                } while(!WIFEXITED(status) && !WIFSIGNALED(status));
            }
            history[capacity - 1].pid = pid;
        }
        return 1;
    }
    

    the issue I have is that the bottom while loop, hangs and doesn't stop whenever I get an invalid command such as 'hello' from the user from the terminal until I press enter again. this function is being called from another function that receives input from the user.

    • kaylum
      kaylum about 3 years
      The wait code looks fine. What does "until I press enter again" mean? Are you saying it hangs until enter is pressed? Also, suggest you add a print statement after the waitpid to confirm whether it has indeed returned or not.
    • DarkLeader
      DarkLeader about 3 years
      @kaylum i added printf statements before and after and yes, the wait_pid statement doesn't return until I press enter
    • Jonathan Leffler
      Jonathan Leffler about 3 years
      Error messages should be printed to stderr, not stdout. There's no need to save or test the return value from execvp() — if it returns, it failed; if it succeeds, it does not return. You should almost certainly have an exit() or _exit() instead of return 0; in the error handling code after execvp(). When the command fails (hello?), then you end up with two processes running — one from the failed execvp() and one the parent process.
    • DarkLeader
      DarkLeader about 3 years
      @JonathanLeffler thank you!
    • user3629249
      user3629249 about 3 years
      the posted code does not compile! Please post a minimal reproducible example so we can reproduce the problem and help you debug it.
    • DarkLeader
      DarkLeader about 3 years
      @user3629249 I can't post the entire program since it is an assignment and it might be considered cheating if another student uses my entire program as his own.
  • Doktoro Reichard
    Doktoro Reichard over 10 years
    Actually the accepted answer author disagrees with newer versions of Windows taking out the file association panel. The way one has to do, however, to achieve any kind of association is non-trivial (i.e. it's the same as the registry entry you typed). For reference here is the Microsoft page on the topic.
  • Dave
    Dave over 10 years
    Thanks KA for confirming it's not just me. I have no idea why Microsoft would be so stupid as to not support setting file associations that work (without manually editing config files or the registry), so I asked the question assuming it was my own lack of knowledge. No such luck I guess.
  • fixer1234
    fixer1234 over 7 years
    It isn't clear whether this is a suggested solution or a new question.
  • Hope
    Hope over 7 years
    @fixer1234, this was supposed to be a solution that worked in a similar case on my computer.
  • linuxdev2013
    linuxdev2013 about 7 years
    This has been tried per the initial question, please do take care to read the full question next time.