Windows equivalent of Unix "find" command?

After long time working with Unix systems I had to make some scripts on Windows. For serious scripting Powershell is the tool you should use. You can search Internet with keywords like powershell find string in file, or other combinations and you find a lot of information. That's the problem, a simple oneliner like

get-childitem C:\yourdir -include *.c -recursive |Select-String -pattern sqlcommand

won't help you much. You need to find the PowerShell IDE, learn the different syntax and try to love / accept that new stuff.

Prepare for a study with PowerShell when you want to do these things more often, or try to get a Unix-like environment on your windows (cygwin, or better git for windows)


NEW AND IMPROVED ANSWER

I recently stumbled upon a built-in command that is rather similar to find in Unix:

ForFiles

Basic syntax is:

forfiles [/p <Path>] [/m <SearchMask>] [/s] [/c <Command>] [/d [{+|-}][{<Date>|<Days>}]]

There are several variables to use when constructing the command to execute per each file (via the /c switch):

  • @FILE File name.
  • @FNAME File name without extension.
  • @EXT File name extension.
  • @PATH Full path of the file.
  • @RELPATH Relative path of the file.
  • @ISDIR Evaluates to TRUE if a file type is a directory. Otherwise, this variable evaluates to FALSE.
  • @FSIZE File size, in bytes.
  • @FDATE Last modified date stamp on the file.
  • @FTIME Last modified time stamp on the file.

It looks like you would use the command like this:

FORFILES /m *.cs /c FINDSTR /I /N /C:"sqlcommand" @FILE

I'm not sure how long this command has been around, but the earliest reference I could find in the documentation is from 2008-09-02:

https://web.archive.org/web/20080902221744/http://technet.microsoft.com:80/en-us/library/cc753551.aspx

and that page states that it was last updated on "April 25, 2007". The documentation is filed under "Windows Server" so it likely started there and was added to the desktop OSes starting with Windows Vista, I believe. I did check Windows XP and didn't see it there, though it is on Windows 10.


ORIGINAL ANSWER

This requires a combination of two DOS commands:

  • FOR /F ["options"] %variable IN ('command') DO command [command-parameters]

    and

  • DIR /B /O:N /W *.c (this is the 'command' noted in the FOR command above)

Create a CMD script as follows:

@ECHO OFF

FOR /F %%B IN ('DIR /B /O:N /W *.cs') DO (
    findstr /I /N /C:"sqlcommand" %%B
)

OR, just use the find command found in this set of Unix command ports:

http://unxutils.sourceforge.net/

or

http://sourceforge.net/projects/unxutils/

(both links should be the same project)

Tags:

Windows

Cmd

Find