Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
724 views
in Technique[技术] by (71.8m points)

windows - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...