Page 1 of 1

Replace greater than signal (">")

Posted: 23 Feb 2018 09:10
by gustavo.lago
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

Re: Replace greater than signal (">")

Posted: 23 Feb 2018 10:04
by Squashman
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.

Re: Replace greater than signal (">")

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

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

Re: Replace greater than signal (">")

Posted: 23 Feb 2018 13:54
by Squashman
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.