1

I have 2 text files.
One contains a filename with path before it.
And the other just the filename.
I want to remove the lines with pathnames if the filename match.
So you can say i only want the files that doesn't match in a new created text file 3

so list1.txt has:

C:\Program Files\Folder1\enter
C:\Program Files\Folder1\numbers.txt
C:\Program Files\Folder1\files.jpg
C:\Program Files\Folder1\movies.jpg

And list2.txt has:

enter
numbers.txt
files.jpg

Outcome has to be in list3.txt

C:\Program Files\Folder1\movies.jpg
  • Is there a particular reason you are doing this with batch scripting? Things like this are much easier to do with Powershell and VBScript... – Tony Hinkle Apr 29 '15 at 17:26
  • 1
    I know its easier write a VBScript but if this is possible i would like it in a batch file. Otherwise if it's not possible than i have no choice – Woordvoerder Apr 29 '15 at 17:38

2 Answers2

1

You shoud read up on FINDSTR (findstr /? from the command line). Also, read What are the undocumented features and limitations of the Windows FINDSTR command?.

Here is a simple batch script that does the job.

@echo off
>"list2.txt.mod" (for /f "usebackq delims=|" %%F in ("list2.txt") do echo \%%F)
findstr /liveg:"list2.txt.mod" "list1.txt" >"list3.txt"
del "list2.txt.mod"

If you change list2.txt to look like:

\enter
\numbers.txt
\files.jpg

then all you need is the following from the command line (no batch needed)

findstr /liveg:"list2.txt" "list1.txt" >"list3.txt"    
Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
0

not tested:

@echo off

set "file_n=fileWithNames.txt"
set "file_l=fileWithTheList.txt"

setlocal enableDelayedExpansion
set "fndstrline="
for /f "usebackq tokens=* delims=" %%a in ("%file_n%") do (

   set "fndstrline=%%a !fndstrline!"

)

type "%file_l%"|findstr /v "%fndstrline%" 2>nul 1>resulrFile.txt

Should work if the file names does not contains some special symbols ...Can be made more robust if needed.

npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • There are lots of ways this can fail besides poison chars. For example, space within file name, and file name embedded anywhere within the entire path. Also, you should explicitly specify literal search, and case insensitive search. – dbenham Apr 29 '15 at 19:59