Question

Let's say I have a text file, it contains batch commands. How can I run that text file as a batch file from within one, without renaming it. I want too keep a portable aspect too it, so no registry keys or such.

The reason for no renaming is too prevent leftover unrenamed files upon unexpected closure.

Was it helpful?

Solution 2

type some.txt>temp.bat
call temp.bat
del /q /f temp.bat 

Is creating a temp file cheating?It's not mentioned as restriction in the question.Though you can loose the %ERRORLEVEL% because of the del command , but you can keep it in temp variable:

type some.txt>temp.bat
call temp.bat
set temp_el=%errorlevel%
del /q /f temp.bat
exit /b %temp_el% 

OTHER TIPS

The simplest way is this:

cmd < file.txt

As in the previous answers, there are several commands that will not work in this file, like GOTO, SETLOCAL and others. However, multiline nested if and for commands do work as long as for replaceable parameters use just one percent (like in the command-line).

Although this method alaways show in the screen the executed commands (@echo off not works here), you may redirect the output to NUL and in the "Batch" file redirect the desired output to CON. For example, this is test.txt:

@echo off
echo Hello World! > CON

(for /L %a in (1,1,10) do (
   echo Turn: %a
   if %a equ 4 echo TURN NUMBER FOUR!
)) > CON

Output example:

C:\> cmd < test.txt > NUL
Hello World!
Turn: 1
Turn: 2
Turn: 3
Turn: 4
TURN NUMBER FOUR!
Turn: 5
Turn: 6
Turn: 7
Turn: 8
Turn: 9
Turn: 10

I'm pretty sure you cannot do what you want. Windows will not let you configure the OS to recognize any other extensions as batch files. Only .bat and .cmd are supported.

You could process a series of simple commands within a text file using a FOR /F loop, but it will be very restrictive. For example, it will not support IF, FOR, GOTO Label, or CALL :Label. There are probably other restrictions. Within your main batch file, you could have the following:

for /f delims^=^ eol^= %%A in (script.txt) do %%A

You might be able to support IF and/or FOR if you execute the command via a new CMD.EXE shell, but then you cannot preserve the value of variables that might be SET by the command.

for /f delims^=^ eol^= %%A in (script.txt) do cmd /c "%%A"

See the windows shell commands

  • assoc. Associates a filename extension (e.g. *.txt) with a file type.
  • ftype. Lets you create a new file type that tells the windows shell how to open a particular kind of file.

From a command prompt, typing assoc /? or ftype /? will get you help on them. Or use your google-fu to find the MS docs.

*.bat is mapped to the file type batfile; *.cmd is mapped to the file type cmdfile. In windows 7 they are identical. If you want to be able to run files named *.foobar as a batch files, just type:

assoc .foobar=cmdfile

Then, assuming a file named 'sillyness.foobar' existing on the path, you just just type

c:\> sillyness

and it will find sillyness.foobar and execute it as a batch file. The Windows shell has a priority for how it resolves conflicts when you have files with the same name and different extensions (.com vs .cmd vs .bat, etc.)

Something like

assoc .pl=perlscript
ftype perlscript=perl.exe %1 %*

will set you up to run perl scripts as if they were .bat files.

If your batch commands are simple sequential ones then you could use this at the command prompt. Double the % signs for use in a batch file.

for /f "delims=" %a in (file.txt) do %a
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top