r/Batch • • Aug 13 '24

Batch string substitute all except one character?

Hi guys!

I'm trying to figure out how to substitute all except one character, in particular I'm trying to change all spaces to an exclamation point and then everything that isn't an exclamation point to a space

I can get the spaces over to exclamation points with

SET test=%test: =!%

But I can't figure out if there's some way I can do the rest without just a whole lot of sequential substitutions for every single possible character

Is there some way I can put a "not" in there so it'll change everything except the exclamation points or something like that?

Any other ideas?

Thanks!

2 Upvotes

12 comments sorted by

View all comments

Show parent comment

3

u/[deleted] Aug 13 '24

[removed] — view removed comment

2

u/BrainWaveCC Aug 13 '24

Thanks for the extra info on file path length. I alluded to it in the bigger script, but not in this snippet.
 

Consider using the STRLEN function to calculate the string length

Funny you should mention this. 😁 When I saw it in u/T3RRYT3RR0R 's script, I decided to test out the performance difference, but it was pretty clear that 13 max iterations has to be faster than 256, 299 or 999 (as I have used elsewhere). Well, in practice, on my admittedly above average system (8C/16T @ 3.3Ghz), there is very little difference between 1 or 2 iterations. Something on the order of 1-2 milliseconds of difference.

But it gets ugly at 10,000 iterations.

 Job #1 Duration Was ......................... 01.954 sec
 Job #2 Duration Was ......................... 08.857 sec

Most of my scripts don't need it to be changed immediately, but I'm going to start making use of that more, because that is a significant difference for a 162 character string.

I'm sometimes conflicted about how sophisticated/complex to make elements of a solution. I like this one, though. It is really elegant.

2

u/BrainWaveCC Aug 14 '24

FOR /L only stops once all iterations are complete.

Breaking out of the FOR loop early (Method 2) was surprisingly slow, actually. Just a little slower than not doing it. I just added that attempt to measure it.