Run text file as command (cmd)

Gotruc Posted messages 17 Status Member -  
Gotruc Posted messages 17 Status Member -
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. dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 660
     
    bonjour
    copy fichier.txt fichier.bat & fichier
    3
  2. kxgeneration Posted messages 737 Registration date   Status Member Last intervention   39
     
    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. Gotruc Posted messages 17 Status Member 1
       
      Yes, I know, but would it be possible to do it without having to create a batch file?
      0
    2. kxgeneration Posted messages 737 Registration date   Status Member Last intervention   39
       
      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. Gotruc Posted messages 17 Status Member 1
       
      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