Solved How do I pluck specific characters using tokens/delimeters?

August 7, 2018 at 13:57:45
Specs: Windows 10
I read and understood Matt123's post on using tokens and delimiters--which was excellent--to grab specific strings from a file, but how would one go about extracting the fifth character from the phrase "Hello, world! How are you today?" (the letter "o", in "Hello", in this case) from MyFile.whatever? How about extracting only the "o's"? Thanks in advance for any help you guys can lend.

message edited by scardog.disruptor


See More: How do I pluck specific characters using tokens/delimeters?

Reply ↓  Report •

#1
August 7, 2018 at 14:09:28
✔ Best Answer
I apologize for explaining the below quite poorly...

if you can get hello in a variable, then:

set var=hello
echo %var:~-1%
o

explanation:

you can echo certain parts of a variable, with the :~ at the end, now the syntax is a bit weird to understand, but for me examples help a lot.

set var=testing123
echo %var:~2,2%
st
(start at char 2 & print the next 2 chars)

set var=testing123
echo %var:~0,5%
testi
(start at char 0 & print the next 5 (%var:~5% would be the same))

but it also works backwards, tho i don't know off the top of my head.

all i know is that this for example gives the last 5 chars or the variable:
set var=testing123
echo %var:~-5%
ng123

i5-6600K[delid]@4.8GHz/4.3GHz@1.4v LLC=6 | 2x4GB Crucial-DDR4-2133CL15@14-14-14-30 1T 2700MHz@1.37v
MSI Armor RX 570 4GB@1260Mhz core@1.000v/1900MHz BiosModded

message edited by hidde663


Reply ↓  Report •

#2
August 7, 2018 at 15:10:14
I'm not very good at Batch files, but found this site and bookmarked it for just that reason.
It may be of help to you:

https://www.dostips.com/DtTipsStrin...

MIKE

http://www.skeptic.com/


Reply ↓  Report •

#3
August 8, 2018 at 01:12:14
Could you explain what you are actually trying to do?

Random example:

Extract "o" from "hello" from "MyFile.whatever" (containing these 2 strings):
testing
Say hello to my little friend.
test1 damn
test2 woah

test.bat:
@echo off&setlocal enabledelayedexpansion
for /f "skip=1 tokens=2 delims= " %%a in (MyFile.whatever) do set "variable=%%a"&echo !variable:~-1!&goto exitlabel
:exit-label
exit/b

the .bat replies with "o" without quotes

i5-6600K[delid]@4.8GHz/4.3GHz@1.4v LLC=6 | 2x4GB Crucial-DDR4-2133CL15@14-14-14-30 1T 2700MHz@1.35v
MSI Armor RX 570 4GB@1260Mhz core@1.000v/1865MHz BiosModded

message edited by hidde663


Reply ↓  Report •

Related Solutions

#4
August 8, 2018 at 02:37:50
Thanks! That's exactly what I was looking for-zAnd your explanation was just fine! Kudos!

Reply ↓  Report •

#5
August 8, 2018 at 02:42:48
Thanks! I really appreciate the link. While I have written a variety of batch scripts to help me with my day to day work, I'm still eager to learn how to use batch to its fullest potential, and some of the finer nuances of batch scripting I just can't seem to wrap my head around. Again, I thank you!

Reply ↓  Report •

#6
August 8, 2018 at 03:22:57
This site has a ton of pre scripted tools made in batch:

http://www.robvanderwoude.com/batch...

and you can ofcourse ask us here on computing.net if you need some help :)

i5-6600K[delid]@4.8GHz/4.3GHz@1.4v LLC=6 | 2x4GB Crucial-DDR4-2133CL15@14-14-14-30 1T 2700MHz@1.35v
MSI Armor RX 570 4GB@1260Mhz core@1.000v/1865MHz BiosModded


Reply ↓  Report •

Ask Question