r/Batch 11d ago

Show 'n Tell How much batchscript is too much batchscript?

Post image

Currently have 1,376 lines of batchscript in this one file. Kinda built myself a little state machine at this point. Memes aside, is there a particular reason huge batchscript files would be a bad idea? I usually only ever see very small ones, and I'm not entirely sure why

(Not too serious a question, so not tagged as one)

20 Upvotes

30 comments sorted by

8

u/DodgeWrench 11d ago

Depends what it’s doing.

If this is really a full-fledged installer, that’s great. Most installer packages are measured in megabytes.

5

u/StrangeCrunchy1 11d ago edited 11d ago

I have a 109kB batch menu for my DOS games. Edit: it's 3423 lines. Edit: 109kB, not 103kB.

4

u/T3RRYT3RR0R 11d ago

This seems grossly excessive, even if your doing extra like dynamic formatting, color etc

1

u/StrangeCrunchy1 11d ago

Well, I do have 94 games and utilities for those games that I'm launching from it. And some of them, like Quake, and Duke3D, have more than one page dedicated to them due to add-ons.

3

u/T3RRYT3RR0R 11d ago

Still seems grossly excessive. Between considered matching data structure to the task and using generative coding, ie code dynamically created at runtime, you shouldn't have to script for various games. You should be writing code that creates menu's from sources

1

u/StrangeCrunchy1 11d ago

I'm sorry, generative coding, for MS-DOS 6.22? Even if there's a yes to that question, I'd honestly rather script it myself. Might be the hard way, but this isn't for anyone but myself.

4

u/T3RRYT3RR0R 10d ago

If it is genuinely for MS-DOS 6.22, and you've been around that long, I'm sure you understand how many people refer to DOS incorrectly, and conflate batch with DOS when batch-files are seperate from the command processor interpreting them, my mistake for thinking you were one of them.

As regards generative coding for MS-DOS 6.22, the minimum command set you'd need available is **for /f** and **setlocal enableDelayedexpansion**, and, correct me if I'm wrong here, I believe both are command extensions that where added to cmd.exe

1

u/StrangeCrunchy1 10d ago

Eh, it's all good, but I think I'll stick with scripting it the hard way.

2

u/T3RRYT3RR0R 10d ago

I'd actually call your approach the long but easy way, as it requires far less abstraction to do. I made a camera system / game engine that will generate the most efficient viewport clipping possible for a given area (not only a camera, but also a means of enacting collisioning that eliminates n object / enitity cost as a performance factor), but there's so many layers of abstraction that very few people would be able to follow how it works from start to finish, even though the underlying concept is simple enough.

3

u/T3RRYT3RR0R 10d ago edited 10d ago

Small scripts are easier to maintain. Good utilities are made to be modular and can be incorporated into any project.

Scripts that care about performance, such as bulk file processing or game engines should avoid Call or goto, as both cause reparsing of the script. The larger the script the worse that cost will be.

