Reputation: 23
How do I loop through the files in the folder and parse the portion of the file name and store it in a variable + echo?
Folder contains following files and need to pull out the year:
Actual2015.txt
Actual2016.txt
What I tried:
for %%f in (*.txt) do (
set year=%%f:~7,4%
echo %year%
)
result should be:
2014
2015
Upvotes: 2
Views: 521
Reputation: 14290
Or you can take the last 4 characters of the file name if you know that last 4 are what you want.
@echo off
setlocal enableDelayedExpansion
for %%f in (*.txt) do (
set fname=%%~nf
set year=!fname:~-4!
echo !year!
)
pause
Upvotes: 1
Reputation: 57252
you need delayed expansion:
setlocal enableDelayedExpansion
for %%f in (*.txt) do (
set fname=%%nf
set year=!fname:~6,4!
echo !year!
)
Upvotes: 2