DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Getting Started With Windows Containers
  • Docker Basics, Commands, and Usage
  • Integrating Redis With Message Brokers
  • Terraform State File: Key Challenges and Solutions

Trending

  • Immutable Secrets Management: A Zero-Trust Approach to Sensitive Data in Containers
  • Scalable System Design: Core Concepts for Building Reliable Software
  • Google Cloud Document AI Basics
  • Emerging Data Architectures: The Future of Data Management
  1. DZone
  2. Coding
  3. Languages
  4. Search from the Windows Command Prompt

Search from the Windows Command Prompt

When you need to search in text files from batch files, you can use either the FIND or FINDSTR commands. You can also automate certain tasks based on the search results.

By 
Peter Kankowski user avatar
Peter Kankowski
·
Jul. 05, 23 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
2.4K Views

Join the DZone community and get the full member experience.

Join For Free

The FIND Command

To search for text in multiple files from the Windows command prompt or batch files, you can use the FIND command, which has been present since the days of MS DOS and is still available in Windows 11. It's similar to the Unix grep command, but does not support regular expressions. If you want to search for the word borogoves in the current directory, please follow this syntax:

find "borogoves" *

Note that the double quotes around the pattern are mandatory. If you are using PowerShell, you will need to include single quotes as well:

find '"borogoves"' *

Instead of the asterisk (*), you can specify a file mask such as *.htm?. The find command displays the names of the files it scans, even if it doesn't find any matches within these files:

The FIND command in Windows 11

The search is case-sensitive by default, so you typically need to add the /I switch to treat uppercase and lowercase letters as equivalent:

find /I "<a href=" *.htm

If you don't specify the file to search in, find will wait for the text input from stdin, so that you can pipe output from another command. For example, you can list all copy commands supported in Windows:

help | find /i "copy"

Another switch, /V, allows you to find all lines not containing the pattern, similar to the grep -v command.

In batch files, you can use the fact that the find command sets the exit code (errorlevel) to 1 if the pattern is not found. For instance, you can check if the machine is running a 64-bit or 32-bit version of Windows:

@echo off 
rem Based on KB556009 with some corrections reg Query "HKLM\Hardware\Description\System\CentralProcessor\0" /v "Identifier" | find /i "x86 Family" > nul if errorlevel 1 goto win64 
echo 32-bit Windows goto :eof 
:win64 rem Could be AMD64 or ARM64 echo 64-bit Windows

The FINDSTR Command: Regular Expression Search

If you need to find a regular expression, try the FINDSTR command, which was introduced in Windows XP. For historical reasons, findstr supports a limited subset of regular expressions, so you can only use these regex features:

  • The dot . matches any character except for newline and extended ASCII characters.
  • Character lists [abc] match any of the specified characters (a, b, or c).
  • Character list ranges [a-z] match any letter from a to z.
  • The asterisk (*) indicates that the previous character cane be repeated zero or more times.
  • The \< and \> symbols mark the beginning and the end of a word.
  • The caret (^) and the dollar sign ($) denote the beginning of and the end of a line.
  • The backslash (\) escapes any metacharacter, allowing you to find literal characters. For example, \$ finds the dollar sign itself.

Findstr does not support character classes (\d), alternation (|), or other repetitions (+ or {5}).

The basic syntax is the same as for the FIND command:

findstr "\<20[0-9][0-9]\>" *.htm

This command finds all years starting with 2000 in the .htm files of the current directory. Just like with find, use the /I switch for a case-insensitive search:

The FINDSTR command in Windows 11

FINDSTR Limitations and Quirks

Character lists [a-z] are always case-insensitive, so echo ABC | findstr "[a-z]" matches.

The space character works as the alternation metacharacter in findstr, so a search query like findstr "new shoes" * will find all lines containing either new or shoes. Unfortunately, there is no way to escape the space and use it as a literal character in a regular expression. For example, you cannot find lines starting with a space.

Syntax errors in regular expression are ignored. For instance, findstr "[" * will match all lines that contain the [ character.

If the file contains Unix line breaks (LF), the $ metacharacter does not work correctly. If the last line of a file lacks a line terminator, findstr will be unable to find it. For example, findstr "</html>$" * won't work if there is no CR+LF after </html>.

Early Windows versions had limitations on line length for find and findstr, as well as other commands. The recent versions lifted these limits, so you don't have to worry about them anymore. See this StackOverflow question for findstr limitations and bugs, especially in early Windows versions.

The findstr command operates in the OEM (MS DOS) code page; the dot metacharacter does not match any of the extended ASCII characters. As the result, the command is not very useful for non-English text. Besides that, you cannot search for Unicode characters (UTF-8 or UTF-16).

Conclusion

You can learn about other switches by typing findstr /? or find /?. For example, the additional switches allow you to search in subdirectories or print line numbers. You can also refer to the official documentation.

In general, the find and findstr commands are outdated and come with various quirks and limitations.

Command (computing) Syntax (programming languages) PowerShell Microsoft Windows Text file

Published at DZone with permission of Peter Kankowski. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Getting Started With Windows Containers
  • Docker Basics, Commands, and Usage
  • Integrating Redis With Message Brokers
  • Terraform State File: Key Challenges and Solutions

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!