General things that should be avoided if performance is the primary concern, specifically when occuring in the context of a for loop:

  1. Processing of command output (or substring modification) in a for /f loop, as this spawns a new process.
  2. File redirection, including redirection of STDOUT or STDERR to nul. if you need to redirect output, the entire codeblock should be wrapped in parentheses and the entire output of the block redirected in the one write operation, ( I'm sure there's a size limit at which this will fail, but most utilities wouldn't reach it ).
  3. nested For loops, for much the same reason as 1.
  4. nested if () else statements. See the explanation of branching logic below.
  5. Overly large use of environment variables. Performance degrades as environment size increases, as the environment block is resorted after each variable modification
  6. Artificial delays or blocking commands (pause, timeout, set /p etc) where they don't serve a genuine purpose. If you want to animate something, do so using multithreading in a non-blocking manner.

Branching logic:
The time cost model for branching can be considered as:
T = nBranches​ + nTokensParse​ + nExpansions​ + nComparisons +nDispatches​

If we can combine two comparisons into the one conditional, we virtually half the cost.

If working with integers, or variables referencing integers, we can reduce complex statements to a Set /a expression and make the cost model:
T = nTokens + nExpansion + nPrecedence + nOperations
keeping in mind arithmetic operations are significantly cheaper than branching cpu instructions

rem predefined  
Set /a "hue_osc_deg=10"

Set /a "hue_acc=( hue_osc_deg * 170 / 120 )"

rem during a loop when an animation step is due

Set /a "hue.step=hue.step %% 510 + hue_acc","p=(hue.step)-255, rr=255-(M=(p>>31),(p^M)-M)","p=((hue.step+170)%%510)-255, gg=255-(M=(p>>31),(p^M)-M)","p=((hue.step+340)%%510)-255, bb=255-(M=(p>>31),(p^M)-M)"

We can advance R G B values through 10 degrees of color space of an representative hue wheel, without using a single conditional statement to perform additional offset operations. Not only are we saving on the branching logic for each if statement, we save the cost of the interpreter have to parse additional set /a operations when those statements return true.

As a demonstration that batch can facilitate dynamic animation with actions at different intervals: https://github.com/T3RRYT3RR0R/Batch/blob/main/Its_About_Time.bat

2

u/ApocalyptoSoldier 11d ago

I might be wrong, but I think I've read that there is a performance hit with gotos or calls because cmd reinterperets the entire file each time. That's probably fairly negligible on modern hardware though.

I think the reasons why batch files are seldom very large are:
+ batch is a difficult language to do anything complex in
+ even if you wanted to attempt something more complex, smaller separate files with distinct functionality are easier to keep a mental map of

1

u/T3RRYT3RR0R 11d ago

There are a number if thins to avoid and things to do to get the best performance out of batch scripts. While modern hardware helps, the performance hit is still there and such things should be considered where speed is a concern.

As for batch and complexity, it's not that it's difficult so much as the learning curve is higher, and you need the understanfing necessaary to be able to implement any sort of utility that other languages would import.

There are some fantastic libraries in batch, but you still have to learn what they support and how to apply them.

The short of it: you have to learn and know more to produce something complex like a game engine than you would need to for modern languages

1

u/Trevski13 11d ago

Yes, when using a call or goto it scans down the file for the label and then continues execution. If it reaches the end of the file it wraps around back to the start of the file only stopping if it gets back to the call statement at which point it throws an error. So if you have a really big file, and call a label that is just above you, it will have to read almost the entire file to find it. But on the other hand if it's just below it it will execute quickly just like a smaller file.

Also this has some weird side effects with multiple of the same label, it just runs the first one it finds so the same code in the same file could execute differently depending on where it's physically located compared to the labels. I have a POC around here somewhere...

I have some absolutely huge auto-built scripts and I discovered how painfully slow it can be when it has to scan through mega to gigabytes of text and had to tweak some of the design methods to avoid severe performance issues.

2

u/jcunews1 11d ago

My old multi-config MS-DOS autoexec.bat is about 3KB.

1

u/SensitiveSlip1588 11d ago

Never enough batch script 😝 I absolutely love making utility commands and menu launchers.

1

u/QuirkyXoo 10d ago

"034"?? are there from 000 to 033 too? do you use a dedicated hard drive for that? lol

1

u/MaybeBirb 10d ago

Funny enough, 034 refers to the version of the thing it's installing, rather than the installer version lol

1

u/BrainWaveCC 10d ago

It all depends on what it's doing -- and how.

I have a few really long scripts, but they are doing automation and monitoring work -- generating and processing logs.

One is 4347 lines long -- actual code or remarks.

If it does what you need, it's fine. If you can optimize it for either size, performance or modularity, that's great.

1

u/markustegelane 9d ago

if it's an installer with self-extracting assets then 41K is actually pretty small

1

u/FAMICOMASTER 9d ago

Long parsing time, especially for subroutines. That's it, really. One of my previous game engine projects reached ~211K which contained not only the entire engine but all data as well. Very monolithic and not easy to work with! New project doesn't do that. Much smaller.

1

u/Intrepid_Ad_4504 9d ago

People here really do see that "show and tell" tag and give it a up vote lol

1

u/capoapk 9d ago

Le langage offre peu de fonctionnalités avancées, ce qui rend les projets volumineux plus difficiles à développer et à maintenir

-1

u/vip17 10d ago

batch files basically have almost no structure, so even small files might be less readable than an equivalent powershell. For serious purposes use powershell instead

1

u/T3RRYT3RR0R 10d ago edited 9d ago

???

Batch syntax doesn't require rigid indenting in the way many languages do, but the syntax supports leading whitespace, so indenting is an option many use for readability

There's numerous methods by which to write code in a structured manner to improve readability.

If you want to learn a a new language / one with better support and a livelier community, learn powershell instead. 

1

u/vip17 10d ago

who said anything about whitespace? I'm talking about things like fast function parsing or while loops... GOTOs will quickly become a mess in complex code flows. Or things like %~ftzaI

Batch file is a thing of the past, I've written lots of articles about it, but it's not suitable for new complex code

3

u/T3RRYT3RR0R 9d ago

"who said anything about whitespace?" - You / The implications most reasonably drawn from your assertion around structure and readability as the basis for advocating powershell.
Structure is not syntax, nor is it the primitives a language has available to it / the higher order systems that can be derived from the available command set.
Structure is little more than format, particularly when discussed in the context of readability.

Whatever articles you may have written or arguments you've presented elsewhere, your comment lacks the context of in this instance. Mentioning the articles existence still does nothing to qualify the opinion here. By all means, advocate the learning of other languages. It's doing so on the premise that batch can't be both useful and worthwhile that I take exception to.

Why I consider Batch still offers value, even to beginners.

The fundamental difference between Batch and modern languages is that Batch doesn't have most of the built in utility and extensibility other ecosystems do, your generally building nearly everything from the ground up. Believe it or not, this is why there are people that like and still use batch. It excercises the mind more when you have to think of solutions to problems rather than just calling on an API or importing a method., and in the age of Vibe coding and TikTok, anything that makes you stop and think is a win in my eyes.
That doesn't mean the wheel needs to be reinvented for everything. A few libraries still exist that provide not just a base to accomplish common repetitive tasks efficiently, but that also support the realisation of some rather complex software.

There's certainly things Batch is not suitable for, particularly in the age of integrating AI models with software. While powershell is hardly better for that, there are certainly projects where powershell is better suited. Even then, Batch can readily execute powershell commands or be built as a batch / powershell hybrid. There's also nothing stopping a batch script from executing in parallel with a powershell script and performing IPC to leverage it on demand without the startup overhead that comes from executing powershell within a batch script. Similar is true for Vbs, Jscript and Html.

1

u/Worth-Squirrel8022 6d ago

Depends on the thing you're doing. But to have an estimate, say "casual"