Search code examples
windowsbatch-filefindstr

Find filename with FINDSTR that start with 'abc...' and not '-abc...'


I have a folder with subfolders, which include files.

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.

REM Adjust location of list
SET list=C:\WorkProjects\Experiment\list.txt

REM Source dir
SET source=Z:\

REM Target dir
SET destination=C:\WorkProjects\Experiment

SET logfile=Outcome2.log

cd /d "%source%">>"%destination%\%directoryfile%"
dir * /s/b | FINDSTR /I /R /C:".*.pdf">"%destination%\%directoryfile%"

ECHO 2>"%destination%\temp.txt"

FOR /F "tokens=1,* delims=|" %%A IN (%list%) DO (
    ECHO %%A>>"%destination%\temp.txt"
    For /F "Delims=" %%X In ('FINDSTR /I /R /C:"\<%%A\..*\>" "%destination%\%logfile%"') DO (
    ECHO %%X>>"%destination%\temp.txt"
    )
)

%list% contains a list of strings as depicted below depicted below:

12345
abcdf
aadfe

Here is the output of the example:

12345 <-- This is the string that I am looking for

Z:\Master List\1\12\123\1234\12345.pdf <-- Output

Z:\Master List\A\AD\AD-1\AD-12\AD-12345.pdf <-- Output

So, the first path is what I want it to return and the second path isn't because it has "AD-" in the front of it.

How would I be able to achieve this?


Solution

  • change 'FINDSTR /I /R /C:"\<%%A\..*\>" to 'FINDSTR /I /R /C:"\\%%A\..*\>" did the trick