batch file structure

84 views
Skip to first unread message

Dallas

unread,
Jul 5, 2022, 8:52:23 AM7/5/22
to
When you code a batch file do you have patterns that help when you compose them?

For example I always start off a batch file with a line in the form

@echo off % mybatchfilename.bat %

Note the spaces after the first % and before the final % character.
I put those there just to make sure I don't accidentally match an actual variable name.

That way the content of the batch file carries the name of the batch file.

I am curious if anyone has things like that that help you compose batch files.

Zaidy036

unread,
Jul 5, 2022, 10:56:46 AM7/5/22
to
My first line is always:

:: name.bat

Auric__

unread,
Jul 5, 2022, 12:43:39 PM7/5/22
to
Not batch files, no. I have a template that I use for the README of my
projects, but nothing for anything scripting- or programming-related. The
majority of my batch files tend to be one-liners anyway.

--
You were warned! Now reap the consequences!

Kerr-Mudd, John

unread,
Jul 5, 2022, 4:28:06 PM7/5/22
to
Mostly self-documented with a 'help' section

rem {insert datestamp}
if \%1==\ goto Usage
...



...
goto end
Usage:
echo %0 is a program to ...
echo Usage is %0 parm1 parm2 {parm3}
echo e.g. %0 x y does action
rem relies on (dependency) being accessible via path


--
Bah, and indeed Humbug.

Auric__

unread,
Jul 5, 2022, 6:00:39 PM7/5/22
to
My batches that I use to compress files have help sections, but seeing how
each batch was based on the previous batch (they're named 1z.cmd, 2z.cmd,
etc.) I really only wrote that section once and then modify it as needed for
whatever changes I make.

--
Because in the end, does it really matter?
And why let the truth get in the way of a good story?

Dallas

unread,
Jul 6, 2022, 11:58:17 AM7/6/22
to
On 7/5/2022 3:28 PM, Kerr-Mudd, John wrote:
> Mostly self-documented with a 'help' section
>
> rem {insert datestamp}
> if \%1==\ goto Usage
> ...
> ...
> goto end
> Usage:
> echo %0 is a program to ...
> echo Usage is %0 parm1 parm2 {parm3}
> echo e.g. %0 x y does action
> rem relies on (dependency) being accessible via path
>
>

Seeing your line coded as

if \%1==\ goto Usage

makes me curious as to how others code that.
I have been using

if [%1]==[] goto Usage:

because it reads well (to my eyes anyway)

Note that I also tack on a colon ( : ) after a label like Usage: in a goto statement just so it
perfectly matches the label to jump to.




Kerr-Mudd, John

unread,
Jul 6, 2022, 12:49:44 PM7/6/22
to
Yes that's better. I merely changed from my older pre-NT batch style of

if "%1 == "

that used to work, and haven't moved on since.

Dallas

unread,
Jul 6, 2022, 2:59:33 PM7/6/22
to
On 7/5/2022 11:43 AM, Auric__ wrote:
> ... The
> majority of my batch files tend to be one-liners anyway.
>

How do you code a batch one-liner ?

I can't remember coding any one-liners.

Auric__

unread,
Jul 6, 2022, 4:59:15 PM7/6/22
to
Like this:

dir/a-d/b/s|find /v /i ".mp3">\list.txt

or this:

D:\Wintools\Programming\tcc\tcc.exe *.c

or this:

for %%x in (*.baf) do AICompile FILE %%~nx.baf %%~nx.err %%~nx.bcs

or even just:

del *.bak

All of them almost exclusively ran from the GUI, not the command line.


I generally use batch files as scripts, not complete programs.

--
Three angels follow me. The bright one bleeds power.
The dark one bleeds will. The smallest merely weeps.

Dallas

unread,
Jul 7, 2022, 11:19:35 AM7/7/22
to
On 7/6/2022 3:59 PM, Auric__ wrote:
> Dallas wrote:
>> On 7/5/2022 11:43 AM, Auric__ wrote:
>>> The majority of my batch files tend to be one-liners anyway.
>>
>> How do you code a batch one-liner ?
>> I can't remember coding any one-liners.
>
> Like this:
>
> dir/a-d/b/s|find /v /i ".mp3">\list.txt
>
> or this:
>
> D:\Wintools\Programming\tcc\tcc.exe *.c
>
> or this:
>
> for %%x in (*.baf) do AICompile FILE %%~nx.baf %%~nx.err %%~nx.bcs
>
> or even just:
>
> del *.bak
>
> All of them almost exclusively ran from the GUI, not the command line.
>
> I generally use batch files as scripts, not complete programs.
>


What did you name those batch files?

How do you run a batch file from the GUI ?



Auric__

