Windows: set file association to batch file

I created a custom file extension that I would associate with the script package. I used

ASSOC .myext=MY.FILETYPE FTYPE MY.FILETYPE=cmd /c "C:\Path\of\my\batch.bat" %1 %* 

now the batch file "C: \ Path \ of \ my \ batch.bat" is a simple single line

 echo %1 

And it works roughly: double-clicking on the .myext file calls the cmd shell, repeating the path to the file.
But the problem occurs when the .myext file is in a path containing spaces: the echo path to the file is truncated into space.
Double quoting% 1 in the FTYPE statement does not seem to work.

 FTYPE MY.FILETYPE=cmd /c "C:\Path\of\my\batch.bat" "%1" %* 
+5
source share
3 answers

The double quote %1 correct, but it fails because cmd.exe contains an error when the command and at least one parameter contain quotation marks.
Therefore, you need to make the command without quotes by inserting CALL .

 FTYPE MY.FILETYPE=cmd /c call "C:\Path\of\my\batch.bat" "%1" %* 
+5
source

Change the contents of the batch file "C:\Path\of\my\batch.bat" to

 echo %* 

Your ASSOC and FTYPE seem correct.

Change as per Monacraft comment.

This is the correct solution, since %1 will refer to the document file name, while %* will refer to additional parameters: if any additional parameters are required by the application, they can be transferred as %2 , %3 . To transfer all parameters to the application, use %* .

It is convenient to use aFile.myext abc directly from the command line, although to do this, use the FTYPE statement

FTYPE MY.FILETYPE = cmd / D / C "C: \ Path \ of \ my \ batch.bat"% 1 ""% *

to distinguish the first parameter if it contains spaces.

Example : with

 ASSOC .xxx=XXXFILE rem a bug here FTYPE XXXFILE=%ComSpec% /D /C "d:\bat\xxxbatch.bat "%1"" %* rem definitely switched to Jeb solution as follows FTYPE XXXFILE=%comspec% /D /C call "d:\bat\xxxbatch.bat" "%1" %* 

and xxxbatch.bat as follows

 @echo( @echo %* @if "%2"=="" pause @goto :eof 

Exit

 d:\bat>D:\test\xxxFileNoSpaces.xxx aa bb cc "D:\test\xxxFileNoSpaces.xxx" aa bb cc d:\bat>"D:\test\xxx file with spaces.xxx" dd ee "D:\test\xxx file with spaces.xxx" dd ee d:\bat> 
+2
source

if you use this from bat file, try changing it to:

 ASSOC .myext=MY.FILETYPE FTYPE MY.FILETYPE=cmd /c "C:\Path\of\my\batch.bat" "%%1" %%* 

I think even

  FTYPE MY.FILETYPE="C:\Path\of\my\batch.bat" "%%1" %%* 

must work.

+1
source

Source: https://habr.com/ru/post/1212092/


All Articles