Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

%~ in REM statement

I've noticed in a windows batch file, even if you REM out a %~ you get the error

For valid formats type CALL /? or FOR /?
The syntax of the command is incorrect.

Why isn't the commented line ignored?

@ ECHO OFF
REM Blah blah blah
REM %~ this will cause an error
REM %%~ Double the fun, no error
like image 205
Mr Mystery Guest Avatar asked Jun 14 '17 08:06

Mr Mystery Guest


People also ask

What Is REM statement with example?

Short for remark, REM is a statement placed in system files such as the autoexec. bat to skip lines from loading. A remark is created by placing "REM" (followed by a space) in front of a line. Doing this remarks the line from loading and with batch files also doesn't show the line if echo is off.

What does this mean %~ dp0?

The %~dp0 Variable. The %~dp0 (that's a zero) variable when referenced within a Windows batch file will expand to the drive letter and path of that batch file. The variables %0-%9 refer to the command line parameters of the batch file.

What is %~ n0 in batch file?

%0 is the name of the batch file. %~n0 Expands %0 to a file Name without file extension.

What does REM mean in powershell?

Rem (abbreviation of remark) is a command (internal) found inside the Windows Command Processor Command Prompt, that allows for inclusion of comments inside batch programs. Comments are supported by most Programming languages usually via special syntax that is associated to them. Ex.


Video Answer


2 Answers

REM is a command like ECHO. Run in a command prompt window rem /? to get help on this command.

The Windows command interpreter first parses this command line like all other lines in a batch file with the exception of those lines starting with a colon which are interpreted as label line and not as command line.

So a single % is interpreted as begin of an environment variable or batch file argument reference before REM is executed at all. The single percent sign is removed because there is no more percent sign on this command line.

%~ is interpreted on command line parsing as invalid batch file argument reference. That the command REM does not evaluate the rest of the line does not matter for Windows command interpreter. First the command line is parsed independent on the command and next the command is executed by cmd.exe whatever the command is.

Remove first line @ ECHO OFF and it can be seen how each command line is executed after being parsed by Windows command interpreter. Lines starting with a colon are not output as being interpreted as label line instead of a command line.

For details about batch script parsing see How does the Windows Command Interpreter (CMD.EXE) parse scripts?

Example:

REM User name: %USERNAME%
REM User profile directory: %USERPROFILE%
REM Application data directory: %APPDATA%
REM Batch arguments: %0 %*
PAUSE

The environment variable references and the batch file arguments reference in those REM command lines are replaced by the values of the environment variables and the batch file arguments on running this batch file without @ECHO OFF at top.

like image 106
Mofi Avatar answered Sep 25 '22 17:09

Mofi


the (initially) posted code will not cause an error. But this will:

REM %~ this will cause an error

The reason is that the argument expansions is with higher priority than the comments and cmd.exe will try first to expand and dequote the not given argument here %~

like image 22
npocmaka Avatar answered Sep 22 '22 17:09

npocmaka