The Book of Batch Scripting: From Fundamentals to Advanced Automation - podcast episode cover

The Book of Batch Scripting: From Fundamentals to Advanced Automation

Mar 23, 202618 min
--:--
--:--
Download Metacast podcast app
Listen to this episode in Metacast mobile app
Don't just listen to podcasts. Learn from them with transcripts, summaries, and chapters for every episode. Skim, search, and bookmark insights. Learn more

Episode description

A comprehensive guide to mastering the Windows command-line language, moving from basic file creation to sophisticated automation techniques. The book covers fundamental building blocks such as variables, conditional logic, and loops, specifically highlighting the power of the FOR command for processing data. McLarney introduces the concept of "batveats," which are the unique syntax quirks and pitfalls inherent to the Batch environment. Advanced sections demonstrate how to implement complex structures like arrays, hash tables, and recursive logic that were not originally intended for the language. Finally, the text explores object-oriented design and robust troubleshooting strategies to help users create professional-grade scripts for modern system administration.

You can listen and download our episodes for free on more than 10 different platforms:
https://linktr.ee/cyber_security_summary

Get the Book now from Amazon:
https://www.amazon.com/Book-Batch-Jack-McLarney/dp/1718503423?&linkCode=ll2&tag=cvthunderx-20&linkId=b0947bb761cabe02079327253be96058&language=en_US&ref_=as_li_ss_tl

Discover our free courses in tech and cybersecurity, Start learning today:
https://linktr.ee/cybercode_academy

Transcript

Speaker 1

I want you to I want you to picture a garage, like a really high end modern.

Speaker 2

Garage, Okay, I'm picturing it.

Speaker 1

And on the left side, you've got the absolute cutting edge of automotive engineering. Let's say a brand new, gleaming Tesla or maybe Elucid Air.

Speaker 2

Right, something really sleek, exactly.

Speaker 1

It's got the self driving AI, the massive touch screens, the over the air updates. It is the future.

Speaker 2

It's sexy. It's what everyone wants to drive right now, right, and.

Speaker 1

That represents modern coding languages, your Python, your rust go things like that. Yeah, But then I want you to look over to the right side of this garage. O. Sitting on cinder blocks covered in maybe forty years of dust, is a rusted out Model T Ford. It looks like it belongs in a museum, sounds out a place. It does. But here's the twist. You pop the hood of that shiny k need Tesla and the engine inside it. It's the Model T engine.

Speaker 2

That is a terrifying image, honestly, but it's painfully accurate.

Speaker 1

It really is. Yeah, Because we're ta talking go to technology that feels ancient, almost prehistoric in computer years, yet it is currently running the back end of your bank. It's the logistics system delivering your packages.

Speaker 2

And crucially, it is installed on every single Windows computer in.

Speaker 1

Existence right now, Yes, every single one.

Speaker 2

We are talking about batch scripting. I've heard it called the cockroach of the IT world.

Speaker 1

Cockroach feels a little harsh, but I kind of get it. It just won't die.

Speaker 2

It won't, and honestly it shouldn't. It's way too integrated into the bedrock of our digital infrastructure.

Speaker 1

So today we're doing a deep dive into a really fascinating source. It's called the Book of Batch Scripting From Fundamentals to Advance Automation by Jack McLarney. It's great read, it is, and the mission for this deep dive is pretty clear. We want to figure out why this nineteen eighties technology, which is rooted in MS doos, is still so vital.

Speaker 2

But we also need to warn you. McLarney makes it very clear in the book that batch is a minefield. Oh absolutely, it's powerful, but it is just full of traps. He actually coins a term for this, which I absolutely love. He calls them bat beats. Bat beats, yeah, like caveats, but specific to batch. A bat beat is basically a quirk or a trap in the language that just doesn't exist in modern coding.

Speaker 1

Right because modern languages have safety rails exactly.

Speaker 2

But in Batch, the interpreter is incredibly literal or sometimes just plain weird in a way that can completely destroy your entire workflow if you aren't careful.

