Split string to characters

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
BatchGuy
Posts: 8
Joined: 13 Nov 2019 08:35

Re: Split string to characters

#31 Post by BatchGuy » 10 Dec 2019 16:31

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

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

Re: Split string to characters

#32 Post by aGerman » 11 Dec 2019 12:00

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

Sponge Belly
Posts: 231
Joined: 01 Oct 2012 13:32
Location: Ireland
Contact:

Re: Split string to characters

#33 Post by Sponge Belly » 31 Dec 2023 13:44

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

Post Reply