issue with IF statement and weird character
Moderator: DosItHelp
issue with IF statement and weird character
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 ?
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 ?
Re: issue with IF statement and weird character
It would help to know what special character you're talking about in particular.
Steffen
Steffen
Re: issue with IF statement and weird character
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)
Re: issue with IF statement and weird character
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
Steffen
Re: issue with IF statement and weird character
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:
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.
Code: Select all
IF "X" == "" ECHO K
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.
Re: issue with IF statement and weird character
You are right Compo, but I guess discarding the character does probably mean it's some kind of unprintable control character.
Steffen
Steffen
Re: issue with IF statement and weird character
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.
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.
Re: issue with IF statement and weird character
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.
Re: issue with IF statement and weird character
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.Compo wrote: ↑28 Dec 2021 05:53The 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:Because the doublequotes protect possible poison charactersCode: Select all
IF "X" == "" ECHO K
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.
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 !
Re: issue with IF statement and weird character
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.
Steffen
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
Re: issue with IF statement and weird character
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
penpen
Re: issue with IF statement and weird character
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: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.
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
Ì ì Í í
Re: issue with IF statement and weird character
Do the following:
- From the cmd.exe command-line, enter: dir > output.txt
- Open output.txt, Select all, Copy text
- Below this reply, post a new reply.
- 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
- Post the reply...
Re: issue with IF statement and weird character
Just be aware everyone that the OP specifically stated
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.
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.
Re: issue with IF statement and weird character
If you redirect to a file, he converts the character to a ?Aacini wrote: ↑28 Dec 2021 11:31Do the following:
- From the cmd.exe command-line, enter: dir > output.txt
- Open output.txt, Select all, Copy text
- Below this reply, post a new reply.
- 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
- Post the reply...
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.