issue with IF statement and weird character

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
ayce
Posts: 9
Joined: 27 Dec 2021 17:15

issue with IF statement and weird character

#1 Post by ayce » 27 Dec 2021 17:36

I'm testing some code to handle 1-character checks and functions in a CMD script, and I stumbled on this:

C:\> if X+==+ echo K
K
C:\>

BUT - I changed the command slightly, in the way that the X is a special character, not just the letter X.
When I tried to copy this full command, Windows didn't allow for the special character to be copied,
which sorts of explains the issue. The special character is just ignored, when copying, but also by the CMD command.

I'm not sure at this point, but it's not very common for any CMD command to just ignore a character, no ?
I would expect some normal output, or a syntax error

The special character may not be an ASCII character, or something else. But, if I can type it in the command ... why would it then totally ignore it ?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: issue with IF statement and weird character

#2 Post by aGerman » 27 Dec 2021 18:15

It would help to know what special character you're talking about in particular.

Steffen

ayce
Posts: 9
Joined: 27 Dec 2021 17:15

Re: issue with IF statement and weird character

#3 Post by ayce » 28 Dec 2021 05:17

aGerman wrote:
27 Dec 2021 18:15
It would help to know what special character you're talking about in particular.

Steffen
I can't paste it because the internal Windows text copy feature also removes it from the text being pasted
It looks like a vertical line, like a pipe symbol, but then it also has something going to the right, on top of that line.
It looks a bit like a little flag, on a pole (waving to the right)

I can't see it in the map of 256 characters of the ASCII table
But I can say the symbol is included as part of the name of a file (NTFS format, Windows 10)


aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: issue with IF statement and weird character

#4 Post by aGerman » 28 Dec 2021 05:47

You need to find a way to get the name into a file where you can observe the value of the characters using a HEX editor.

Steffen

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: issue with IF statement and weird character

#5 Post by Compo » 28 Dec 2021 05:53

The first thing you should note is that you are not using the recommended correct syntax for a string comparison with the IF command. What you should always use is:

Code: Select all

IF "X" == "" ECHO K
Because the doublequotes protect possible poison characters

If that alone does not fix your issue, then I'd suggest that you first of all ensure that your batch file is written to use a codepage within which your, as yet unknown, character exists.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: issue with IF statement and weird character

#6 Post by aGerman » 28 Dec 2021 06:04

You are right Compo, but I guess discarding the character does probably mean it's some kind of unprintable control character.

Steffen

ayce
Posts: 9
Joined: 27 Dec 2021 17:15

Re: issue with IF statement and weird character

#7 Post by ayce » 28 Dec 2021 07:46

If the character is unprintable, how come I see it, using DIR in CMD, and in the name of the file in my Windows Explorer ? My Windows is not setup for any exotic language or so.
When it is unprintable, how do I end up describing how the character looks ?

The issue actually is not the IF command, the issue is that a character gets lots when being parsed, for example in an IF command.

ayce
Posts: 9
Joined: 27 Dec 2021 17:15

Re: issue with IF statement and weird character

#8 Post by ayce » 28 Dec 2021 07:53

aGerman wrote:
28 Dec 2021 05:47
You need to find a way to get the name into a file where you can observe the value of the characters using a HEX editor.

Steffen
That would be great, but I'm not sure how to do that. To be clear, then we would like to get the name of the file, not the content, listed as a hexadecimal result.

I'm actually already analyzing the name of the file, and this thread is stating I have issues with it because - non surprisingly - there's some weird characters messing up everything. I'm expecting exactly this sort of behaviour, now need to work around it. The issue is not just this character, it's an issue with all these kind of characters. To my knowledge, these are printable characters, outside of ASCII. I don't need to do anything with these characters, other than removing them. At the moment, the code is thinking this character is a whitespace (because the code is written like that), and that causes issues.

ayce
Posts: 9
Joined: 27 Dec 2021 17:15

Re: issue with IF statement and weird character

#9 Post by ayce » 28 Dec 2021 08:02

Compo wrote:
28 Dec 2021 05:53
The first thing you should note is that you are not using the recommended correct syntax for a string comparison with the IF command. What you should always use is:

Code: Select all

