I have a number of files with the same naming scheme. As a sample, four files are called \"num_001_001.txt\", \"num_002_001.txt\", \"num_002_002.txt\", \"num_002_003.txt\">
I'm not sure whether this is officially documented, but you can simulate delayed expansion using the call statement:
@echo off
FOR /R %%X IN (*.txt) DO (
set temp=%%~nX
call echo directory %%temp:~0,7%%
)
Doubling the percent signs defers the variable substitution to the second evaluation. However, delayed expansion is much more straightforward.