Better way to extract data

  • Thread starter Thread starter Matt Williamson
  • Start date Start date
M

Matt Williamson

Given the following script

setlocal enabledelayedexpansion
if exist file.txt (
for /f "tokens=*" %%a in ('type file.txt^|find "BOF"') do (set dt=%%a)
set dt=!dt: = !&set dt=!dt: = !&set dt=!dt: = !
for /f "tokens=7" %%a in ('echo !dt!') do set dt=%%a
echo !dt!
)

Where after the 3rd line the dt variable contains:

BOF PING GLOBAL ACTIVITY DATA OF 12/13/2006 TO REMOTE 123 BEGINS
HERE 12/13/2006 23:13:38

Is there a better way to extract the Date after DATA OF? Right now, I'm
replacing 2 spaces with 1 space multiple times so I can use the second for
loop to reliably extract the date. I'm just wondering what other ways there
are to do it.

TIA

Matt
 
Matt said:
Given the following script

setlocal enabledelayedexpansion
if exist file.txt (
for /f "tokens=*" %%a in ('type file.txt^|find "BOF"') do (set dt=%%a)
set dt=!dt: = !&set dt=!dt: = !&set dt=!dt: = !
for /f "tokens=7" %%a in ('echo !dt!') do set dt=%%a
echo !dt!
)

Where after the 3rd line the dt variable contains:

BOF PING GLOBAL ACTIVITY DATA OF 12/13/2006 TO REMOTE 123 BEGINS
HERE 12/13/2006 23:13:38

Is there a better way to extract the Date after DATA OF? Right now, I'm
replacing 2 spaces with 1 space multiple times so I can use the second for
loop to reliably extract the date. I'm just wondering what other ways there
are to do it.

TIA

Matt

This works for me:

- - - - - - - begin screen capture WinXP MCE 2005 SP2 - - - - - - -
c:\cmd>type c:\temp\file.txt
sadsdlkj weroiuaglxv sdfkljsdf zlkdflk ertiou asldkfjf rtiou vbnx,nv
BOF PING GLOBAL ACTIVITY DATA OF 12/13/2006 TO REMOTE 123 BEGINS HERE 12/13/2006 23:13:38
xvcm,n weroiu qwe fgklj qweopir topi zxlkdj cvb.,m
c:\cmd>demo\GetBOFdt
[12/13/2006]

c:\cmd>rlist demo\GetBOFdt.cmd
=====begin c:\cmd\demo\GetBOFdt.cmd ====================
1. @echo off
2. for /f "tokens=7" %%a in (
3. 'findstr "BOF" c:\temp\file.txt'
4. ) do set dt=%%a
5. echo [%dt%]
=====end c:\cmd\demo\GetBOFdt.cmd ====================
- - - - - - - end screen capture WinXP MCE 2005 SP2 - - - - - - -
 
Matt Williamson said:
Given the following script

setlocal enabledelayedexpansion
if exist file.txt (
for /f "tokens=*" %%a in ('type file.txt^|find "BOF"') do (set dt=%%a)
set dt=!dt: = !&set dt=!dt: = !&set dt=!dt: = !
for /f "tokens=7" %%a in ('echo !dt!') do set dt=%%a
echo !dt!
)

Where after the 3rd line the dt variable contains:

BOF PING GLOBAL ACTIVITY DATA OF 12/13/2006 TO REMOTE 123 BEGINS
HERE 12/13/2006 23:13:38

Is there a better way to extract the Date after DATA OF? Right now, I'm
replacing 2 spaces with 1 space multiple times so I can use the second for
loop to reliably extract the date. I'm just wondering what other ways
there are to do it.

Matt,

Since several delimiters in a row are threated as a single one, you don't
need any conversion since following should be enough

for /f "tokens=7" %%i in (
'findstr "BOF" file.txt') do (
echo %%i
)

And talking about other ways to do it, you could use slash as delimiter
which can be more appropriate in cases you don't have better clue about
substring position in the string:

@echo off
setlocal enabledelayedexpansion
for /f "tokens=1,2,3 delims=/" %%i in (data.txt) do (
set out=%%i
set out=!out:~-2!/%%j/%%k
set out=!out:~0,10!
echo !out!
)
 
Back
Top