search for list of filenames in a single file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ladduq
Posts: 31
Joined: 22 Jan 2012 01:08

search for list of filenames in a single file

#1 Post by ladduq » 22 Mar 2012 06:10

Hi

Need help to search for list of filenames in a single file.

Lets say the file information.txt contains lakhs of filenames but i only need to get a list of hundred filenames with all the instances in that file.

lets say information.txt contains

1 1.txt
2 2.txt
3 3.txt
4 2.txt
5 4.txt
6 5.txt
7 2.txt
8 6.txt
9 2.txt
10 1.txt

if the search criteris is "1.txt" and "2.txt"
the output should be

start
=================
1 1.txt
2 2.txt
4 2.txt
7 2.txt
9 2.txt
10 1.txt
=====================
end

the same should be applicable for any no of files with any no of instances as in this case we have only 2 files as our search criteria.

Regards
Ladduq

tonysathre
Posts: 14
Joined: 20 Mar 2012 10:07

Re: search for list of filenames in a single file

#2 Post by tonysathre » 22 Mar 2012 06:13

C:\temp>findstr "1.txt 2.txt" information.txt
1 1.txt
2 2.txt
4 2.txt
7 2.txt
9 2.txt
10 1.txt
C:\temp>

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: search for list of filenames in a single file

#3 Post by dbenham » 22 Mar 2012 15:24

@tonysathre

That search will be treated as a regex, and the dot will match any character. Not what is wanted.

You need to add the /L (literal) option to the findstr command.

Also, you can't search for file names that contain spaces that way. For spaces you need the /c:"search" option.


Dave Benham

tonysathre
Posts: 14
Joined: 20 Mar 2012 10:07

Re: search for list of filenames in a single file

#4 Post by tonysathre » 23 Mar 2012 10:33

I thought that regex was only used when you specify /R. Is that not the case?

Tony

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: search for list of filenames in a single file

#5 Post by foxidrive » 23 Mar 2012 10:35

No, it defaults to regex

Good one Microsoft. Nice of them to include a /R switch. :)

miskox
Posts: 554
Joined: 28 Jun 2010 03:46

Re: search for list of filenames in a single file

#6 Post by miskox » 23 Mar 2012 11:47

tonysathre wrote:C:\temp>findstr "1.txt 2.txt" information.txt
1 1.txt
2 2.txt
4 2.txt
7 2.txt
9 2.txt
10 1.txt
C:\temp>


You should search for " 1.txt" so 11.txt or 101.txt are excluded. (of course with /C as others already mentioned).

Saso

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: search for list of filenames in a single file

#7 Post by !k » 23 Mar 2012 13:14

it defaults to regex

Hmm... Image

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: search for list of filenames in a single file

#8 Post by foxidrive » 23 Mar 2012 23:12

That's normal. What are you trying to say?

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: search for list of filenames in a single file

#9 Post by !k » 24 Mar 2012 01:05

it's no regexps

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: search for list of filenames in a single file

#10 Post by foxidrive » 24 Mar 2012 02:34

Code: Select all

@echo off
type nul> "Crystal Reports Shifts01.pdf"
type nul> "Crystal Reports Shifts02.pdf"
type nul> "Crystal Reports Shifts03.pdf"
type nul> "Crystal Reports Shifts04.pdf"

dir /b |findstr ..pdf

del "Crystal Reports Shifts0?.pdf"

pause

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: search for list of filenames in a single file

#11 Post by foxidrive » 24 Mar 2012 02:45

With the literal string /c: switch it switches to literal mode.

Post Reply