A while back I thought I would try to share some of my favorite batch script snippets from my Google notebook but the shared notebook was quickly flagged as potentially dangerous and I didn't feel like contesting the flag so I pulled the snippets back into my private notebook.
I still want to share a few of my most useful snippets, so here is my favorite which I have heavily adapted it to fit many needs
Replace.bat
@Echo Off
Set /p Source=Text you would like to search:
Set /p Search=Text you would like to find:
Set /p Replace=Text to replace "%Search%" with:
Set Dest=
Call :Replacer "%Source%" "%Search%" "%Replace%" "Dest"
@Echo.%Dest%
GoTo :EOF
:Replacer
Set t=%~1
Call Set t=%%t:%~2=%~3%%
Set %~4=%t%
GoTo :EOF
So what's going on here?
Well, first you provide a string to search, what you are looking for, then what you want to replace it with. The script then calls a function which replaces all instances of the search term with the desired replace term. Simple right?
The interesting part comes in the line which does the search and replace.
variable t is a temporary variable which gets set as the source string then it is replaced with the new string in a call set statement in this strange part "Call Set t=%%t:%~2=%~3%%" which could be written as "Call Set Destination=%%t:%Search%=%Replace%%%".
What it expands to is fascinating as it double expands. Say you want to replace "is" with "was" in the string "This is your search string". You would end up with "Call Set t=%%This is your search string:is=was%%" which needs to be expanded one more time as it is now an environment variable replacer which the call statement executes to replace the instance of "is" with "was" so you end up with "This was your search string".
The final confusing piece of code sets the result to an arbitrary variable name which you feed the function in this case "Dest", this allows you to reuse the code with any destination variable you would like.
This has been a fascinating howbeit confusing piece of batch script that I have found to be very useful in so many situations.
Now for a few notes about batch code hygiene in which this sample employs.
- Never wrap strings used in set statements with quotes "". doing so will make string cleanup difficult. Set will set a variable to the entire line after the equals = sign.
- In batch, all variables are global, so work with them accordingly. You may end up with confusing results if you don't understand this.
- Create empty variables for readability. If you have a set of variables where one will be set later but you can set it blank near the top then fill it later, this is only for readability.
- When you pass a string to a function, even if you are passing a variable, quote "" it. You never know when you will be passing a string with spaces or some other weird characters, so in order to make sure it is all kept together quote it.
- When you retrieve a variable from a function use the tilde ~ eg. %~1. the tilde removes the surrounding quotes from a string passed through a function.
- When echoing a variable use "@Echo.". This prevents displaying the echo state such as "Echo off" if the variable ends up being empty. The @ prevents echoing the command if Eco is on.
No comments:
Post a Comment