Run text file as command (cmd)

Hello,

I don't know if this question has already been asked, but since I couldn't find an answer, I'm asking my question here.

I would like to know how to execute the contents of a text file as a command.

I know that you can do this by entering the content of the file as a variable and then executing it afterwards:
set /p command= <"text file"
%command%

But is there a simpler way?

3 answers

  1. Contributor
    bonjour
    copy fichier.txt fichier.bat & fichier
    3
    1. Change simply the extension of your .txt file to .bat, double click on it and it will run like a charm :)

      .txt => .bat
      0
      1. Yes, I know, but would it be possible to do it without having to create a batch file?
        0
      2. It's impossible to execute a .txt file as such!!!!
        After that, you can possibly create a PowerShell script that does the same thing and compile it into an .exe, the same goes for C#.

        What code do you actually want to execute? Post it, and I'll see if similar functions exist in PS.
        0
      3. I don't really have any example code or usage, but I wasn't thinking of executing the txt file itself (I know that's impossible) but executing its content. However, according to the responses in this topic, it seems impossible (except for the solution I mentioned in my question).
        0