This can be tricky if you do not know how to do this, it should also be noted depending on what text you are replacing your method you will use will change.
These methods can also be used to remove text from strings.
Standard Text String Replacement
This is straight forward and you should just use the example below. This will not allow the use of %
@echo off set mystring=This is a water bottle. echo %mystring% set mystring=%mystring:water=glass% echo %mystring%
Outputs as follows:
This is a water bottle. --> This is a glass bottle.
% Character and String Replacement
To replace % you need to do a few more steps because % is a special character. This method will also allow you to change normal strings aswell.
The following example will convert a URL that has had the slashes replaced with HTML entities and of course these include % which is a special character in batch files and cannot be escaped.
We use EnableDelayedExpansion to change when these variables are parsed allowing us to convert the %5C to \ so the URL can be used appropriately. We are just using echo here but the variable can be used like normal variable aswell.
The example will convert the following URL
D:%5Cwebsites%5Chtdocs%5Cprojects%5Cqwcrm%5Csrc%5Ccache%5Csmarty%5Ccompile%5Cd0d7cab3fc900.file.financial.tpl.php --> D:\websites\htdocs\projects\qwcrm\src\cache\smarty\compile\d0d7cab3fc900.file.financial.tpl.php
The Example
@echo off :: Each variable is to be expanded at execution time rather than at parse time setlocal EnableDelayedExpansion :: Set the URL that has been passed to the batch file as a commandline parameter set URL=%1 :: Convert '%5C' --> '\' - Notice the command is wrapped in ! set URL=!URL:%%5C=\! :: Optional endlocal :: Output the URL echo %URL%
Links
- General
- How a command line is interpreted | programming-books.io - How a command line is interpreted including quoting and escaping
- External commands | programming-books.io
- External commands available to Windows command interpreter are separate executable program files, supplied with the operating system by Microsoft, or bundled as standard with the third-party command interpreters. By replacing the program files, the meanings and functions of these commands can be changed.
- A list and description of all windows external commands that can be used in your batch file.
- Prevent cmd Window for Batch File from Closing
- Learn how to stop cmd (command prompt) window for batch files from closing. The .bat files automatically close window after execution.
- cmd /k
- Batch File Comment (Remark) - Windows - ShellHacks
- How to comment (leave remarks) in a batch file in Windows. How to comment out and uncomment a line or a block of code (multiple lines) in a batch file.
- :: = comment out and dont display
- REM/rem = comment out but still show
- Setlocal - Local variables - Windows CMD - SS64.com - setlocal, EnableExtentions, EnableDelayedExpansion and more explained here.
- Character Escaping
- Batch files - Escape Characters | Rob van der Woude's Scripting Pages - A list of special characters and how to escape them.
- Replace character of string in batch script - Stack Overflow
- Removing Characters from a string
- Batch Script - Replace a String | Tutorials Point - Batch Script - Replace a String, To replace a substring with another string use the string substitution feature.
- windows - Batch â Delete Characters in a String - Super User
- Batch Script - Replace a String - GeeksforGeeks - In this article, we are going to Replace a substring with any given string.
- Percent Symbol '%'
- How can I use the percent symbol (%) in a batch file? | ITPro Today
- Percent symbols - % - Windows CMD - SS64.com - A common question asked by new batch file programmers is "why do we need to double the percent symbols when writing a FOR command?". On the command line %A works fine, so why does a batch need %%A?
- How do you use % as a literal character in a batch file / on the command line?
- How to replace character "-" by "%_%" in a batch file? - Super User - This is the article I got my solution from for swapping out %5C
- escaping - Ignore percent sign in batch file - Stack Overflow
- windows - How to replace string inside a bat file with command line parameter string - Stack Overflow
- String replacement in Windows batch - Super User - This explains the process of how a batch file is processed and what the use of EnableDelayedExpansion is for
- Replace %%20 in string using windows batch script - Stack Overflow