An official Batch Programming blog! To Solve the daily problems with windows and fixing them with little notepad scripts. Unlock the Hidden Secrets of Windows, Learn to crack Software via CMD & Make your own Programming tools. Understand to Manipulate your PC via CMD.

TheBATeam Forum

Use a declared vari...
 
Notifications
Clear all

[Solved] Use a declared variable as a token in For loop  


Mr. Km
Posts: 2
Moderator
(@kabue-murage)
Moderator
Joined: 1 year ago

Hello all.. I was wondering if it's possible to use a declared variable as a token in a for /f loop ? For instance I have something like:

For /f "tokens=%Variable% delims=;" %%b in ...blah blah do ..

Have tried it even with enabled variable extensions with no luck!
The interpreter seems to take '%Variable%' as the actual token.. Is there a way to work around this so that the Token is interpreted as the declared valid integer value from  the variable?

4 Replies
TheBATeam
Posts: 12
Admin
(@thebateam)
Member
Joined: 5 months ago

Basically, it is possible to make delims as a variable... Try the following Code:

@Echo off
cls

Set "_set=1,2.3,4"
Set "_delims=,"
echo %_delims%

For /f "tokens=1,2,3 delims=%_delims%" %%A in ("%_set%") do (Echo %%A - %%B - %%C)

pause

OUTPUT:

Delims-as-variable

 

And, making tokens as a variable is also seems to be possible to accomplish... Try the following Code:

@Echo off
cls

Set "_set=1,2.3,4"
Set "_tokens=1,2,3"
echo %_tokens%

For /f "tokens=%_tokens% delims=," %%A in ("%_set%") do (Echo %%A - %%B - %%C)

pause

OUTPUT:

Tokens-as-variable

 

Please also See the attached files - They might help you!

Reply
sintrode
Posts: 7
(@sintrode)
Active Member
Joined: 2 months ago

It's worth mentioning that variables that get used for for /f loops get parsed before regular variable expansion, so if you're using delayed expansion variables, this won't work.

 

https://www.dostips.com/forum/viewtopic.php?t=6485

Reply
2 Replies
Kvc
 Kvc
Admin
(@thekvc)
Joined: 1 year ago

Admin
Posts: 22

@sintrode Yes, that is why The DEC (delayed expansion condition) was not used here in the above code.

Reply
sintrode
(@sintrode)
Joined: 2 months ago

Active Member
Posts: 7

@thekvc - True, but usually when you enable delayed expansion you'd use it everywhere since it's generally better to use it than not to. I'm just throwing out a warning for the people who have it enabled because it will trip them up otherwise.

Reply
Share:

© 2020 TheBATeam

Our Core Values   /   Terms & Conditions   /   Privacy Policy   /   Why Register?

Theme by Anders Norén