unread,
Jul 7, 2022, 12:22:05 PM7/7/22
to
find-non-mp3s.bat
compile-tcc.bat
cmp.bat
killbaks.bat (although if I had to type it out, it'd probably be 'kbk.bat')

> How do you run a batch file from the GUI ?

Just like anything else in the GUI, double-click it, or select it and press
Enter. The batch is run from the directory it's in, unless you make a .lnk
(or .pif) with a different "Start in" directory.

- 'find-non-mp3s.bat' was run in the top level of my Music directory
- 'compile-tcc.bat' is run in whatever dir contains C files I need compiled
- 'cmp.bat' is for a game mod I was working on, a long time ago
- a copy of 'killbaks.bat' exists in every directory I have where .bak files
are regularly created that I don't want to save

--
You regard an indecision as a mistake?

Dallas

unread,
Jul 7, 2022, 12:34:52 PM7/7/22
to
I just now noticed that none of the one-liners has any arguments.
The %1 %2 %3 ... arguments.
So, running by double clicking makes more sense to me now.

I suppose if you did need arguments you could prompt for them as the batch file ran.


Auric__

unread,
Jul 7, 2022, 4:00:44 PM7/7/22
to
Dallas wrote:

> I just now noticed that none of the one-liners has any arguments.
> The %1 %2 %3 ... arguments.
> So, running by double clicking makes more sense to me now.
>
> I suppose if you did need arguments you could prompt for them as the
> batch file ran.

All of my batches that require arguments are rather longer than one line.
There is a method to pass arguments via a .lnk and/or .pif, but I no longer
remember how. (Haven't needed it in... 15 years? Longer?)

--
Such a pity. How much grander and more powerful you could have still
become. If you've but had the temerity to embrace the path set before you.

Grant Taylor

unread,
Jul 8, 2022, 9:15:13 PM7/8/22
to
On 7/7/22 10:34 AM, Dallas wrote:
> I suppose if you did need arguments you could prompt for them as the
> batch file ran.

You should be able to get a batch file to respond to files drug and
dropped onto it's icon.

I used to have some batch / command files for converting / compressing
things. I think I even put a shortcut in the Send To menu on one of my
systems when I was doing a LOT of things.



--
Grant. . . .
unix || die

Ammammata

unread,
Jul 15, 2022, 6:03:23 AM7/15/22
to
Il giorno Wed 06 Jul 2022 08:59:30p, *Dallas* ha inviato su
alt.msdos.batch.nt il messaggio news:ta4m2j$3v84$1...@dont-email.me. Vediamo
cosa ha scritto:

> How do you code a batch one-liner ?
>
>

batch file - How do I run two commands in one line in Windows CMD? - Stack
Overflow
https://stackoverflow.com/questions/8055371/how-do-i-run-two-commands-in-
one-line-in-windows-cmd

In MS-DOS 5.0 and later, through some earlier Windows and NT versions of
the command interpreter, the (undocumented) command separator was character
20 (Ctrl+T) which I'll represent with ^T here.

dir ^T echo foo

--
/-\ /\/\ /\/\ /-\ /\/\ /\/\ /-\ T /-\
-=- -=- -=- -=- -=- -=- -=- -=- - -=-
........... [ al lavoro ] ...........

Harry Potter

unread,
Jul 21, 2022, 1:54:06 PM7/21/22
to
Hi! I use batch files a lot and have a simple, empty batch file, as follows:
------------
@echo off
-------------
When I need a new batch file, I use Template Creator and this or another template to start my batch files. I have a DOS laptop at my mother's house with the JAM disk compressor installed. Since JAM doesn't automatically mount disks, I use batch files to mount floppies and run the programs on them. It would be easier to create a batch file to copy such files to the floppies, but Template Creator for DOS can do the job. On my WIn98SE system at my mother's house, AppZip doesn't work properly, so I use 7Zip and batch files to temporarily decompress programs on the hard drive. If you want Template Creator, just ask, and I'll provide the URL.

Clif McIrvin

unread,
Sep 17, 2022, 9:33:29 PM9/17/22
to
> You should be able to get a batch file to respond to files drug and
> dropped onto it's icon.

Drag and drop shows up as %1 (or more precisely %* )
Also parameters supplied via .lnk show up as command line parameters.
I don't recall what happens if you drop a file onto a shortcut that includes parameters.

Timo Salmi's excellent FAQ has lots and lots of examples and explanations.
I just saw a link in another recent thread.

Dallas

unread,
Sep 18, 2022, 10:56:49 AM9/18/22
to
On 9/17/2022 8:33 PM, Clif McIrvin wrote:
> Timo Salmi's excellent FAQ has lots and lots of examples and explanations.
> I just saw a link in another recent thread.

http://www.elisanet.fi/tsalmi/info/tscmd.html
Reply all
Reply to author
Forward
0 new messages