Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command Prompt: Execute Commands from Any Text File (Not Having ".bat" or ".cmd" Extensions)

I can't find any way to do, for example, the following:

cmd.exe /C "script.txt"

In other words, I need Command Prompt to (try) to execute file with any extension (not necessarily .bat or .cmd) if it contains valid batch script code. I'm looking for behavior similar to Unix shells:

./script.txt

While on Unix the shebang (#!/bin/sh) is responsible for understanding that the file is actually a script, it seems like on Windows .bat or .cmd extensions play the same role, indicating a batch script file for Command Prompt.

Is it possible to avoid that and force Command Prompt to interpret a file with any name?

NOTE: Please, no answers like:

Give your file .bat or .cmd extension.

That's not what the question is about.

like image 444
Alexander Shukaev Avatar asked Jan 10 '23 18:01

Alexander Shukaev


1 Answers

This depends on the complexity of the NON-Batch file. If the NON-Batch file does not use these facilities:

  • Access to Batch file parameters via %1 %2 ... and execution of SHIFT command.
  • Execution of GOTO command.
  • Execution of CALL :NAME command (internal subroutine).
  • Execution of SETLOCAL/ENDLOCAL commands.

then you may execute any file as a "Batch file" via this trick:

cmd < anyFile.ext

Further details at this post

like image 141
Aacini Avatar answered Jan 30 '23 23:01

Aacini