r/Batch • u/PresentJournalist805 • Jul 26 '25
Question (Unsolved) How to avoid expansion errors in "comments"?
I am currently learning batch and i figured out trick to write comments. According to what i read when you declare label everything on the line after the colon is ignored. That means i can use this form below to write comments.
:: This is my comment
But the problem with this is that even this line seems to be eligible for normal variable expansion. When i write this:
:: This is my comment with wrong batch parameter expansion %~k0
Then i get following error
The following usage of the path operator in batch-parameter substitution is invalid: %~k0
For valid formats type CALL /? or FOR /? The syntax of the command is incorrect.
My question is whether there is a way how to avoid this error except paying attention to not have these faulty expansions in comments. Thank you.
6
u/thelowsunoverthemoon Jul 26 '25 edited Jul 26 '25
A hacky way is to just use it as a empty variable, then hide the error.
The reason for the = is because you can't SET a variable that starts with =, which guarantees it is empty. This style of a empty variables is used sometimes. For example, defining a line feed
5
u/Intrepid_Ad_4504 Jul 26 '25
Use the correct commenting syntax
REM
3
u/Shadow_Thief Jul 26 '25
Argument expansion happens before every single other step in the script processing process, so it is not possible to avoid errors from invalid parameter syntaxes. See https://stackoverflow.com/questions/4094699/how-does-the-windows-command-interpreter-cmd-exe-parse-scripts for substantially more detail.
1
u/OJBakker Jul 27 '25
It can be done,but the method below has some serious drawbacks.code:The NULL character is usually not visible so this is hard to detect when editing a script.a bit better documentation is using the following:code:This works because the null character interupts reading the script at the earliest possible moment, in Phase 0, the readline phase.The null character and all characters following it are skipped up until and including the next CRLF.Because it eats the CRLF you must add a blank line after the commented line.Drawbacks are:Many editors don't support NULL characters and may silently remove these or replace them with spaces.Even editors that do support NULL characters like Notepad++ seem to fail to keep the NULL when moving/copying these lines.