0

I have a text file, and I want to read in the entire file to a variable and then execute it. I then want to clear the variable contents and the file contents.

for /f "delims=" %%A in ('type command.txt') do set "cmd=%%A" 
%cmd%
set "cmd=%cmd*=%"
break>command.txt

this does not work, %cmd% returns ECHO is off meaning it is empty.

3
  • stackoverflow.com/a/15818723/2128947 should "execute" your text file.
    – Magoo
    Commented Apr 12 at 5:51
  • 2
    There is no echo in your code, meaning echo is off originates from executing %cmd% (it can't be empty, else you wouldn't get a response at all). Replace %cmd% with set cmd to verify the contents of %cmd%
    – Stephan
    Commented Apr 12 at 6:53
  • Perhaps this is not what you are looking for, but I suggest you to carefully review this answer
    – Aacini
    Commented Apr 12 at 21:47

2 Answers 2

0

If you read the file like you did, you might get problems with some poison chars. They can be escaped, but it isn't trivial.

(I would use .cmd instead of .txt in the first place, but you might have a reason for that, like the file gets generated by something else, you haven't full control over).

This is how I would do it (straight forward and very simple):

ren command.txt command.cmd
call command.cmd
del command.cmd

Pros:

  • no escaping needed
  • multi-line scripting possible (take advantage of full batch capabilities)
2
  • thanks! this works, but isn't quite what I needed. I wanted to keep the file as .txt and preserve it, so I found this method that worked: FOR /f "delims=" %%i IN (command.txt) DO %%i break>command.txt Commented Apr 12 at 9:56
  • copy instead of ren and break>command.txt instead of del?
    – Stephan
    Commented Apr 12 at 12:17
-1

[Edited]

Based on what I understand, you want to execute a command written in a file, store that command in a variable, and then execute it. Let's assume the file contains this command:

commad.txt:

cmd.exe /c "for /l %i in (1 1 10) do @echo %i"

and the script:

@echo off
    set "command="
    set "args="

    for /f "tokens=1,*" %%i in (command.txt) do (
        set "command=%%i"
        set "args=%%j"
    )
    
    REM Check if command was found.
    if defined command (
        REM execute command.
        %command% %args%
        REM Destroy file contents.
        type nul >command.txt
    ) else (
        echo command not found.
        exit /b 1
    )

exit /b 0

1. Set variables command and args as empty.

2. Read the first word from command.txt as command, the rest as args.

3. If a command is found, run it with its arguments.

4. Destroy the file contents.

5. If no command, show an error message and exit with exit code 1.

3
  • This doesn't work with the backslash in tokens=1,\*. With that removed it works with only one line=command in the file, a limitation not stated in the Q, except that the file is not then emptied but rather set to space space backslash space CRLF. Commented Apr 12 at 10:35
  • I wrote this code from scratch and there might have been some errors, but regarding ::, you can use it instead of REM. I have now modified the code and it works fine for me. Also, regarding \, this was a mistake that happened while I was formatting the code.
    – Dahy Allam
    Commented Apr 12 at 16:27
  • You can try it, and tell me.
    – Dahy Allam
    Commented Apr 12 at 16:28

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.