Substring

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
santhosh
Posts: 41
Joined: 02 Aug 2010 05:10

Substring

#1 Post by santhosh » 31 May 2011 01:26

Dear Orange/agerman,

i have a doubt is it possible to separate a string in substring in DOS(input file are in .txt format),example
input:-
2323,344429890
41234,454324424252
41241,35433353535
output:-
have to print only first 4 number in second column
3444
4543
3543
Thanks in advance

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Substring

#2 Post by orange_batch » 31 May 2011 01:37

Oh, being singled out? That's a first. 8)

If you want to store these numbers all in variables it would be different, but to do exactly as you said is also very easy.

Without delayed expansion:

Code: Select all

for /f "delims=, tokens=2" %%a in (input.txt) do (
set tempvar=%%a
call echo:%%tempvar:~,4%%
)

With delayed expansion:

Code: Select all

setlocal enabledelayedexpansion
for /f "delims=, tokens=2" %%a in (input.txt) do (
set tempvar=%%a
set tempvar=!tempvar:~,4!
echo:!tempvar!
)

delims=,
2323,344429890

tokens=2
2323,344429890

tempvar:~,4 (same as tempvar:~0,4)
2323,344429890

Post Reply