Replace greater than signal (">")

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
gustavo.lago
Posts: 4
Joined: 23 Feb 2018 08:52

Replace greater than signal (">")

#1 Post by gustavo.lago » 23 Feb 2018 09:10

Hi!

I'm trying to replace a string in a text file, but the string has the greater sign.

original.txt file:

Code: Select all

url = "http://myurl/<API_KEY>/xx"
I need to replace the <API_KEY> with variable contents:

Code: Select all

set var=12345678
For /f "tokens=* delims=/" %%a in (original.txt) do (
  Set str=%%a
  set str=!str:<API_KEY>=%var%!
  echo !str! >>out.txt
)
I have tried the script without the <> and it works. Are there any alternatives without using third-party programs or VBS, JS, etc?

Code: Select all

 INFO.BAT version 1.4
--------------------------------------------------------------------------------
Windows version        :  Microsoft Windows [versão 10.0.10586]
Product name           :  Windows 10 Pro, 64 bit
Performance indicators :  Processor Cores: 4      Visible RAM: 16686620 kilobytes

Date/Time format       :  (dd/mm/yy)  23/02/2018  11:57:33,37
__APPDIR__             :  C:\Windows\System32\
ComSpec                :  C:\WINDOWS\system32\cmd.exe
PathExt                :  .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
Extensions             :  system: Enabled   user: Enabled 
Delayed expansion      :  system: Disabled  user: Disabled
Locale name            :  pt-BR       Code Pages: OEM  850    ANSI 1252
DIR  format            :  23/02/2018  09:27     7.103.500.288 pagefile.sys
Permissions            :  Elevated Admin=No, Admin group=Yes
User    AutoRun found  :      AutoRun    REG_SZ    "C:\PROGRA~2\clink\043213~1.9\clink.bat" inject --autorun 

                          Missing from the tool collection:  debug

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Replace greater than signal (">")

#2 Post by Squashman » 23 Feb 2018 10:04

Use quotes to protect special characters.

Code: Select all

Set "str=%%a"
set "str=!str:<API_KEY>=%var%!"
Going to assume you have delayed expansion enabled higher up in your script.

gustavo.lago
Posts: 4
Joined: 23 Feb 2018 08:52

Re: Replace greater than signal (">")

#3 Post by gustavo.lago » 23 Feb 2018 11:22

So simple when you know it! Worked well!

Thank you! I've been trying this for 2 days.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Replace greater than signal (">")

#4 Post by Squashman » 23 Feb 2018 13:54

gustavo.lago wrote:
23 Feb 2018 11:22
So simple when you know it! Worked well!

Thank you! I've been trying this for 2 days.
It is kind of buried in the help for the SET command.

Code: Select all

If you use any of the logical or modulus operators, you will need to enclose the expression string in quotes.

Post Reply