Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Create strings using echo commands  (Read 45617 times)

0 Members and 1 Guest are viewing this topic.

Teroro

    Topic Starter


    Greenhorn

    • Experience: Beginner
    • OS: Unknown
    Create strings using echo commands
    « on: October 27, 2021, 03:44:35 AM »
    So, I started messing around with a .bat file and started learning how to use variables, user input, show text using the commands: echo,set,etc.
    And I noticed I can create files with the command: echo > (File name) Which was really intresting but I ran into a problem.
    When creating a file you can do: echo "Text in it" > (File name) and I tried putting a CPP script in it but it doesn't seem to work. Can anyone tell me why?

    I tried putting it into a string:
    Code: [Select]
    set Code=#include<fstream>usingnamespacestd;ifstreamfin("");ofstreamfout("");int main(){inta,b;fin>>a>>b;fout<<a*b;I tried directly applying it:
    Code: [Select]
    echo #include<fstream>usingnamespacestd;ifstreamfin("");ofstreamfout("");int main(){inta,b;fin>>a>>b;fout<<a*b; > "%CPPFileName%"
    Can anyone help me with this?
    Heres the full code:
    Code: [Select]
    @echo off
    echo In File name:
    set /p File1Name=
    echo 2 Variables:
    set /p Variables=
    echo %Variables% > "%File1Name%"
    echo Out File name:
    set /p File2Name=
    echo 0 > "%File2Name%"
    echo CPP File name:
    set /p CPPFileName=
    set Code=#include<fstream>usingnamespacestd;ifstreamfin("");ofstreamfout("");int main(){inta,b;fin>>a>>b;fout<<a*b;
    echo #include<fstream>usingnamespacestd;ifstreamfin("");ofstreamfout("");int main(){inta,b;fin>>a>>b;fout<<a*b; > "%CPPFileName%"

    Squashman



      Specialist
    • Thanked: 134
    • Experience: Experienced
    • OS: Other
    Re: Create strings using echo commands
    « Reply #1 on: October 27, 2021, 03:47:04 PM »
    The obvious culprit should be the >.  This character is a special character to tell the script to redirect the output to the file. Otherwise known as Standard Output.  The < is also a special character as it is used for Standard Input.  Any special character that you need to use Literally, needs to be escaped by the ^ character.
    example
    Code: [Select]
    echo #include^<fstream^>usingnamespacestd;ifstreamfin(""); >"%CPPFileName%"

    Teroro

      Topic Starter


      Greenhorn

      • Experience: Beginner
      • OS: Unknown
      Re: Create strings using echo commands
      « Reply #2 on: October 28, 2021, 01:41:49 PM »
      I tried but it still doesn't create my file...

      Code: [Select]
      @echo off
      echo In File name:
      set /p File1Name=
      echo 2 Variables:
      set /p Variables=
      echo %Variables% > "%File1Name%"
      echo Out File name:
      set /p File2Name=
      echo 0 > "%File2Name%"
      echo CPP File name:
      set /p CPPFileName=
      echo #include^<fstream^>usingnamespacestd;ifstreamfin("%File1Name%");ofstreamfout("%File2Name%");int main(){inta,b;fin>>a>>b;fout<<a*b;} > "%CPPFileName%"

      Teroro

        Topic Starter


        Greenhorn

        • Experience: Beginner
        • OS: Unknown
        Re: Create strings using echo commands
        « Reply #3 on: October 28, 2021, 01:47:26 PM »
        It worked omg.
        Thank you very much!