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.
@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
1. What does %greeting:~0,5% extract from a variable named greeting containing 'Hello World'?
2. How do you delete all occurrences of a substring 'foo' from a variable named path?
3. Why does batch typically compute string length using a loop instead of a single function call?
4. What is the role of the 'delims' option in a FOR /F command?
5. By default, is the %variable:old=new% replace operation case-sensitive?
Was this page helpful?
You May Also Like
Environment Variables in Batch
Understand how Windows Batch scripts create, read, scope, and persist environment variables, including the tricky topic of delayed expansion.
Batch Script Arguments
Learn how Windows Batch scripts read command-line arguments using positional parameters, SHIFT, and path modifiers like %~dp0.
Batch Script Functions with CALL
Learn how to simulate reusable functions in Windows Batch using labeled subroutines invoked with CALL, including parameter passing and local variable scope.
Related Reading
Related Study Notes in Microsoft Technologies
Browse all study notesWindows 10 / UWP Development Study Notes
.NET · 30 topics
Microsoft TechnologiesMFC (Microsoft Foundation Classes) Study Notes
C++ · 30 topics
Microsoft TechnologiesSilverlight Study Notes
.NET · 30 topics
Microsoft TechnologiesXAML Study Notes
.NET · 30 topics
Microsoft TechnologiesWPF (Windows Presentation Foundation) Study Notes
.NET · 30 topics
Microsoft TechnologiesMVVM Design Pattern Study Notes
.NET · 30 topics