100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Batch

String Manipulation in Batch

Master substring extraction, search-and-replace, and line/token parsing in Windows Batch using variable expansion modifiers and FOR /F.

Advanced Batch ScriptingIntermediate10 min readJul 10, 2026
Analogies

Strings Are Just Text in Variables

Batch treats every variable as a string by default, and it provides a compact substring syntax built directly into variable expansion rather than separate string functions: %variable:~start,length% extracts a substring, where a negative start counts from the end of the string and a negative or omitted length behaves specially. This syntax looks cryptic at first, but it is the backbone of nearly all text processing in batch, since there is no dedicated string library like Python's str methods, only this expansion-modifier syntax layered onto the existing %variable% mechanism.

🏏

Cricket analogy: It is like reading only the last two digits of a jersey number off a scorecard by counting backward from the end of the printed string, rather than needing a dedicated 'jersey number parser' tool.

Length, Substring, and Replace

Because there is no LEN() function, string length is measured indirectly, most commonly with a loop that uses the substring operator to test increasing offsets until it hits an empty result, or with a FOR /F trick combined with a helper subroutine. Substitution is more direct: %variable:old=new% replaces every occurrence of 'old' with 'new' in one pass, case-sensitively, and passing an empty replacement like %variable:old=% effectively deletes all occurrences of 'old', which is a common way to strip unwanted characters such as quotes or trailing slashes from a path.

🏏

Cricket analogy: Measuring length by probing offsets is like estimating a boundary's exact distance by walking paced steps from the stumps until you hit the rope, since there's no laser rangefinder built into the ground; %var:old=new% is like a groundskeeper repainting every instance of a faded boundary marker in one pass.

batch
@echo off
SET path=C:\Users\John\Documents\
SET nopath=%path:C:\Users\John\=%
echo Trimmed: %nopath%

SET greeting=Hello World
SET first5=%greeting:~0,5%
SET last5=%greeting:~-5%
echo First 5: %first5%
echo Last 5: %last5%

SET fixed=%greeting:World=Batch%
echo Replaced: %fixed%

Splitting Strings with FOR /F and Tokens

For anything more structured than simple substring or replace operations, such as splitting a CSV line or parsing key=value pairs out of a config file, FOR /F is the workhorse: it reads input line by line and further splits each line into tokens using the delims option, assigning each token to a successive loop variable like %%a, %%b, and so on. The usebackq and skip options handle quoted filenames and header rows respectively, and combining FOR /F with SET inside the loop body is the standard way to build a lightweight config-file parser or CSV importer entirely in native batch without external tools.

🏏

Cricket analogy: It is like a scorer parsing a ball-by-ball commentary line into separate fields, over number, bowler, runs, and extras, by splitting on a fixed delimiter, feeding each field into its own column of the scorebook.

FOR /F "tokens=1,2 delims=," %%a IN (file.csv) DO echo %%a - %%b splits each line on commas and assigns the first token to %%a and the second to %%b; use tokens=* to capture everything from a given token onward as one string.

String comparisons and substring operators in batch are case-sensitive by default for replace (%var:old=new%) but IF string comparisons are case-insensitive unless you add /I is omitted — always verify case behavior explicitly with a test string, since mixing these up silently breaks matching logic.

  • Batch has no dedicated string type; all string operations are expansion modifiers on %variable%.
  • %variable:~start,length% extracts a substring; negative start counts from the end of the string.
  • There is no LEN() function; length is typically measured by looping and probing substring offsets.
  • %variable:old=new% replaces all occurrences of 'old' with 'new'; an empty replacement deletes 'old'.
  • FOR /F splits input line by line and further into tokens using the delims option.
  • tokens=1,2 delims=, assigns comma-separated fields to successive loop variables like %%a and %%b.
  • String replace is case-sensitive by default, while IF string comparisons are case-insensitive unless specified otherwise.

Practice what you learned

Was this page helpful?

Topics covered

#Batch#WindowsBatchScriptingStudyNotes#MicrosoftTechnologies#StringManipulationInBatch#String#Manipulation#Strings#Just#StudyNotes#SkillVeris