Using Batch Tokens and Delims on filenames?

November 10, 2009 at 19:19:00
Specs: Windows 7
Ok, I've spent hours researching this, and am still confused. What I have so far is this (and I'm a super beginer, so please forgive me if it's a stupid question):

FOR /F "tokens=2,3 delims=_" %%A IN ('%FILENAME%\*.tga') DO IF "%%B"=="[%1]" SET IMGTYPE=%%A

I'm trying to get my batch to look into the folder 'FILENAME' (which contains only one .tga file, with a naming convention of: 'txt_****_x.tga', where * and x is a number. I'm trying to get it to assign the 'x' number to my IMGTYPE variable, but it either sais it can't find the directory, or just leaves it blank when I ECHO it to my .txt

A thousand thanks, Milun

See More: Using Batch Tokens and Delims on filenames?

Report •

November 10, 2009 at 21:03:14
This could be simplified but I'm unsure if **** could contain a dot ".".

for %%a in ("%filename%\*.tga") do (
    for /f "tokens=3 delims=_" %%b in ("%%~na") do (
        if "%%b"=="%~1" set IMGTYPE=%%b

...or just leaves it blank when I ECHO it to my .txt

Be sure to do the redirection before the echo to avoid problems with type redirection or trailing spaces.

>> "filename" echo %imgtype%

Batch Variable how to

Report •

November 10, 2009 at 23:03:42
Sorry, but I copy and pasted you text, and I think I did it wrong, as it's still not ECHOing anything. FILENAME variable is usually assigned something like **Name, where each * is a number, so 24Water for example. And I copied your

>> "filename" echo %imgtype%

exactly like that. Oh, and the **** had no dots, they were just 4 simple numerals like 3534.

Report •

November 10, 2009 at 23:16:06
Ok lets simplify then:

for /f "tokens=3 delims=_." %%b in ('dir "%filename%\*.tga"') do set IMGTYPE=%%b

I did test it but I have no way of knowing what %filename% or
%1 contain in your situation.

Batch Variable how to

Report •

Related Solutions

November 11, 2009 at 00:19:40
hello. I am also very much beginner Xp batch. I ran some tests, and i could not get any variation of "tokens=" other
than value 1 to work. However, i have seen Judago's posts and I know he is experienced. something we're both doing wrong.
my tests resulted with anything other than 1 supplied for "tokens =" failed. I tried it a bunch of variations and the script went into suspended animation. I, too, will risk looking stupid/ridiculous, but that was my experience.

in the meantime, maybe try changeing:
FOR /F "tokens=2,3 delims=_" %%A IN ('%FILENAME%\*.tga') DO IF "%%B"=="[%1]" SET IMGTYPE=%%A

for /f "tokens=1,3 delims= " %%A in ('%filename%\*.tga') do (
if %%C=="[%1]" set imgtype=%%C

the var. names are assigned from the %% specification (a,b,
etc) upwards for each token. the %% spec can be any single-letter alpha name (a-z, A-Z) and each token assigned to the var beginning with the first spec (%%a etc) till end of inputline.
to me, it *should* work as you have written: specif tokens=
2,3 would skip over token (field) one, assigning field 2 to the %% specification and moving up from there. Why don' it work that way?? i'm missing something...
If it's stupid question, I'm in same boat cause i don' kno ether.

Report •

November 11, 2009 at 14:40:21
Thanks! I tried Judago's example, but for some reason it only worked if token 3 had two digits. So by removing the dot in:

...ens=3 delims=_." %%b...

I managed to make it work, but it returned the number and .tga at the end. For my specific need, I can use this properly (I needed to know the number and assign a name to it accordingly, so the .tga at the end doesn't matter to an if statement). I tried nbrane's suggestion too, and it yielded the same results.

Thanks a lot for all your help.

Report •

Ask Question