Batch command/script to do commands from .txt file?
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.
Solution 1:
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 nestedif
andfor
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
- An A-Z Index of the Windows CMD command line
- A categorized list of Windows CMD commands
- redirection - Redirection operators.
- type - Display the contents of one or more text files.
Solution 2:
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
2. Command Line
C:\Users\User\Desktop\RunMe\RunTextCommands.cmd "C:\Users\User\Desktop\RunMe\cmd.txt"
@ECHO OFF
Results
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
Solution 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
2. Command Line
C:\Users\User\Desktop\RunMe\RunTextCommands.cmd "C:\Users\User\Desktop\RunMe\cmd.txt"
@ECHO OFF
Results
Further Resources
- For /F
- Cmd