1

Context: I have associated .batch extension to be opened by my text editor by default when double-clicking or hitting ENTER on it in the Explorer (default action for "Open").

(I probably can't do this directly for .bat files and it's probably not something we want, it could break other things).

Example:

helloworld.batch
================
dir
md hello
cd hello

How to run this file from command line as if it were a .bat or .cmd?

Doing call helloworld.batch opens it in my editor, doing helloworld.batch in the command line does the same; idem for start helloworld.batch. How to actually run it?


PS: why do I want to achieve this? Let's compare these situations:

  • Before: Double click on test.bat, run it. Oh no it hasn't worked as expected, and the window is now closed and I haven't seen the error (it went too fast)... I should have added pause at the end. Let's open it in the text editor. I edit it. I save, I close the editor. Let's double click on test.bat again...

  • After: Double-click on test.batch, it opens in the text editor. CTRL+B, it runs and I see the output in the bottom panel of my editor. If there's something to copy/paste, I can (not possible in cmd.exe by default even if I found a way). If there's something to modify in the batch I'm already in the editor... Life is good :) –

  • Have you tried Start helloworld.batch? – spikey_richie Jun 4 at 9:37
  • @spikey_richie Yes it's the same than call. – Basj Jun 4 at 9:45
  • note that you usually run a batch file more often than you write it, so it makes more sense to set the default action to execute instead of exit – phuclv Jun 4 at 10:04
  • @phuclv In my case, I rarely execute a .bat or .batch from Explorer, I always open it in the text editor and CTRL+B to run it, and have the console display the results. – Basj Jun 4 at 10:33
1

Simple solution:

cmd < file.batch

(An end-of-line is required at the end of the last text-line in the file.)

| improve this answer | |
  • 1
    I can't for the life of me understand what the OP is trying to achieve: a .bat or .batch file is a command file, it's meant to execute commands in CLI, not to have its content displayed in a text editor. You can always right-click on it and modify it if you don't get the results you expected, but once you get it to work properly, why bother to open it in a text editor and press Ctrl-B, wait for it to execute, then close the CLI window, THEN the text editor's? I must be missing something.. – user1019780 Jun 4 at 10:49
  • @Didier: I would guess he is using .batch while developing the script, then renaming to .bat when finished. – harrymc Jun 4 at 10:59
  • @Didier I added the reason at the end of my question. – Basj Jun 4 at 12:20
  • Yes, I understand that, but a .bat file is supposed to be run, period. If you are going to develop using .bat files, do so, it's no skin off my back, but that's not the purpose of a .bat script in the first place. Once your script is done just so, and running to your satisfaction, you "should" (again, you do what you want) just run it, not open it in a text editor then run it with Ctrl-B, then close both the CLI and text editor windows. It's a waste of time, plus some formatting issues can come in the way when you do that. Same as with AppleScript: write it, test it, run it, forget it. – user1019780 Jun 4 at 13:37
  • 1
    @Didier: Such discussions shouldn't be here. Anyway, the poster has his own tools and methods. – harrymc Jun 4 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.