BATCH - String manipulation
I need help in splitting text in a document call date_baseline.txt Th开发者_Python百科e 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!
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.
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.
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
精彩评论