1.

Solve : Create strings using echo commands?

Answer»

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%"
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%"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%"
It worked omg.
Thank you very much!



Discussion

No Comment Found