I need help to make a batch code (if it's possible) to get substring from filename. My filename can be like (filename lenght is changing):
7_D_D1_012345678-2015-07-07.pdf
8_A_087654321-2015-07-07.pdf
10_D_D1_011122558-2015-07-07.pdf
100_C_CCC1_C2_C3_C4_055555555-2015-07-07.pdf
file number - from left to first _
id1 - from 1 to n string with _ separator; for example C_C1_C2_C3_C4
id2 - always 9 digits; for example 011122558
date - for example 2015-07-07
extension .jpg
How to loop substring (file number, id1, d2, date) for all filenames in folder and put it to my code
convert - "file number" -annotate "id1" -annotate2 "id2" -annotate "date"
for example:
convert - "01" -annotate "C_C1" -annotate2 "012345678" -annotate "2015-07-07"
Thanks for help.
pure batch. Simple string manipulation mixed with tokenization. No need for additional utilities.
(g.txt
holds your example file names; could be replaced by 'dir /b /a-d'
)
@echo off
for /f %%i in (g.txt) do call :process %%i
goto :eof
:process
set x=%1
set ext=%x:*.=%
for /f "delims=_" %%i in ("%x%") do set fileno=%%i
for /f "tokens=1,*delims=-" %%i in ("%x%") do (
set x1=%%i
set x2=%%j
)
for /f "tokens=1,* delims=." %%i in ("%x2%") do (
set dat=%%i
set ext=%%j
)
set id2=%x1:~-9%
for /f "tokens=1,* delims=_" %%i in ("%x1:~0,-10%") do set id1=%%j
echo filename %x%
echo ------------------------
echo Nr. %fileno%
echo ID1 %id1%
echo ID2 %id2%
echo Date %dat%
echo Ext. %ext%
echo ------------------------
echo convert - "%fileno%" -annotate "%id1%" -annotate2 "%id2% -annotate "%dat%"
echo(
echo(
goto :eof