Speaker 1

And we have a list of the nastiest ones to get through today, we're going to look at the space trap, the Octal Horror Story, which is my absolute.

Speaker 2

Favorite, by the way, oh it's a classic.

Speaker 1

And the inception logic of nested variables. But let's start with the basics what the book calls the wild West of variables.

Speaker 2

The wild West is definitely the right way to put it. There is no sheriff in this town at all.

Speaker 1

So in most languages, if I want to write code, I have to be specific. I tell the computer, Hey, this variable is a number, or this variable is a string of text. There were rules.

Speaker 2

Batch doesn't care about any of that, not at all. In Batch, everything is created x nihilo out of nothing. Intrinsically, to the Batch interpreter, everything is just a string of bytes. It doesn't know the difference between the number five and the word five.

Speaker 1

Until you force it to do math, which leads to what I think is the most infuriating yet kind of hilarious rookie mistake in the book The Space Trap.

Speaker 2

Oh Man, the Space Trap. This is a classic batv. I have seen grown engineers, people with master's degrees, completely pull their hair out over this.

Speaker 1

So walk us through this. Let's say I want to set a variable named x to the value hello.

Speaker 2

Okay.

Speaker 1

In a normal language, being a tidy person who likes readable code, I might type set x, then a space, an equal sign, another space, and then hello. I put spaces around the equal sign because it looks nice.

Speaker 2

It's clean, right, And in Python our JavaScript, that's fine. The computer ignores the white space. That knows what you mean. But the batch interpreter it is aggressively literal.

Speaker 1

Aggressively literal.

Speaker 2

Yes, if you type set x space equal space hello, the interpreter doesn't say, oh they want x to equal hello, does it do? It reads exactly what you typed. It sees the space before the equal sign and includes it in the name. So now you have a variable named x space.

Speaker 1

Wait xspace literally.

Speaker 2

Literally, and then it sees the space after the equal sign and includes that in the value. So the value isn't Hello, it's space Hello.

Speaker 1

So I haven't said X to hello at all. I've set x space to space hello exactly.

Speaker 2

And here is the kicker. If you try to use that variable later by calling just x, which is what you think you named it, it won't work because X doesn't exist, only X space exists.

Speaker 1

That is maddening. And since you can't see a space character with the naked eye, you'd be steering at your code for hours wondering why it's broken.

Speaker 2

Precisely, you're looking at the screen shouting, I defined X. It's right there. But you didn't define X. You defined x space.

Speaker 1

Wow.

Speaker 2

That's why their author mcclerney practically begs you to stop using basic notepad. You need an editor like Notepad plas plus where you can turn on show space and tab. You have to see the invisible characters or badge will destroy you.

Speaker 1

Okay, So once we've successfully set a variable, you know, without the space as, we run into another issue, usefulness. Usually when I set a variable and a command prompt, it vanishes the second I close the window.

Speaker 2

That's the ephemeral nature of the standard set command. It's only a live for that specific session. It's like short term memory. Yeah, but mcclarney highlights a tool that I think is vastly underused, sex the extreme set Essentially, yes, SETX allows you to save a variable into the system environment permanently. It actually survives a reboot.

Speaker 1

So why does that matter? Give me a real world scenario where i'd use that.

Speaker 2

Imagine you're a developer. You're building an app that needs to know where a database is located. You don't want to type that really long filepath every single time you open a terminal to test your.

Speaker 1

Code, right, That would be pedious.

Speaker 2

So you can use set x to burn that location into your machine's environment.

Speaker 1

So it becomes part of the furniture exactly.

Speaker 2

Every time you open your tools, the variable is just there. You don't have to retype it. It's a huge time saver for setting up development environments. But again, you have to be careful because if you set ux a typo, that typo is now permanent until you go in and fix it.

Speaker 1

High stakes, high reward. Okay, so we've got our variables, we're avoiding spaces. Now we need to talk about the segment that actually made me laugh out loud while reading the octal horror story.

Speaker 2

This is the ultimate batbate. It's a story that perfectly illustrates how fragile batch logic can be.

Speaker 1

So the setup is this. The author has a script that's been running perfectly for six months. It's a date calculator. Its job is to figure out the previous.

Speaker 2

Day, right, simple logic. If today is the fifth, yesterday was the fourth. If today's the first, we need to go back to the previous month.

Speaker 1

And this works beautifully in January, February, March, all the way through July. But then August first rolls around the day of doom. The script wakes up, tries to calculate the previous month. It takes the current month, which is August represented as zero eight, and tries to subtract one to get July or zero seven. Simple math eight minus one.

Speaker 2

Equal seven, But the script crashes.

Speaker 1

It just implodes why why on Earth would eight minus one crash a computer program?

Speaker 2

It comes down to a legacy holdover from the dawn of computing, specifically from the C programming language in Unix. In Batch, a leading zero on a number is a signal. It tells the interpreter we are not using the decimal system.

Speaker 1

The base ten system that humans use.

Speaker 2

Right, it says we are using octal base eight.

Speaker 1

Okay, let's unpack octal. Most listeners probably haven't thought about base eight since high school computer science.

Speaker 2

If ever, so, in our normal base ten system, we have digit zero through nine. When we get to ten, we roll over. In octal, you only have digit zero through seven. That's it. There is no eight, there is no eight, and there's no nine. In octaal you count zero one, two, three, four, five, six, seven, and the next number is ten.

Speaker 1

So when the script saw zero eight, it.

Speaker 2

Saw the zero and switched to octal mode. Then it saw the eight and panicked. It's like trying to divide by zero. To the back interpreter, zero eight is an impossible number. It's a hallucination. It just doesn't exist.

Speaker 1

That is wild. So literally, because the date was August first on the calendar, the server process died.

Speaker 2

Yes, And the author describes his reaction when he finally figured this out. He's looking at the logs, he sees the zero eight and he realizes it's the octal bug. He just screams oscy tale at the sky.

Speaker 1

He compares it to Captain Kirk screaming con in star Trek two.

Speaker 2

I can picture it perfectly, just sheer nerd rage.

Speaker 1

So how do you fix that? Do you just cancel August?

Speaker 2

You have to scrub the data. You use substring tricks to look at the date. You say, if the first character is a zero, delete it. You have to turn zero eight into just eight.

Speaker 1

And once it's eight without the.

Speaker 2

Zero, Batch treats it as a decimal and everything is fine.

Speaker 1

It really reinforces the idea that Batch is just a language of workarounds.

Speaker 2

Absolutely, it's ductate all the way down. And that leads perfectly into the next topic, faking functionality. Because if you're coming from Python or Java, you expect certain things. You expect to.

Speaker 1

Have boollions, true or false variables.

Speaker 2

Right, and you expect to have floating point numbers, decimals.

Speaker 1

And Batch has neither none, zero.

Speaker 2

So how do you check if something is true or false?

Speaker 1

You have to diyat. The book shows this clever workaround. Instead of setting a variable to the abstract concept of true, you set the variable to the string true equals equals true.

Speaker 2

Wait, you set the variable to an actual equation exactly.

Speaker 1

So later when you use an if command, you're basically pasting that equation into the command, the interpreter checks does the string true equal the string truth? Yes, so the condition passes. It's crude, but it works.

Speaker 2

That feels like hot wiring a car to get it started.

Speaker 1

That is Batch in a nutshell, right, And it gets even messier with math. Say you want to calculate sales tax. You need decimals. You need to multiply by one point oh seven. Batch can't do decimals. It truncates everything to a whole number.

Speaker 2

So how do you do it? The book mentions a spade shovel analogy. Right. Mcclarney says, doing floating point math in Batch is like digging a foundation with a spade shovel. You can do it, but do you really want to? The trick is what he calls digital anarchy. Digital anarchy, You take your number, say one point ninety nine, You treat it as a string of text. You remove the dot. Now you have one ninety nine.

