Hey Guys, this is Master Jake. I’m going to be showing you guys another batch file tutorial today and this is going to be on how to write the files.
Just go ahead and open up your text entry whatever you’re using. I’m going to be using notepad. And I hope you watched my other tutorials because I’m not going to explain other stuff in this tutorial that I already have in the past basically. It’s going to be a really simple tutorial and small so let’s go and start it. We’re just going to start the basic batch file and title it Write To File. And then I’m going to go ahead and set it up. Okay.
So, we have this line, it says echo and you used echo statements in the past before and you’re just echoing lines out into the batch program but what you don’t know is that if you put the greater than sign, a double quote. And then the file that you want to echo into, it will actually place this whatever is after the echo and for the greater than sign. It will place inside the file. So, I’m going to go ahead and save this real quick, you can choose Ctrl+S file save whatever. We’re going to name this writetofile.bat. Save as type, change to All Files, make sure .bat is on the end of the filename, click Save. So, this file pops up called writetofile, we’re going to double click it, opens, closes and this new file called test.txt pops up. We’re going to open it and it says Hello World inside.
So now, we’re going to learn how this happens. Yo go up here and you see it says hello world right here. We’re echoing Hello World and then it’s greater than sign, it’s kind of like an arrow pointing towards the file that you went into, double quote and then the filename and you make sure to include the extension. The extension is very important if you put .bat or something else in there to a batch file or something.
Let’s say you stopped now and you just try to go and make your own program. Let’s say you do this is the first line, echo, this is the second line, you save it, you open it and then you look inside. You’ll notice that it only said this is the second line. You’re like, “What the crap, why they did that?” Well, I’m going to show why right now.
When you’re doing a single greater than sign, when it’s going down the line, the very last one it finds in the program, it’s going to put that into that file. I mean if these were two different files, it would echo into different but since it’s same file it’s going to be using this one because it’s the bottom. And it echoes that in originally but when it gets down on this one, it echoes it back in on the first file because this same greater than sign is just saying to put on the first line, don’t worry about anything else. So to change that, every line after the first one you need to use a double quote, double greater than sign. So, put a double on there, echo this is the third line, double test .txt. Save this. Double click it. Look inside.
And I see this is the first, this is the second, this is the third line. So, you’ve just learned how to write to files in batch files. And remember you can use any extension. I’m going to go ahead and show you one more thing. Before I go, I don’t know how much time I have left. We’re actually going to write a batch file in a batch file. So, we’re going to echo, echo off into hello.bat then we’re going to echo, echo hello world and double into hello.bat. And we’re going to save this file, double click. You’ll notice a new file called hello.bat pops up. We’ll double click it. See, it says hello world, press any key to continue. Press the key it goes—if you’re going to edit the batch file, you’ll notice that our code is in there.
So, that’s actually how you can write through batch files. You can also write to many other files. So now that you know how to do this, make sure knowledge and everything you know, if statements, loops, labels, everything and you can make some really exciting programs using writing the files technology, make installers, anything you want. Okay, tune in with me next time. I’m going to be showing you how to read from a file. That’s it for this tutorial. Thank you for watching. Please visit my site at masterjake.com, alright. It might be under construction but it will e up back soon. Okay. So, thanks for watching again. See you later guys.
Transcription by:
Scribe4you Transcription Services