How <cr> behaves in a variable?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Szecska
Posts: 17
Joined: 15 Aug 2019 15:29
Location: Hungary

How <cr> behaves in a variable?

#1 Post by Szecska » 21 Aug 2019 06:09

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

jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: How <cr> behaves in a variable?

#2 Post by jeb » 21 Aug 2019 07:26

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.

Szecska
Posts: 17
Joined: 15 Aug 2019 15:29
Location: Hungary

Re: How <cr> behaves in a variable?

#3 Post by Szecska » 21 Aug 2019 10:17

Thank you very much Jeb :!:
It works :)

Post Reply