Speaker 1

Okay, so now it's an integer. Batch can handle that, right.

Speaker 2

You do your math on one ninety nine. Let's say you double it to three ninety eight. Then you have to use substring logic to forcefully paste the decimal point back in before the last two digits. So you manually turn three ninety eight back into three point ninety eight.

Speaker 1

You are manually managing the decimal point.

Speaker 2

You are the floating point processor. You are doing the job the CPU usually does for you.

Speaker 1

That is dedication. But I guess if it's the only tool installed on the server, you do what you have to do.

Speaker 2

And that brings us to the feature that sounds the most intimidating but is actually the secret weapon of Batch power users.

Speaker 1

Delayed expansion set local enabled delete expansion. Sounds like a cheat code.

Speaker 2

It basically is. To understand why you need it, you have to understand how Batch reads code. Normally, Batch reads a full line of code or a block of code inside parenthesis and parses it all at once.

Speaker 1

Explain that parsing process a bit.

Speaker 2

Think of it like ordering at a restaurant. In a normal language, you order appetizers, you eat them, then you order the main course. In Batch without delayed expansion, you have to order everything appetizers, main dessert the moment you sit down.

Speaker 1

So if I change my mind halfway through the meal, too bad.

Speaker 2

The order is already in. In coding terms, if you change a variable inside a loop and then try to read it inside that same loop. The interpreter won't see the change. It already decided what that variable was before the loop started.

Speaker 1

That seems like a massive design flaw.

Speaker 2

It's just how it's built in the eighties. But enabled delayed expansion fixes it. It allows you to use exclamation marks instead of percent signs, like exclamation point variable exclamation point. This forces the interpreter to.

Speaker 1

Wait, wait for what.

Speaker 2

It tells the interpreter, don't check this variable when you take the order, check it when you actually serve the dish. It forces the variable to be evaluated at the exact moment of execution.

Speaker 1

Hence delayed expansion precisely.

Speaker 2

And this unlocks something really cool that the author compares to the movie Inception nested.

Speaker 1

Variables, variables inside variable.

Speaker 2

Yes, with delayed expansion, you can do something like exclamation food percent city percent exclamation.

Speaker 1

Okay, let's unpack that. That looks like a typo salad.

Speaker 2

It does, but look at the order of operations. The interpreter looks at the percent city percent first. Let's say the city variable is Nashville. It resolves that part immediately. Now, the overall variable name becomes exclamation food Nashville exclamation ah.

Speaker 1

I see. So it construct the name of the variable it wants to find exactly.

Speaker 2

Then it looks up the value of food Nashville, which might be hot chicken.

Speaker 1

So you can dynamically change which variable you are looking at based on the value of another variable exactly.

Speaker 2

It allows for dynamic coding like arrays and lookups that is actually really hard to do in some compiled languages. It turns this clunky old scripting language into something surprisingly flexible.

Speaker 1

I love that. It's like finding a secret compartment in that rusted out model.

Speaker 2

T a secret compartment that holds a supercomputer.

Speaker 1

So we've covered the logic and the variables. Now we need to talk about the heavy lifting moving files, because at the end of the day, that's what most people use Batch for backups, organizing folders, moving data.

Speaker 2

And there is a clear hierarchy here. It's like an evolutionary chart. At the bottom you have the copy command, the primitive tool. The expert advice here is simple, do not use copy for actual files. It's too basic. It stops at the first sign of trouble. Its only real use nowadays is concatenation, merging, text files together or creating empty files.

Speaker 1

Using the null device, So we graduate to s copy.

Speaker 2

SKP is the workhorse. It's been around forever. It's great for single files, and the book highlights two crucial flags or options you need to know, slash y and slash F.

Speaker 1

Slash y suppresses the conformation pumped right. Are you sure you want to overwrite prompt yes.

Speaker 2

