Page 3 of 3

Re: Split string to characters

Posted: 10 Dec 2019 16:31
by BatchGuy
The error message flashing by when my batch crashed, is indeed sth. like "( was unexpected at this time."
Which I couldn't figure out at all until now, thx to your highly appreciated clarification.
And sort of confirms that the weird cmd.exe design --- if any --- is really rocket science! :?

BG

Re: Split string to characters

Posted: 11 Dec 2019 12:00
by aGerman
BatchGuy wrote:
10 Dec 2019 16:31
And sort of confirms that the weird cmd.exe design --- if any --- is really rocket science! :?
The CMD is buggy as hell and Microsoft is absolutely aware of that. When I called it an "evil beast", Michael Niksa (one of the Console/Terminal core developers) agreed with me :D
However, the decision of Microsoft is to avoid any updates of cmd.exe. The official reason is that they are afraid to "break the world" as soon as they change the current behavior of the CMD. And I tend to agrre with them. If you look around in this forum you will realize that we all try hard to take advantage of any bug and every undefined behavior of cmd.exe and the Windows command line tools. Microsoft argues that they would get loads of complaints if undocumented "features" disappear with an update people have been used to for decades. But in my opinion there is another reason. They're trying to get us to use PowerShell instead of CMD. They already made it the default shell on Win 10. And again I tend to agree with their decision (even if it may sound odd from a moderator in a Batch forum). PowerShell enables you to get access to any possibility of .NET, including graphical interfaces and the invocation of Windows API functions. However, Microsoft promises to continue shipping the CMD along with future Windows versions. People who like Batch don't have to worry. But I'm convinced that it will lose importance over time...

Steffen

Re: Split string to characters

Posted: 31 Dec 2023 13:44
by Sponge Belly
Hi Again! :)

I updated my split string to characters code based on Jeb’s nested for loops technique:

Code: Select all


@echo off & setLocal enableExtensions disableDelayedExpansion

set "hex=0 1 2 3 4 5 6 7 8 9 A B C D E F"
set str="!%%^&*~|<>?"
set "chr=" & set "N=0"

setLocal enableDelayedExpansion
if not defined str goto break
for %%A in (0 1) ^
do for %%B in (%hex%) ^
do for %%C in (%hex%) ^
do for %%D in (%hex%) do (
    set "chr=!str:~0x%%A%%B%%C%%D,1!"
    if defined chr (set /a N+=1 & echo(!N!:!chr!) else goto break
)
:break
echo(length of string: %N%
endLocal

endLocal & goto :EOF

The loop in the code above isn’t infinite, but it doesn’t have to be! It goes from 0 as far as 8191, the maximum string length. But most strings aren’t that long. As soon as the chr variable becomes undefined, the program jumps to the :break label. And unlike buggy for /l loops, plain for loops stop iterating when exited by a goto command.

HNY! 8)

- SB