5

Im trying to find a command or a script that can do commands from a specified .txt file. e.g it would treat a .txt file as a batch file but not necessarily rename it to .bat.

Can anyone help?

e.g:

cmd "filename.txt"
do cmd

That would run filename.txt as a .bat file and do any commands in it.

5
7

I'm trying to find a command or a script that can run commands from a .txt file

The simplest solutions are always the best.

Use the following command:

cmd < command.txt

Or:

type command.txt|cmd

Source StackOverflow answer How to DIRECTLY execute command in txt file in cmd? by MC ND and it's comments.

Note:

There are several commands that will not work in this file, like goto, setlocal and others. However, multi-line nested if and for commands do work as long as for replaceable parameters use just one percent (like in the command-line).

Source StackOverflow answer Running a non bat extension file as a batch file by Aacini

See StackOverflow answer How to run batch script without using *.bat extension, also by by Aacini, for more observations on this approach. This answer also includes a link to code that can get goto and call working as well!


Further Reading

7

Run a Text File as a Batch Script

Yes this is possible by making the text file which contains the commands you need executed to be referenced as the first batch argument that is passed to the batch script.

You can set the argument value as a variable and using type you can create a temporary batch file with the content of the text file and then run that file through cmd to process accordingly.


Batch Script Logic

@ECHO ON

SET cmdtxt=%~1
SET cName=%~N1

if exist "%tmp%\%cName%.cmd" del /q /f "%tmp%\%cName%.cmd"
if not exist "%tmp%\%cName%.cmd" type "%cmdtxt%" >> "%tmp%\%cName%.cmd"
cmd /k "%tmp%\%cName%.cmd"

pause
exit

Note: You can use cmd /c to have the command window(s) close after execution.


Text File

ECHO Line 1
dir *.txt

Note: This is the text file without a .bat or .cmd which each line will contain a valid batch command which will be executed.


Execute this by using either. . .

1.Drag and Drop

enter image description here enter image description here


2. Command Line

C:\Users\User\Desktop\RunMe\RunTextCommands.cmd "C:\Users\User\Desktop\RunMe\cmd.txt"

@ECHO OFF Results

enter image description here


Further Resources

  • Command Line arguments (Parameters)

    %* in a batch script refers to all the arguments (e.g. %1 %2 %3 %4 %5 ...%255) only arguments %1 to %9 can be referenced by number.

  • If

  • Type
  • Cmd

  • FOR /?

    In addition, substitution of FOR variable references has been enhanced. You can now use the following optional syntax:

    %~nI        - expands %I to a file name only
    
0
3

Run Individual Lines from a Text File as Batch Commands

Yes this is possible by making the text file which contains the commands you need executed to be referenced as the first batch argument that is passed to the batch script.

You can then run the file through a for /f loop and iterate over the content of each line individually and pass each [command] value to cmd /c to execute accordingly.


Batch Script Logic

@ECHO ON

SET cmdtxt=%~1

FOR /F "USEBACKQ TOKENS=*" %%A IN ("%cmdtxt%") do (
    cmd /c "%%~A"
    )
::PAUSE
EXIT 

Text File

ECHO Line 1
dir *.txt

Note: This is the text file without a .bat or .cmd which each line will contain a valid batch command which will be executed.


Execute this by using either. . .

1.Drag and Drop

enter image description here enter image description here


2. Command Line

C:\Users\User\Desktop\RunMe\RunTextCommands.cmd "C:\Users\User\Desktop\RunMe\cmd.txt"

@ECHO OFF Results

enter image description here


Further Resources

2
  • Hmm. This will only work with a very simplified subset of commands. For example, any command that is over multiple lines, or requires interpretation by a cmd shell, will not work. – DavidPostill Jun 16 '18 at 17:49
  • Yes, the OP should test the commands that are on each line as requested to iterate and run separately and individually for the needs as setup in the environment, etc. – Drink More Pimp Juice IT Jun 18 '18 at 4:33

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.