Windows CMD FINDSTR STRING AND COPY FILE

半城伤御伤魂 提交于 2021-01-29 19:29:55

问题


I have a folder with subfolders include txt and pdf files. There is a Pdf file for each txt file which has nearly same name.

For example; for each ABC_R10.txt --> there is a ABC).pdf file.

In Windows 10, with a batch file,

I want to search specific string in a .txt file with FINDSTR command, and copy files, which contain my string, into current folder. I achieved proper code until this point.

CLS
@ECHO OFF
ECHO FIND BUKUM

findstr /m /s /i /p /c:"BUKUM" *.txt > logfile.xls

for /f "delims=" %%a in ('findstr /m /s /i /p /c:"BUKUM" *.txt') do ^
copy "%%a" "%cd%" 


if errorlevel 1 echo nothing found.
PAUSE
CLS
EXIT

But I want to find file name of exact match but get pdf file with similar name, not txt file.

I have to get ABC of ABC_R10.txt and add ).pdf string and get ABC).pdf

Substring of _R occurs each .txt file.

How can I achieve it?


回答1:


Based solely on your now edited question:

@For /F "Delims=_" %%A In ('FindStr /SPMIC:"BUKUM" *.txt') Do @Copy /Y "%%A).pdf">Nul



回答2:


EDIT: there is nothing to say to @Compo's modification,
just when using the ) unquoted inside a code block it has to be escpaped ^)

@ECHO OFF
CLS
ECHO FIND BUKUM
for /f "delims=_" %%A in (
  'findstr /msip /c:"BUKUM" *.txt'
) do if exist "%%A).pdf" (
  copy "%%A).pdf" "%cd%" 
  Echo copied %%A^).pdf to %cd%
) else echo not found "%%A).pdf"
PAUSE


来源:https://stackoverflow.com/questions/53452100/windows-cmd-findstr-string-and-copy-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!