1

I have created a txt file with the following commands.

cd.. cd <Folder location> del *_extract.txt /Q 

Now, I want to create a batch file that will open cmd and execute the commands mentioned in the text file. The batch file I have created has the following content

@echo off start "clear" cmd.exe -m <textfilelocation> 
1
  • 3
    Why don't you just rename your file.txt to file.bat? Commented Oct 12, 2020 at 7:22

2 Answers 2

1

The simplest way is as Stephan Said in the comment. Rename your file from .txt to .cmd or .bat. Then you can run it on its own, or simply call it from your other batch file as:

call "C:\Some Location\file.cmd" 

If you really want to retain the txt file, then run a for loop:

@echo off for /f "usebackq delims=" %%i in ("C:\Some path\file.txt") do %%i 

Considering a text file with comments mayb, in this case using # (which is a non batch file standard) we can execute the lines that are not comments.

@echo off for /f "delims=" %%i in ('type "C:\Some Path\file.txt" ^| findstr /v "#"') do %%i 
Sign up to request clarification or add additional context in comments.

Comments

1

The best option is to rename the file to .bat or .cmd (or make a (temporary) copy).

I for some reason it just has to be a .txt file, your options are very limited.

Besides Gerhards suggestion (the one with the for loop), a maybe better option is to "feed" the lines of the textfile to cmd (not recommended; provided just for academic reasons):

<script.txt %comspec% 

or without command repetition and header:

<script.txt %comspec% /q /k 

NOTES:

  • the lines in the script.txt have to be command-line syntax (for example for %a ..., not batch-file syntax for %%a ...)
  • GOTO and CALL :label won't work (they don't work on command-line)
  • a "multiline" for command works, but will clutter your output with a More? prompt per line (not suppressable).
  • if delayed expansion is needed, you need to add the /v:on switch to the cmd command (setlocal * has no effect on the command line)

Example-script.txt:

echo off echo/ :start [ignored] echo hello %username% ping -n 1 www.google.com | find "TTL=" for %a in (alpha beta) do ( echo %a ) goto :start [ignored] echo done. 

Output:

hello Stephan Antwort von 172.217.21.206: Bytes=32 Zeit=12ms TTL=121 Mehr? Mehr? alpha beta done. 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.