How to Extract Part of a Variable in Batch Scripting
In this article, we will discuss how to extract a part of a variable in batch scripting using the set and call commands. Specifically, we will focus on extracting characters from a string using the echo command and the substring syntax.
Background
In batch scripting, variables are used to store values that can be used and manipulated throughout the script. Often, there is a need to extract a part of a variable, such as a substring, to use in further processing. This can be achieved using the set and call commands in combination with the echo command.
Extracting a Substring Using the Echo Command
To extract a substring from a variable in batch scripting, you can use the echo command with the substring syntax. The syntax for extracting a substring from a variable var is as follows:
echo %var:~start,length%Where start is the starting position of the substring and length is the length of the substring. The starting position is zero-indexed, meaning that the first character in the string is at position 0.
Example
Let's say we have a variable keys that contains the string "ABCD". To extract the second and fourth characters from this string, we can use the following code:
set "keys=ABCD"
echo %keys:~1,1%
echo %keys:~4,1%The output of this code will be:
B
DUsing the Call Command for Substring Extraction
Another way to extract a substring from a variable in batch scripting is by using the call command. This method is useful when you need to extract a substring from a variable that has already been defined earlier in the script. The syntax for using the call command to extract a substring is as follows:
call :substring_label var
...
:substring_label
set "substring=%~1"
set "substring=%substring:~start,length%"
echo %substring%
goto :eofWhere substring_label is the label for the subroutine, var is the variable containing the string, start is the starting position of the substring, and length is the length of the substring. The call command invokes the subroutine with the variable as an argument, and the subroutine extracts the substring and echoes it to the console.
Example
Let's say we have a variable keys that contains the string "ABCD". To extract the second and fourth characters from this string using the call command, we can use the following code:
set "keys=ABCD"
call :extract_char keys 1 1
call :extract_char keys 4 1
goto :eof
:extract_char
set "substring=%~1"
set "substring=%substring:~%2,%3%"
echo %substring%
goto :eofThe output of this code will be:
B
DIn this article, we have discussed how to extract a part of a variable in batch scripting using the set and call commands. Specifically, we have focused on extracting characters from a string using the echo command and the substring syntax. By using these techniques, you can easily extract the parts of a variable that you need for further processing in your batch scripts.
References
- Microsoft Docs. (2021). Set. https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/set_1
- Microsoft Docs. (2021). Call. https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/call
- SS64.com. (2021). Echo. https://ss64.com/nt/echo.html
- Rob van der Woude. (2021). Windows CMD Scripting. https://www.robvanderwoude.com/batchstart.php