问题
I have to process a text file which contains a bunch of events that look like
event_name1 : dd.mm.yyyy
event_name2 : dd.mm.yyyy
...
enemt_nameN : dd.mm.yyyy
I don't know how many lines there are before hands
then I have to extract a date from each line and parse it to find a date I am looking for. if the date is the one needed, I need to echo the event name and keep searching
I can parse the file line by line but then a line won't split. Everywhere I am using the same command as you might see below
@echo off
rem parsing today's date and saving variables i will need later
echo %date%
for /f "tokens=1-3 delims=-" %%a in ("%date%") do (
set day = %%a
set month = %%b
set year = %%c
)
rem parsing the file with events
FOR /F "tokens=*" %%i IN (Dates.txt) DO (
rem trying to work with the current line
for /f "tokens=1-3 delims= " %%j in (%%i) do (
set /f name = %%a
set /f inputdate = %%c
rem doing stuff with the date in the line, i need to parse it by day, month and year
for /f "tokens=1-3 delims=." %%k in (%inputdate%) do (
if /I "%day%" EQU %%a echo %inputdate%
)
)
)
I expect the line to be split normally, however, I keep getting an error like "unable to find the file event_name" at each loop where I try to parse a string
Echo output in the cmd looks like this, for example:
(for /F "tokens=1-3 delims= " %j in (tomorrow : 12.06.2019) do (
set /f name = %a
set /f inputdate = %c
for /F "tokens=1-3 delims=." %k in ((null)) do (if /I "" EQU %a echo
)
) )
Unable to find the file tomorrow.
回答1:
when I understand correctly what you try to do, the following one-liner should be enough:
for /f "delims=:" %%a in ('type Dates.txt^|find "%date%"') do @echo %%a
来源:https://stackoverflow.com/questions/56550041/processing-and-splitting-lines-within-batch-files