批处理脚本/ findstr函数仅回显分隔符

时间:2017-05-03 06:01:54

标签: batch-file batch-processing findstr

我想只获取包含" .PDF"

的文本文件中的文件名

预期输出:

PP_12345.PDF 03/05/2017 - 13:56:32.18 
PP_23456.PDF 03/05/2017 - 13:56:32.18 
PE_12345.PDF 03/05/2017 - 13:56:32.30 
PE_23456.PDF 03/05/2017 - 13:56:32.30 

test.txt内的信息

put PP_12345.pdf some text here
DOC ID 11223344555
put PP_23456.pdf some text here
oopps this is a sample
ls PP*.pdf
put PE_12345.pdf some text here
this is a sample sentence
ls PE*.pdf
put PE_23456.pdf some text here

这是我的代码:

set Logtext="PP"
set Logtext1="PE"
set findfile="test.txt"

FOR  /f "TOKENS=1 DELIMS=." %%A IN ('findstr %Logtext% %findfile%') DO echo  %%A %DATE% - %TIME% >> log.txt
FOR  /f "TOKENS=1 DELIMS=." %%A IN ('findstr %Logtext1% %findfile%') DO echo  %%A %DATE% - %TIME% >> log.txt

以下是log.txt的结果:

 put PP_12345 03/05/2017 - 13:56:32.18 
 put PP_23456 03/05/2017 - 13:56:32.18 
 ls PP* 03/05/2017 - 13:56:32.18 
 put PE_12345 03/05/2017 - 13:56:32.30 
 ls PE* 03/05/2017 - 13:56:32.30 
 put PE_23456 03/05/2017 - 13:56:32.30 

1 个答案:

答案 0 :(得分:1)

@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q43752174.txt"
set "Logtext=PP"
set "Logtext1=PE"
REM set "findfile=test.txt"

FOR /f "tokens=1-3delims=. " %%a IN ('FINDSTR /L /i /c:"%logtext%_" /c:"%logtext1%_" "%filename1%"^|find /v "*"') DO IF /i "%%c"=="pdf" (
  ECHO %%b.%%c %DATE% %time%  
)

GOTO :EOF

您需要更改sourcedir的设置以适合您的具体情况。

我使用了一个名为q43752174.txt的文件,其中包含我的测试数据。

这假定您搜索的文件名都以 word Space 开头,并且没有包含" P?_"除了作为起始人物之外的其他地方

编辑:添加额外的"找到"过滤以排除星号。