Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Created December 30, 2017 07:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tanaikech/6cbd517b165979d3c7f2018e00391680 to your computer and use it in GitHub Desktop.
Save tanaikech/6cbd517b165979d3c7f2018e00391680 to your computer and use it in GitHub Desktop.
Splitting String by N Characters for Batch-file

Splitting String by N Characters for Batch-file

This sample script is for splitting string by N characters for batch-file. In this sample, after it retrieves N characters from the first character of STR, the N characters are removed from STR. This is repeated until the end of STR.

Sample script :

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "STR=ABCDEFGHIJKLMNOPQRSTUVWXYZ"

REM Split STR by N characters
SET "N=2"

:LOOP
SET "RES=%RES%!STR:~0,%N%! "
SET "STR=!STR:~%N%!"
IF DEFINED STR GOTO LOOP
ECHO "%RES:~0,-1%"

Result :

  • N=2
"AB CD EF GH IJ KL MN OP QR ST UV WX YZ"
  • N=5
"ABCDE FGHIJ KLMNO PQRST UVWXY Z"

Reference :

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment