3

Imagine having text file with custom (not reserved) extension

myFileSayHello.myExtension

Which contains for example:

@echo on
@echo "Hello"
@pause

How can I run it using cmd as bat file?

cd %pathToFile%    
start myFileSayHello.myExtension

Code above does not working and explorer asking to select program to open .myExtension.

How can I do that .myExtension will behave like .bat inside cmd ?

t4dohx
  • 675
  • 4
  • 24
  • 1
    `cmd /k < Anyfile.ext` –  Jan 02 '17 at 19:12
  • @Noodles, I guess this is going to fail for several commands (like `for`), because the `.ext` file is not parsed as a batch file but as a typed command lines... – aschipfl Jan 02 '17 at 19:22
  • Yet is can do everything a batch file can except for %errorlevel%. I don't use batch files. I copy and paste lines. –  Jan 02 '17 at 19:24
  • No, it can't -- see the [`for` variable references](http://ss64.com/nt/for.html) with one or two `%` signs for `cmd` and batch files, respectively; or `setlocal`/`endlocal`, which will be totally ignored, or `exit /B`, labels... – aschipfl Jan 02 '17 at 19:37
  • I can confirm that it does what I except. Can you, Noodles, post an answer that I can mark as working for others. Thanks. – t4dohx Jan 02 '17 at 19:40
  • 1
    @aschipfl: At [this answer](http://stackoverflow.com/questions/13320578/how-to-run-batch-script-with-out-using-bat-extention/13337597#13337597) there is a detailed description of the differences that happen in a text file executed this way vs. a real .BATch file. – Aacini Jan 02 '17 at 22:27

1 Answers1

2

Here is a way in the command prompt that requires administrative privileges:

assoc .myExtension=myExtensionfile
ftype myExtensionfile="%1" %*
set "PATHEXT=%PATHEXT%;.myExtension"
setx PATHEXT "%PATHEXT%;.myExtension"

cd /D "%pathToFile%"

myFileSayHello

The modified PATHEXT is stored for the current user's context, unless you append /M to the setx command line.

aschipfl
  • 33,626
  • 12
  • 54
  • 99