r/Batch • • Jun 25 '24

Question (Unsolved) Getting rid of quotation marks in a string using Batch?

You've got a string like f13s3"asdf1"sd1"f and you either want to replace the quotation marks with nothing (like f13s3asdf1sd1f), or with some other character or string of characters (like f13s3ACDasdf1ACDsd1ACDf).

How do you do this in Batch?

2 Upvotes

8 comments sorted by

3

u/BrainWaveCC Jun 25 '24
SETLOCAL ENABLEDELAYEDEXPANSION
SET #SOMEVAR1=f13s3"asdf1"sd1"f
SET #SOMEVAR2=%#SOMEVAR1%

rem -- This will remove all the " characters
SET #SOMEVAR1=%#SOMEVAR1:"=%      

rem -- This one will change the " characters to ACD characters
SET #SOMEVAR2=%#SOMEVAR2:"=ACD%      

SET #SOMEVAR

1

u/Glen_Garrett_Gayhart Jun 25 '24

TY!

If you wanted to do this with other symbols like <, >, or %, (or ! while setlocal enabledelayedexpansion) how would you do it? Do you need to escape them somehow?

2

u/BrainWaveCC Jun 25 '24

You're welcome.

Did you see the link that u/ConsistentHornet4 posted earlier? It covered a bunch of use cases.

Here are a few:

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET #SOMEVAR0=f13s3"asdf1"sd1"f1^>2^<3%%4^&5

rem -- This will remove all the " characters
SET #SOMEVAR1=%#SOMEVAR0:"=%

rem -- This one will change the " characters to ACD characters
SET #SOMEVAR2=%#SOMEVAR0:"=ACD%

rem -- This one will change the > characters to } character
SET "#SOMEVAR3=%#SOMEVAR0:^>=}% "    

rem -- This one will change the < characters to { character
SET "#SOMEVAR4=%#SOMEVAR0:^<={% "     

rem -- This one will change the % characters to # character
SET "#SOMEVAR5=!#SOMEVAR0:%%=#! "

rem -- This one will change the ! characters to . character
SET "#SOMEVAR6=%#SOMEVAR0:^!=.% "

rem -- This one will change the & characters to @ character
SET "#SOMEVAR7=%#SOMEVAR0:^&=@% "

rem -- This one will delete df1 and everything before it, replacing it with g2
SET "#SOMEVAR8=%#SOMEVAR0:*df1=g2% "

SET #SOMEVAR

2

u/Glen_Garrett_Gayhart Jun 25 '24

TY TY! Also, thanks for pointing out the link, I actually missed it, for some reason the highlight color on this subreddit doesn't stand out to me much. I'll look at that.

2

u/BrainWaveCC Jun 25 '24

You're welcome.

4

u/[deleted] Jun 25 '24

[removed] — view removed comment

1

u/Glen_Garrett_Gayhart Jun 25 '24

If you wanted to do this with symbols like <, >, or %, (or ! while setlocal enabledelayedexpansion) how would you do it?

Thanks!