问题描述
我必须处理一个包含一系列事件的文本文件
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
我不知道手前有多少行
然后,我必须从每一行中提取一个日期,然后解析它以找到我要查找的日期.如果日期是必需的日期,我需要回显事件名称并继续搜索
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%
)
)
)
我希望行能正常分割,但是,在尝试解析字符串的每个循环中,我都会不断收到诸如无法找到文件event_name"之类的错误消息
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
cmd中的echo输出如下所示:
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.
推荐答案
当我正确理解您要尝试执行的操作时,以下一线应该足够了:
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
这篇关于在批处理文件中处理和拆分行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!