-5

Is there a way to maintain the original file association when running a program from a batch file?

I created a batch file that calls a Windows program and performs some file maintenance. I changed the file association to the batch file. When I click on a file that's associated with that program, the batch file executes and opens the program but the file I click on isn't loaded. The original file association is lost.

This sorta makes sense because the CALL command within the batch file is once removed from the initial mouse-click that initiated the batch file.

Is there a syntax I can add that would pass the target file name to the batch file as a variable and append it to the CALL command line?

BTW, this is for an XP machine. Any assistance would be appreciated!

EDIT: here's the code I'm trying to write:

call "C:\Program Files\CorelDRAW X4\Programs\CorelDRW.exe"
:loop
if exist "C:\Documents and Settings\<user>\My Documents\corel user files\*.cdr" copy "C:\Documents and Settings\<user>\My Documents\corel user files\*.cdr" "C:\Documents and Settings\<user>\My Documents\corel user files\*.sav"
ping localhost -n 300 > nul
goto loop

I'm trying to protect CorelDraw's auto-save file. There's a bug whereby CorelDraw sometimes deletes the auto-save file during abnormal shut-down. I changed the .cdr file association so that clicking on a cdr file calls the batch file, which in turn calls Coreldraw and copies the auto-save file to a different filename. That part works, but I have to manually open the file I clicked on.

Ideally, I'd like to figure out a way to terminate the loop when I close CorelDraw, but I'll cross that bridge once I solve the file association problem.

EDIT2: Here is the result of echo %CMDCMDLINE%:

C:\WINDOWS\system32\cmd.exe /c ""C:\Documents and Settings\<user>\My Documents\corel user files\protect_autosave.bat"  "C:\Documents and Settings\<user>\My Documents\filename.cdr""
ginahoy
  • 1
  • 1
  • 1
    You may find that the `%CMDCMDLINE%` variable gives you what you need. – Compo Apr 29 '18 at 02:19
  • @Compo thanks for the tip, but %CMDCMDLINE% returns the entire command string so I'm not sure how to use that. BTW, this is my first comment in this forum. How do I add a linebreak in my comments? According to the linked page on mini-Markdown formatting, two spaces adds a linebreak but it doesn't work for me. – ginahoy Apr 29 '18 at 10:54
  • 1
    Use `%*` to append all script arguments to the call. i.e. `call "C:\Program Files\CorelDRAW X4\Programs\CorelDRW.exe" %*` – michael_heath Apr 29 '18 at 13:19
  • @ginahoy, why not include in your question area, the string assigned to `%CMDCMDLINE%` so that we can see it. Also are you aware that arguments given to the batch script can be read already using special metavariables `%1`, `%2` etc. Also why change a file association? The easiest way to do this is to create a right click context menu entry so that whenever you select a `.cdr` file you get a menu allowing you to run the batch file. – Compo Apr 29 '18 at 13:20
  • @Compo, if there's a way to do this without changing file association, I'm all ears. It's been probably 10 years since I modified the Windows Explorer context menu but I'm sure I can figure that out. But I still need to figure out how to pass the target filename to the batch file. I added the result of `%CMDCMDLINE%` to my question. As you can see, it includes the entire command, not just the target filename. – ginahoy Apr 29 '18 at 21:06
  • Thanks for the edit, if you take a look at the value of the `%CMDCMDLINE%` variable, you can see that the `protect_autosave.bat` file was called with a single parameter. In your actual batch file that parameter can be directly referenced with a special metavariable, `%1`. So you have two options, parse `%CMDCMDLINE%` to retrieve the called files name, of use the special metavariable, _(easier)_. To create a context menu entry, you'd add a couple of registry keys relevant to the `HKCU\Software\Classes` file association, `.cdr`. – Compo Apr 29 '18 at 23:07
  • @Compo... excellent! I got it working now. Now for the final step... is there a way to detect when the application is closed that I could use to exit the loop and end the batch file? – ginahoy Apr 30 '18 at 03:18

1 Answers1

0

As far as I have understood the requirements for the task the code to use in batch file %ProgramFiles%\CorelDRAW X4\Programs\CorelFile.bat is:

@echo off
if "%~1" == "" goto :EOF
"%ProgramFiles%\CorelDRAW X4\Programs\CorelDRW.exe" %*
for %%I in (%*) do if exist %%I copy /Y "%%~I" "%%~dpnI.sav" >nul

This batch file must be associated with file extension .cdr for example by importing following registry file on Windows XP and later Windows versions with administrator privileges:

REGEDIT4

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.cdr]
@="CorelDrawFile"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CorelDrawFile]
@="Corel Draw Image"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CorelDrawFile\DefaultIcon]
@=hex(2):22,25,50,72,6f,67,72,61,6d,46,69,6c,65,73,25,5c,43,6f,72,65,6c,44,52,\
  41,57,20,58,34,5c,50,72,6f,67,72,61,6d,73,5c,43,6f,72,65,6c,44,52,57,2e,65,\
  78,65,22,2c,30,00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CorelDrawFile\shell\open\command]
@=hex(2):22,25,50,72,6f,67,72,61,6d,46,69,6c,65,73,25,5c,43,6f,72,65,6c,44,52,\
  41,57,20,58,34,5c,50,72,6f,67,72,61,6d,73,5c,43,6f,72,65,6c,46,69,6c,65,2e,\
  62,61,74,22,20,22,25,31,22,00

It is also possible to register file extension .cdr with absolute paths with REG_SZ instead of using REG_EXPAND_SZ and %ProgramFiles% in paths.

REGEDIT4

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.cdr]
@="CorelDrawFile"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CorelDrawFile]
@="Corel Draw Image"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CorelDrawFile\DefaultIcon]
@="\"C:\\Program Files\\CorelDRAW X4\\Programs\\CorelDRW.exe\",0"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CorelDrawFile\shell\open\command]
@="\"C:\\Program Files\\CorelDRAW X4\\Programs\\CorelFile.bat\" \"%1\""

Windows Explorer calls for each selected *.cdr file the batch file on using context menu Open respectively on double clicking a single *.cdr file.

The batch file starts Corel Draw with all the arguments passed to the batch file passing to Corel Draw. This is usually just the file name of the *.cdr file with full path and file extension enclosed in double quotes.

After Corel Draw terminated, the batch file checks for existence of each file specified as command line argument and copies the file with same name in same directory with different file extension .sav.

The batch file is designed for being started with multiple *.cdr file names specified as arguments on command line. I don't know if Corel Draw supports multiple *.cdr files being specified on command line.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /? ... explains %* (batch file) and %1 (Windows registry).
  • copy /?
  • echo /?
  • for /?
  • if /?
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • Thanks, @Mofi. That's a lot to absorb. It may be a few days before I find time to review (just broke ground on a new house, sorta up to my rear end in alligators at the moment! =:-o – ginahoy May 01 '18 at 01:30