How to reliably use `rem` on the command line without ignoring adjacent commands?

I am trying to use the rem command to put a note on a command line containing several commands. Here are a few examples to illustrate what I mean:

 echo Hello & rem.Comment & echo world! (echo Hello & rem.Comment) & echo world! 

This works fine, both echo on each line execute as I expect. It seems that . changes the behavior of the rem command so that the remaining line is not treated as a comment:

 Hello world! 

If I put a SPACE (or any other TAB separator ,,,;; ,) instead . , the remaining line and, therefore, the second echo would be ignored (for the second example, the More? prompt appears, because ) is part of the comment, and cmd expects to close ) due to ( ):

 Hello 

I found out next to . The following characters work : , / , \ , [ , ] and + .
What else works is shielded delimiters: ^ SPACE , ^ TAB , ^, ^; and ^= .

However, is there a safe and reliable way ?

I would be very happy with a solution that works for both the command line and batch files.


According to this external link , the familiar syntax is echo. to return an empty string does not work under certain circumstances, so using echo( recommended, as this is the only reliable method.

However, for rem , ( does not work, everything after rem( not recognized as a command.


Since I am aware of a strange rem command error in Windows XP (link this external link : rem %~ ), I am interested in a solution that applies to Windows Vista, Windows 7 or later.

+5
source share
2 answers

The "weird" REM %~ "error" is not limited to XP. It is present in all modern versions of Windows that use CMD.EXE. After reading your question, I wrote a Simon from SS64 note to clarify this issue. REM can also fail if var exists, and you have rem %var:= .

So technically, there is no reliable safe blind use of REM.

But, if you are ready to accept the risk of fatal% expansion, most of your listed hacks are safe to use, but only if the line includes at least one additional command via & or && .

REM. It will never be safe in any situation if there is a file named REM (without extension).

The directory partitions \ and / always fail if the current folder contains a file called test.bat and you use REM\..\test.bat .

Similarly, REM:\..\test.bat always fails.

Each of the other hacks can fail autonomously in a similar situation. For example, REM^[tab]\..\test.bat crashes autonomously, but works when combined with another command. This is the only type of situation I have found where + , [ , ] or ^[tab] can fail.

There are additional cases where some of the other hacks may fail.

Any character in the set C ( ^[space] , ^, remC.bat ^= )) that is valid in file names can fail offline if remC.bat exists. For example, the following failures are autonomous:

 rem^ Fails if "rem .bat" exists 

However, they are all safe when combined with another command:

 echo OK&rem^ This is safe rem^ This is safe &echo OK 

Temporary update

Some of the above errors are incorrect. Research continues at http://www.dostips.com/forum/viewtopic.php?f=3&t=6895&p=44813#p44813 .

I believe that the following simple forms that are guaranteed to work in all cases (excluding the invalid% extension)

 REM: At least one space (or other token delimiter) must be after : REM\ At least one space (or other token delimiter) must be after \ REM/ At least one space (or other token delimiter) must be after / REM^[tab] At lease one space (or other token delimiter) must be after [tab] 

But I will not correct earlier information until the dust calms down.

Complete Temporary Update



My favorite way to use inline comments is to use impossible variables. Only dynamic pseudo variables can contain = in the name, and no variable name can ever contain two = . Therefore, I like to use %= Remark goes here =% . The beauty of this form is that it can be used almost everywhere with impunity if the comment does not contain % or : It can even be safely used in square brackets.

 for %%F in (*) do ( %= Comment within code block =% %= 2nd comment within code block =% FINDSTR /B %=Must match beginning of line=% "string" %= Search string =% "%%F" %= File to search =% ) 
+7
source

These REM variants appear to be a safe way to include the & sign in the comment section.

 REM/ REM\ REM: 

Despite @ dbenham's comment, I cannot create a file that uses these REM variants (I tried REM.bat , REM;.bat , etc.).
It is always useful to add a space after REM^<char> .

The problem with %~ cannot be resolved since cmd.exe uses several parser phases for each line.
And the %~ error is detected in the early phase (percentage expansion phase) just before the phase where a REM will be detected.

But generally, I prefer comment percentages for inline comments described by dbenham

EDIT:
I removed the carriage with REM^<char> , as it does not matter.

Typically, a REM notices the rest of the line, because the parser-parser detects the REM keyword in phase2 of the analyzer and switches to a specialized REM-only parser.

But when a character is added to REM , the keyword will be detected in phase2.
If the character is one of \/;,=+( , the parser will delete it later and execute the usual REM command.

In this case, the reason why the operators of the & , && , | , || can be recognized.

Why rem/ | break rem/ | break does not work, but (REM/) | break does (REM/) | break work?
This is because the pipe starts two separate cmd child processes.
With surrounding brackets, the command will be parsed for the first time in the child process.
But without parentheses, the parent process has already analyzed REM/ and checks if the file exists (but does not execute it).
But when such a file exists, the parser is smart enough to remove the seperator character and detects that REM is an internal command.
It looks a little weird.

+3
source

Source: https://habr.com/ru/post/1240588/


All Articles