IF "X" == "" ECHO K
Because the doublequotes protect possible poison characters

If that alone does not fix your issue, then I'd suggest that you first of all ensure that your batch file is written to use a codepage within which your, as yet unknown, character exists.
I know, you're right. but I'm lazy - like most programmers - and I just don't have the double quotes in this IF statement, because here it doesn't matter.
Rest assured. I know how it works. The one character I'm analyzing can also be just one double quote, so I must do extra effort there anyway. I don't have issues or questions regarding that, I know how double quotes and IF statements work.

But you're right, double quotes should always be used !

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: issue with IF statement and weird character

#10 Post by aGerman » 28 Dec 2021 08:10

Lots of graphical interfaces make unprintable characters visible. They may show up as empty box or any other kind of placeholder. The NTFS supports almost every character to be used in a file name, even those that Windows doesn't allow you to use. This is often an issue if you get files created on Linux (or using WSL).
Run this batch file in the folder with the suspicious file.

Code: Select all

@echo off &setlocal
powershell /c "gci|foreach{$_.name|Format-Hex -encoding Unicode}"
pause
Steffen

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: issue with IF statement and weird character

#11 Post by penpen » 28 Dec 2021 08:25

The actual codepage of the "cmd.exe" might be interesting (using "chcp.com"), in case it's no control character, but some form of whitespace-character like some version of a "comma".

penpen

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: issue with IF statement and weird character

#12 Post by Compo » 28 Dec 2021 09:22

As I initially stated, and also mentioned by penpen above, it may simply be a chosen codepage issue.

I'd suspect your actual character is most likely an extended ASCII upper or lower case i with a grave, or acute. So in codepage 1252 that would be dec 204 and 205 (hex CC and CD), or dec 236 and 237 (hex EC and ED). Examples:

Code: Select all

Ì ì Í í
Please be aware that in the used font here, and in your console, the upper case version may, or may not, include a horizontal top and base, and the lower may or may not include a horizontal base.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: issue with IF statement and weird character

#13 Post by Aacini » 28 Dec 2021 11:31

ayce wrote:
28 Dec 2021 05:17
aGerman wrote:
27 Dec 2021 18:15
It would help to know what special character you're talking about in particular.

Steffen
I can't paste it because the internal Windows text copy feature also removes it from the text being pasted
Do the following:
  1. From the cmd.exe command-line, enter: dir > output.txt
  2. Open output.txt, Select all, Copy text
  3. Below this reply, post a new reply.
  4. Enter left-square-braquet+code+right-square-braquet in a line, then paste the text below it, and terminate with left-square-braquet+/code+right-square-braquet
  5. Post the reply...

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: issue with IF statement and weird character

#14 Post by Compo » 28 Dec 2021 12:11

Just be aware everyone that the OP specifically stated
ayce wrote:
27 Dec 2021 17:36
I can type it in the command
Which means that they do know what the character is, and how to type it, and there is therefore no reason for them not to provide that information.

IMO, I would suggest that this is not a specific issue, and the OP is hoping that somebody will come along with a solution to handle every single possible character, without any restrictions, regardless of language, locale, character set, or encoding etc.

ayce
Posts: 9
Joined: 27 Dec 2021 17:15

Re: issue with IF statement and weird character

#15 Post by ayce » 29 Dec 2021 13:50

Aacini wrote:
28 Dec 2021 11:31
ayce wrote:
28 Dec 2021 05:17
aGerman wrote:
27 Dec 2021 18:15
It would help to know what special character you're talking about in particular.

Steffen
I can't paste it because the internal Windows text copy feature also removes it from the text being pasted
Do the following:
  1. From the cmd.exe command-line, enter: dir > output.txt
  2. Open output.txt, Select all, Copy text
  3. Below this reply, post a new reply.
  4. Enter left-square-braquet+code+right-square-braquet in a line, then paste the text below it, and terminate with left-square-braquet+/code+right-square-braquet
  5. Post the reply...
If you redirect to a file, he converts the character to a ?
So, step 1 fails

Now, that is interesting, because the string itself is a name of a file, and as you know, a ? is not an allowed character in the name of a file. So, also redirecting messes up the encondig of the character, like does the IF statement.

Post Reply