Messages - wolf.b [ switch to compact view ]

Pages: prev1 2 3 [4] 5 6 7 8 9 ... 13next
16
A note on writing comments inside batch files: (sorry, I am getting carried away)

the keyword "rem" is not universally useful for commenting out lines inside batch files. It really is a "do-nothing" command and it will be executed. This can lead to strange behavior to say the least. Take for example the following line inside a batch file:

...
echo Some text that will appear in the file test.txt > test.txt
...

Now let us assume that I want that line to be ignored (commented out), but I don't want to delete it, to save me remembering, with the option to include the line later again ... If I added the command rem in front of the line like so:

...
rem echo Some text that will appear in the file test.txt > test.txt
...

I will get this behavior: the command "rem" with the parameters "echo Some text that will appear in the file test.txt" will be executed and do nothing, that is: it will produce no output. But that empty output will still be redirected to the file test.txt. It is the redirection (">") that will now create an empty file test.txt. The danger here is that redirection does not ask for confirmation when overwriting an existing file. I repeat: rem is a command, that will be executed and, when used for commenting out a line that contains redirection, the redirection will be carried out as well.

Solution:
I use the double colon ("::") to comment out lines that I want to temporary be ignored, or for writing comments inside batch files. The behaviour of that can be regarded as such: Any line beginning with a colon is treated as a label. If the second character (first character of the label name) is an illegal character for a label name, then the entire line will be ignored. The following line inside a batch file will not produce an empty file called test.txt, and it will not overwrite an existing file:

...
:: echo Some text that will appear in the file test.txt > test.txt
...


Greetings
Wolf

17
A note on using two percent signs inside batch files, when using the for command. There are other similar cases as well:

The command line interpretor reads the batch file line by line. When it comes across a line that reads for example:
set PATH=%PATH%;C:\TOOLS
then it will append the directory C:\TOOLS to the current path. This is achieved by doing a replacement before execution. When the line is read, the CLI will replace the expression %PATH% with the string that is stored inside the environment variable called PATH (usually a list of folders, separated by semicolons). Lets assume for the sake of easy examples that the PATH would be C:\WINDOWS;C:\WINDOWS\COMMAND at the beginning. Replacement leads to the following command being executed:
set PATH=C:\WINDOWS;C:\WINDOWS\COMMAND;C:\TOOLS
Relacement (also called expansion of variables) is done first, execution is done second.

Now the for command is special, as you don't want the variable %f to be expanded, you want to assign something to it. If you were to write on the command line (DOS prompt) this:
for %x in (1 2 3) do echo %x
you would get what you want, because it is not inside a batch file and is correct syntax.The same line inside a batch file would produce a syntax error, secondary to expansion of %x to an empty string, resulting in the execution of:
for in (1 2 3) do echo
and that is missing the necessary name for the variable.

The solution is the special expansion of double percent signs: "%%" expands to (the shorter) "%", and the line:
for %%x in (1 2 3) do echo %%x
will expand to the correct line:
for %x in (1 2 3) do echo %x

Note that the name of the variable (here: x) is just added on after the expansion ot "%%", just like all the other letters.

Short: at the prompt use one, in batch files use two percent signs. (Applies only to special commands like for)


Greetings
Wolf

18
Thanks tinjaw,

Yes , I should have specifically said in reply #13, that the renaming can be done, instead of just
... that can be done.
My reason for "breaking" the flow of discussion at that point was to admit that
  • eighther I have to go and study the syntax of cmd.exe (I really should do that!) and both of foolios and myself benefit from the discussion at a very slow rate (nothing wrong with that), or
  • we switch to an alternative command line interpreter.

In my humble opinion, it is easier to start learning batch programming for 4DOS than it is to learn it for cmd.exe. Let me admit that my opinion is mainly based on the fact that I never really bothered to tackle the second, because I thought why learn a batch "dialect" that does not give good help. I am very fond of the help system that came with MS-DOS 6.22 and 7.10. Only many years later I had first contact with cmd.exe, which I believe is also much more powerful than command.com. But secondary to lack of good help being available to me, and the fact that 4DOS did everything I wanted it to do, and I already knew the features, I ended up ignoring cmd.exe as a command line interpreter.

For now I just give a sample batch file that I think will be easy enough for beginners to look at, and that slowly introduces some of the power of 4DOS:

:: TEST.BAT
:: by Wolf
::
@echo off

:: The following line will branch the flow of execution the the label called
:: Continue, if the expression evaluation returns true. All MS DOS command
:: line processors will evaluaye the condition as false because they treat
:: both sides of the double equal signs as STRINGS. "1.0" and "01" are not
:: the same. 4DOS has the ability to recognise the case here that on both
:: sides of the double equal signs, we have numerical expressions, that
:: evaluate to the same number.

if 1.0 == 01 goto Continue

:: default command line processor will call 4DOS to run the same batch as
:: well, with added parameters on the command line. After 4DOS finishes, it
:: will give control back to the default command line processor.
if exist 4dos.com 4dos.com /c %0 this is test batch
if not exist 4dos.com echo Sorry, 4DOS.COM not found.

goto End

:Continue
echo Command line: %0&
echo Command line parameters: %&
echo.
echo Batch file name:   %0
echo First parameter:   %1
echo Second parameter:  %2
echo Third parameter:   %3
echo Fourth parameter:  %4

:End


In case anybody wants to provide enlightenment, as to what the equivalent batch would be for cmd.exe, please post. In the meantime I do a bit of research myself.


Greetings
Wolf


19
No need to install it. We don't want to create dependencies. The batch files for 4DOS will work on many OS: all MS DOS versions since version 3.3 (1990[edit:1988]). All Windows versions since 3.0. As cranioscopical has pointed out correctly, the alternative 4NT is recommended for MS OS that are based on what they call "New Technology", such as Windows NT, Win2000, Win2003, WinXP, and Vista. The drawback is that you only get a time limited version for free. The full version is quite expensive. Everything that you write for 4DOS will be useful for 4NT as well. So I suggested 4DOS for a start and you can always change to 4NT later. At the moment I think for learning batch commands the free 4DOS will be good.


20
4DOS Revived!

Click on the button that says "Binaries" to download the zip file. For a start, you would only need one file from inside called 4dos.com.

Pages: prev1 2 3 [4] 5 6 7 8 9 ... 13next
Go to full version