Finding a substring in a file name

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
bluefire_xl
Posts: 5
Joined: 28 Jan 2016 09:06

Finding a substring in a file name

#1 Post by bluefire_xl » 20 Oct 2016 13:07

I have a series of file in a folder and I want to check if a specific string is in one of the file name. I was looking at findstr command but I think it only looks at the content of the file.

Its like find "hold" in the file name holdings092516.txt should return true. I am a bit new to dos commands so I hope someone can help me.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Finding a substring in a file name

#2 Post by Compo » 20 Oct 2016 13:27

Either:

Code: Select all

dir/b mydir\*.txt|find/i "hold"

or:

Code: Select all

dir/b mydir\*.txt|findstr/i "hold"
should work fine.

bluefire_xl
Posts: 5
Joined: 28 Jan 2016 09:06

Re: Finding a substring in a file name

#3 Post by bluefire_xl » 25 Oct 2016 15:48

Thank you for the response.

I notice that it was returning the filename back if part of the string is found. Is their a way to determine the output by yes or no?

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Finding a substring in a file name

#4 Post by Compo » 25 Oct 2016 17:22

You could always use the where command:

Code: Select all

Where /Q "%UserProfile%\Documents" *hold* >Nul 2>&1
If %ErrorLevel%==1 (Echo=No) Else (Echo=Yes)
Or even search directly for the string in your dir command:

Code: Select all

Dir/A-D "%UserProfile%\Documents\*hold*" >Nul 2>&1&&(Echo=Yes)||(Echo=No)

Post Reply