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.