解决 批处理 for 体内的字符串截取问题
背景:
笔记想读取bat同目录下 a文件夹下的所有文件名,并在for循环体下截取前两位字符串,如图
代码块
@echo off &setlocal enabledelayedexpansion
set fa=C:\Users\GilbertXiao\Desktop\a\a
for /f "delims=" %%a in ('dir /b/on %fa%\*.*') do (
set fullname=%%a
echo !fullname!
for /f "tokens=1,2 delims=." %%b in ("!fullname!") do (
set name=%%b
echo !name!
rem substr the name
set shortname=!name:~0,2!
echo !shortname!
rem write the shortname into b.txt
echo !shortname!>b.txt
)
)
pause