This is Hacker Public Radio Episode 3700 for Friday the 7th of October 2022. Today's show is entitled Introduction to Batch Files. It is part of the series, Dawes. It is hosted by Aloka and is about 15 minutes long. It carries a clean flag. The summary is more on Dawes. This time is Introduction to Batch Files. Hello, this is Ahuka, welcoming you to Hacker Public Radio and another exciting episode in our ongoing Dawes series and what I want to do today is begin taking a look at Batch Files. It's an important concept. Now a Batch File is a file that contains a number of Dawes commands, each of which you could run individually from the command prompt. By putting them into a Batch File, they can be run as a group by simply running the Batch File. Note that the commands execute in the order they appear in the Batch File and that anything that causes a command to halt will also halt the Batch File. You create a Batch File by using an ASCII text editor, such as the Dawes edit, which we had looked at previously in preparation for all of this stuff. Or you can do it in if you run Windows with Notepad. When you have created the Batch File, you save it with a file name and give it the extension dot BAT. Note that you must not use a name that is the same as any Dawes commands or any other program or utility or likely to run. If you use a Dawes command name, trying to run your Batch File will not work because the Dawes command will execute first. If your name matches some other program or utility, you may never be able to run that program again because your Batch File will run before the program runs. So pick something that is not likely to match any command name or program file name. Virtually all internal and external commands can be used in a Batch File. The few exceptions are the commands that are intended only for configuration, which are used in the configs as File. For example, of these include buffers, country, device and so on. Now when you create a Batch File, you are beginning to write a program basically, Dawes, Batch Files may not have the power of a structured programming language, but they can be very handy for quick tasks. Because this is a form of programming, let us begin with learning some good habits. The number one good habit for any program or to learn is to put comments in the program that explain what the program is doing. This is a very good thing to do. But you need to be careful not to fool the operating system into trying to execute your comments. The way to avoid this is to place REM, short for remark, at the beginning of a comment line. The OS will then ignore that line entirely when it executes the program. But anyone who looks at the source code in the Batch File can read your comments and understand what it is doing. This is also a way to temporarily disable a command without deleting it. Just open your Batch File for editing, place the REM at the beginning of the line you want to disable. When you want to reenable that command, just open the file for editing and remove the REM, and the command will resume functioning. This technique is sometimes referred to as remarking out or commenting out a command. Now from this description, you may think the sounds familiar. In the UNIX world, this is simpler to things like a Batch script, which is very much the same kind of thing, a series of commands that you can execute, and they will execute in order. Now, our DOS Batch Files is good as Batch scripts. Well, they don't have all of the power that a Batch script has. And of course, Batch is only one of a number of shells that you could be operating. It's the one that I use. It's most common one. Well, I'm not really qualified to comment on, you know, z-shell or z-shell or what have you, because I don't use any of those. I'm bash is the one I'm used to. But there's nothing new under the sun, in other words. Now, Batch Files can save time. Many years ago, I was at a technology conference for college professors. I was a college professor at the time, and faculty development officer at the college I was at. At the end of the conference, we needed to quickly make about 40 floppy disks, each with an identical set of about 15 vials. Well, there are various ways of doing this, such as using discopy or copy commands. But I wanted to do this as quickly and efficiently as possible. So I sat down with the computer, which was running DOS, and quickly copied the 15 files into a temporary directory on the hard drive. I could have then opened edit, but to be even faster, I entered it directly from the console. And that's one of the things you can do is you can type right at the console, and the command I used was C, colon, backslash, right at the prompt, the C, colon, prompt, backslash, copy, con, copy, space, con, space, one dot, B, A, T, space, copy, space, C, colon, backslash, temp, backslash, star dot, star, space, A, colon, space, control Z. So what's this all about? Copy, space, con, that is saying, I'm about to type something into this computer, and I want you to copy whatever I type into a file. Then one dot, that's the name of the file, all right? Then another copy. Now this is a command that's going to be in the file, and what is the command? Copy, space, C, colon, backslash, temp, backslash, star dot, star. S and copy, everything in the temp directory. Where do we copy it? Space, a, colon, copy it to the A drive. Control, Z, I'm done, end copying into this file. And then back comes a response from the computer. It says one file, parentheses, S, closed parentheses, copied. All right, so we copy from the console. We store it in a file called one dot, that. The console in this case just means the keyboard. Kind of, it's just taking whatever I type and entering it in a file. And the second line is the single command in my batch file. It copies all of the files in the directory, C, colon, backslash, temp, to the floppy disk in the A drive. Third line is holding the control key while typing control Z. This is the end of file marker. And tells the OS, I am through entering text into this file. The fourth line is the response from the operating system, saying, OK, boss, we copied your file. Now, I could then view this file in several ways. I could use the type command, the type command will cause it does to open a file and display the contents on the screen. It's typing it out for you. I could open it in edit. And if I did, I would see the single line, copy, space, c, colon, backslash, temp, backslash, start.star, space, a colon. Now, if I opened it in edit, I could then add more commands, or whatever I wanted to do with it. But in this case, I didn't want to do anything else. Now, once I had created this file, all I had to do was feed in a floppy, press the one key, then the enter key, and the batch file would copy everything. As soon as one disk had received its contents, pop it out, put in a fresh disk, hit the one key, then enter, etc. I had the 40 floppy disks done in not much more than 10 minutes. In this case, I used the batch file to automate a repetitive process and save me some keystrokes. Now, another great use for a batch files to clear out your temporary directories. This trick works great in Windows, which uses batch files just like DOS. Great a batch file like this, DEL space, C colon, backslash, temp, backslash, start, start, space, DEL space, C colon, backslash, Windows, backslash, temp, backslash, start, start. With these two commands, you can clean out two directories that might otherwise gradually accumulate a lot of temporary files. Create the batch file, stick a shortcut to it in your startup folder, and those directories will be cleaned out automatically every time you boot Windows. Now, if you want to be a little more conservative, and you worry that you might delete something important, what you could do is alter your commands like this. So, the first command would be DEL space, C colon, backslash, temp, backslash, start, dot, TMP. And then, for the other one, DEL space, C colon, backslash, Windows, backslash, temp, backslash, start, dot, TMP. Now, you will only delete files with the TMP extension, and by definition, those files are safe to delete. Now, batch files from multiple commands, and to really tap the power of batch files, you need to use multiple commands. Now, on my website, and there's a link in the show notes to the page, I take an actual example from the Windows 95 installation CD-ROM, and start picking it apart. It's complicated. And, you know, if I try and just recite all of this stuff, it's going to be very boring audio. So, let me just give you a little overview. It starts off with a command to turn off echo. So, it's not going to throw everything up on the screen. Then, there's comments to say, this is what the section of the file is doing. It's looking for the name of your Windows directory. For most people, it would have been C Windows, C colon, backslash, Windows. But, you know, mine at the time was C colon, backslash, when 95, because I used to use dual boot machines a lot. In fact, I was kind of famous for having, you know, one machine that had, I think, six different operating systems I could boot into at the boot time. Then, it's looking for the name of the directory. If it does not find a name, it'll jump down to a section called no window, no window directory. If it does find an aim, it'll keep going with the commands in order. Then, there's some more comments about the next section, what it expects to find. If it doesn't find it, it'll jump down to the no file section. If it does find it, it'll keep going through the commands in order. Then, there are some commands to rename the file and so on. Finally, there's a thing at the end that says, all right, we're done. This ends the batch file and it stops running. This is an example of a moderately complex batch file that uses excellent technique in this documentation. All of the remarks, I think, are a wonderful thing. If you happen to have a windows 95 installation CD or whatever you lying around, you know, check it out for yourself. Each section has an RE each section of the batch file has an REM section that explains what's going on. Also, note how the writer used extra blank REM lines above and below each remark to set it off from the rest of the batch file. How these are not needed, in any sense, but they make it more readable for a person who's trying to follow what the batch file does. You put in, do that, you would do REM, get the Enter key, REM again, and now write your remark, get the Enter key, and then write REM again, if the Enter key. So you've got blank lines above and below. It's great technique. I really love it. You can see it on my website or take a look at it if you have the media around yourself. It is wonderful, and I'd encourage everyone to take a look at it. This is a hookah for hacker public radio signing off, and as always encouraging you to support free software. Bye bye. You have been listening to hacker public radio, as hacker public radio does work. Today's show was contributed by a HBN this night like yourself. If you ever thought of recording podcasts, you click on our contribution to find out how easy it means. HostingPrayHBR has been kindly provided by an onsthost.com, the Internet Archive, and our Sync.net. On the satellite stages, today's show is released on our creative comments, attribution for going to international license.