misctp asdas
misctp asdas

Reputation: 1023

BATCH - String manipulation

I need help in splitting text in a document call date_baseline.txt The contents of this file is :

1st Day = 2011-08-26
2nd Day = 2011-07-30
3rd Day = 2011-07-29

I need to take out each of the date's shown above. Any pros with batch knowledge?

thanks in advance!

Upvotes: 0

Views: 1096

Answers (3)

adarshr
adarshr

Reputation: 62583

Here you go!

for /f "tokens=3 delims== " %i in (date_baseline.txt) do @echo %i

If you want to put that into a batch file,

@echo off

for /f "tokens=3 delims== " %%i in (date_baseline.txt) do (
    echo %%i
)

Note that just extracting the last fragment, 3 is sufficient.

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 881363

If by "take out", you mean "extract", the following would be a good start:

@setlocal enableextensions enabledelayedexpansion
@echo off
for /f "usebackq tokens=4" %%a in (input.txt) do (
    call :process %%a
)
endlocal
goto :eof

:process
    set myvar=%1
    echo !myvar!
    goto :eof

This outputs:

2011-08-26
2011-07-30
2011-07-29

The process function can be modified to do whatever you wish. At the moment, it simply save it in a variable and then prints that but you can do arbitrarily complex processing on it.

Upvotes: 1

ghostdog74
ghostdog74

Reputation: 342353

You can use vbscript ,

Set objFS=CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file"
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfLine
    strLine= objFile.ReadLine
    s = Split(strLine,"=")
    WScript.Echo s(1) 'display the date column
Loop
objFile.Close

Upvotes: 0

Related Questions