Split and extract a string – StrSurr and StrSplit by Kvc.
Strings in Batch tend to be difficult to handle, especially for newer programmers. That is why many people use external plugins to avoid the hassle. These two plugins, created by Karanveer Chouhan (Kvc), allow developers to split a string into substrings, and extract substrings inside of Batch with ease. In this article, we will be demonstrating the two plugins syntax and use case. You can download the plugin on our official GitHub page.
DEMONSTRATION – EXTRACTING A STRING
Have you ever had the need in a script to extract a string in a formatted way? Maybe you are trying to get a specific piece of information from a string. You can use StrSurr to extract a string from a set of delimiters. The syntax of the plugin is here:
StrSurr <"delimiters"> <"dataString">
The plugin will take each instance of a substring between those delimiters, and return it to STDOUT. With this in mind, you would expect the following to happen:
As you may have taken from the above, it is capable of returning multiple instances at once. For example, say we have a string that contains configuration information that follows a format. Strings inside of “<>” is primary information, and strings inside of “[ ]” are secondary information.
Hopefully, this demonstrates the power of this function. Now, we will go on to the second demonstration in this set, splitting a string into substrings.
DEMONSTRATION – SPLITTING A STRING
Let’s say you have a string that contains a large amount of data in one line, such as a path variable. Typically, your path variable would look something like this:
C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) MComponents\IPT;C:\WINDOWS\System32\OpenSSH\;C:\ProgramData\chocolatey\bin;C:\Users\Zeek\Desktop\tcc;C:\Progra;c:\users\zeek\appdata\local\programs\python\python37\Scripts;C:\Program Files\dotnet\;C:\Program Files\CMake\bin;C:\Program Files\Git\cmd;C:\Program Files (x86)\LLVM\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\cygwin64\bin\java;C....
This is quite a mess. Especially when you are trying to debug a program that needs to use the system path. We can use the StrSplit plugin to quickly generate a clean and separated list of the items in our path. The syntax of the StrSplit command is as follows:
StrSplit <"delimiter"> <"dataString">
With this in mind, we can get our needed result in just one call to the plugin:
As you can see, we used the semicolon which separates each path variable in the string. Thus, each instance is one of the system path variables, and we get the multi-line result we were looking for. Another example of the power of plugins in Batch.
Hopefully, the GIFs, video, and demonstrations have shown the usefulness of the two plugins Kvc created. They are small, fast (especially because they are written in the C language) and portable. I hope you enjoyed reading this article, please share your thoughts below and make sure to join our Discord and YouTube community. Thank you for reading.
I am a programmer and a computer hobbyist in the United States. I make articles about a variety of topics.