- 1 year ago
Windows Batch SET with Variable Substring Length?
In a Windows batch script, you can use variable substring length with the SET
command by utilizing the %variable:~start,length%
syntax. Here's an example:
batch@echo off
set "string=Hello, World!"
set "substring=%string:~7,5%"
echo %substring%
In this example, the SET
command is used to assign the value "Hello, World!" to the string
variable. The substring
variable is then set to a substring of string
using the syntax %string:~start,length%
. In this case, %string:~7,5%
extracts a substring starting at index 7 (0-based index) and with a length of 5 characters.
The output of the above script will be:
World
You can adjust the start
and length
values to extract different parts of the string. If the length
parameter is omitted, the substring will extend to the end of the string.
Note that the SET
command in Windows batch scripts does not support negative indexing. If you need more advanced string manipulation capabilities, you might consider using a different scripting language like PowerShell or using external tools like sed
or awk
.