批处理脚本/findstr 函数仅回显分隔符的左侧
batch script /findstr function to echo only the left of the delimiter
我只想获取包含“.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
@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
的文件,其中包含您的数据用于我的测试。
这假定您查找的文件名都以 wordSpace 开头并且 none 包含 "P?_" 别处作为起始字符
编辑:添加了额外的 "find" 过滤器以排除星号。
我只想获取包含“.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
@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
的文件,其中包含您的数据用于我的测试。
这假定您查找的文件名都以 wordSpace 开头并且 none 包含 "P?_" 别处作为起始字符
编辑:添加了额外的 "find" 过滤器以排除星号。