Running an extension file without a bit as a batch file

Say I have a text file, it contains command commands. How can I run this text file as a batch file from one, without renaming it. I also want to keep the portable aspect, so no registry keys, etc.

The reason for the lack of renaming is the excessive prevention of remaining unnamed files when closing unexpectedly.

+2
source share
5 answers
type some.txt>temp.bat
call temp.bat
del /q /f temp.bat 

Does the temp file create a hoax? It is not mentioned as a limitation in the question. Although you may lose %ERRORLEVEL%due to the del command, you can save it in the temp variable:

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

, , . Windows . .bat .cmd.

FOR/F, . , IF, FOR, GOTO Label CALL :Label. , . :

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

, IF / FOR, CMD.EXE, , SET .

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

:

cmd < file.txt

, , , GOTO, SETLOCAL . if for , (, ).

, , (@echo off ), NUL, "Batch" CON. , 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

:

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
+3

. Windows

  • assoc. (, *.txt) .
  • ftype. , Windows, .

assoc /? ftype /?, . google-fu MS.

*.bat batfile; *.cmd cmdfile. 7 . *.foobar , :

assoc .foobar=cmdfile

, , " .foobar",

c:\> sillyness

sillyness.foobar . Windows (.com vs .cmd vs .bat ..)

-

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

perl, .bat .

+1

If your command commands are simple sequential, you can use this on the command line. Double characters %for use in a batch file.

for /f "delims=" %a in (file.txt) do %a
0
source

All Articles