If you are automating a backup that runs at three in the morning, you cannot have the script pause to ask a question. No one is there to answer it. The script will just hang there forever. Slash y just says yes to everything, and slash F full paths. It forces the LUG to show you exactly where the file came from and exactly where it went. Without it, your flying blind if something goes wrong.

Speaker 1

But then we reach the final form the robust copy robocopy.

Speaker 2

Robocopy is an absolute beast. It's designed for serious automation. It can mirror entire drives, it can resume file transfers if the network cuts out. It's incredibly resilient.

Speaker 1

And the logging is intense.

Speaker 2

Oh it's beautiful. Header's speed statistics separate counts for skip files. Mismatched files. But there is a batfeat here too. The return codes.

Speaker 1

We talked about this before the show. In SAT copy a zero means success. Everything is good right.

Speaker 2

In almost all of computing, zero means no error. But robocopy it uses powers.

Speaker 1

Of two logicwers of two, So.

Speaker 2

If robic copyer returns to zero, it actually means no errors. But I didn't copy anything.

Speaker 1

Wait, so zero is essentially I did nothing right.

Speaker 2

It means the files matched the destination already, so it just skipped them.

Speaker 1

Okay.

Speaker 2

If it returns a one, it means I copied one or more files successfully.

Speaker 1

Okay, that sounds like success.

Speaker 2

But then you have two four, eight, sixteen, and they can be combined. This is bitmasking. So if you get a return code of three, that means one files copied plus two extra files detected.

Speaker 1

So you literally have to do modulo math just to figure out if your backup work you do.

Speaker 2

You can't just check if aer a level equals zero. You have to check if the air level is less than eight. Essentially, it's bizarre, but it gives you a granular level of detail that other commands just don't.

Speaker 1

It seems like that's the theme of this entire deep dive. Bizarre but useful, it really is.

Speaker 2

It forces you to think differently.

Speaker 1

Which brings us to the philosophy of the book. Mcclerney compares being a batch coder to being a Sumerian priest.

Speaker 2

I love this analogy. He says, reading batch code, especially when you get into the messy syntax of four loops and delayed expansion, is like reading cuneiform. It looks esoteric, it looks cryptic, but if you can read it, you have immense power over the machine. You are one of the few who can speak the language of the temple.

Speaker 1

And he quotes Woody Guthrie in the epigraph. Any fool can make something complicated, it takes a genius to make it simple.

Speaker 2

Batch force is simplicity. You don't have libraries, you don't have fancy IDEs helping you out. You have raw text commands. You are forced to strip the problem down to its bare metal logic. And there is something deeply satisfying about that.

Speaker 1

It's digital minimalism.

Speaker 2

It is, and the barrier to entry is zero. That's the most important takeaway.

Speaker 1

You don't need to install Python, you don't need to set up a compiler, right now you.

Speaker 2

Can open notepad, you can type echo Hello world, you can save it as dot bat, and when you double click it, you have written a program. You are officially a programmer.

Speaker 1

It's the garage door opener to the world.

Speaker 2

Of codev And honestly, if you can master the logic of preventing an octal crash or figuring out robocopy bit masks, you can learn any other language easily. Batch teaches you to think like the computer.

Speaker 1

So here is a final thought for you to chew on. We talked about how a simple date August first, could crash a server because of a tiny logic quirk, and how a number is interpreted.

Speaker 2

The octal bug.

Speaker 1

It makes you wonder what other invisible logic is running the world right now. How many banking systems or traffic lights or power grids are running on a dot bat file written in nineteen ninety five, just waiting for a specific date or a specific file name to trigger a hidden trap.

Speaker 2

That is a terrifying thought. The digital infrastructure of the world is held together by duct tape and set local enabled delayed expansion.

Speaker 1

And on that note, maybe go check your backups. Thanks for diving in with

Speaker 2

Us always a pleasure,

Transcript source: Provided by creator in RSS feed: download file
For the best experience, listen in Metacast app for iOS or Android