Page 1 of 1

How <cr> behaves in a variable?

Posted: 21 Aug 2019 06:09
by Szecska
Hi everyone!

My problem is that I have no idea how to check if a variable is equal with a <cr>(0x0d) charater.
So far what I got:
The var key comes from a read key method (with either the "replace /w" or "xcopy /w" method)

Code: Select all

for /f skip^=1^ delims^=^ eol^= %%A in ('replace ? . /u /w') do set key=^%%A
(this is the more efficient one)
Got the charater in the %CR% variable:

Code: Select all

for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
And now I'm trying to if it:

Code: Select all

if %key%==%CR% Echo.
It gives me some output that i dont undertstand :/

The full test code:

Code: Select all

setlocal enabledelayedexpansion
for /f "tokens=*" %%D in ("%cd%") do if %%~dD==%systemdrive% %systemdrive%
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
:beg
set/p "nnn=xcopy key:"<nul
set "keyx="
for /f "delims=" %%k in ('xcopy /w "%~f0" "%~f0" 2^>nul') do if not defined keyx set "keyx=%%k"
set "keyx=%keyx:~-1%"
echo %keyx%
echo The return for the %keyx%:
if %keyx% equ ^%CR% (echo Yes ) else echo nope
set/p "nnn=replace key:" <nul
for /f skip^=1^ delims^=^ eol^= %%A in ('replace ? . /u /w') do set keyr=^%%A
echo %keyr%
echo The return for the %keyr%:
if %keyr% equ ^%CR% (echo Yes ) else echo nope
goto beg
Thanks in advance for helping

Re: How <cr> behaves in a variable?

Posted: 21 Aug 2019 07:26
by jeb
Hi Szecska,

the cr character should be used only with delayded expansion.

So your code should be changed to

Code: Select all

 if "!key!" == "!cr!" Echo.

Re: How <cr> behaves in a variable?

Posted: 21 Aug 2019 10:17
by Szecska
Thank you very much Jeb :!:
It works :)