Just trying to search a particular text inside files belonging to a directory(and all its subdirectories).
1) findstr /f:"C:\\test.txt" "TextForSearch" > "out.txt"
gives me the correct answer. test.txt here contains all the files that are to be searched.It is being re-directed to out.txt.
Now, out.txt contains all the matching instances but are not delimited using spaces. How to get a list of all filename(only filenames) with a space or delimiter in the file out.txt?
2) findstr /D:"C:\\" "TextForSearch"
does not search at all. It simply hangs? Where am I going wrong here?
findstr /S /D:"C:\\" "TextForSearch" *
/S recersion Searches for matching files in the current directory and all subdirectories.
* search for all files
For others,
Thanks Ted and Harry.
The solutions are
1) 1) findstr /m /f:"C:\\test.txt" "TextForSearch" > "out.txt"
for searching for a particular strings in files specified in test.txt and redirecting it to a text file named out.txt with matching files in separate lines (Demilited. Thank god.)
findstr /S /D:"C:\\" "TextForSearch" *
checkout Ted's answer!
Related
From Windows CMD I can use
findstr -m subroutine *.f90
to list all files with suffix .f90 containing "subroutine". To list all .f90 files not containing the string I can do something like
dir /b *.f90 > files.txt
findstr -m subroutine *.f90 > files_with_string.txt
and then write a script to list the lines in files.txt not found in files_with_string.txt. Is there a more elegant way?
There is a /v option in findstr, but that wouldn't help here.
Process each file with a for loop, try to find the string and if it doesn't find it (||), echo the filename:
for %a in (*.f90) do #findstr "subroutine" "%a" >nul || echo %a
(above is command line syntax. For use in a batchfile, use %%a instead of %a (all three occurences))
I needed to search for filenames which contained one specific string ("up"), but did not contain another string ("packages").
However, I was hoping to run it from the command line.
This is actually possible to do, you just have to call findstr twice.
Mine looked like:
dir /B /S up | findstr /I "up" | findstr /I /v "packages"
That means:
search all directories (/S & subdirs)
give me the bare formatting (/B)
and pass it through (| pipe) findstr (/I ignore case) to find ones that have "up" then
pass the results (| pipe) through findstr again but this time ignore all that
contain (/v) "packages"
If you have items like:
c:\test\packages\up
c:\extra\thing\up
c:\extra\thing\packages\up
c:\extra\test\up
c:\extra\test\nothing
The results would be only the ones that contain "up" but do not contain "packages"
c:\extra\thing\up
c:\extra\test\up
Call findstr /v multiple times on result
In other words you can keep passing the result into another findstr with /v to remove the ones that have additional words you don't want.
When I use findstr on a single file, like this:
findstr "import" filename.py
it returns a number of matches. But when I extend it to cover the current folder and sub-folders:
findstr /S "import" .
it doesn't find any matches. What am I doing wrong?
OK i've figured it out. The /S option indicates searching current directory and all sub-directories, but I still need to specify a filename pattern.
This indicates all files:
findstr /S "import" *
I want to copy names of files to text in a directory based on their file extension.
As of now I am using dir /b >i67.txt which works fine for me but its not resolving problem of specific file extensions.
Can someone help me in getting a batch script for the same.
You are looking for the following command, run it in the context of the directory which contains your files:
dir /b /s /-p *.txt /o:n | findstr /E .txt > i67.txt
Using the above code example, you will be able to find all *.txt files in the directory and output the results into the i67.txt file (will be outputted to the same directory).
You can specify multiple file masks within one DIR /B command. Based on your comment to Yair Nevet's answer, it seems you want the following extensions: .ovr, .inc, and .dat. That can be done simply using:
dir /b /s *.ovr *.inc *.dat >i67.txt
If the files are on an NTFS volume that has short 8.3 names enabled, then you might get additional undesired file extensions if you have any file extensions longer than 4 characters that begin with your wanted extension. For example someName.data would show up in your output because it most likely would have a short name of SOMENA~1.DAT that matches your file mask.
You can prevent short name inclusion by piping the output to FINDSTR. The /L option forces a literal search as opposed to regular expressions, the /I option ignores case, and the /E option matches only the end of each line. Multiple search terms are delimited by spaces.
dir /b /s *.ovr *.inc *.dat | findstr /lie ".ovr .inc .dat"
Regarding your following comment:
Here is what I am using now: dir /b | findstr [a-z].*ovr>i67.txt &&
dir /b | findstr [a-z].*inc>>i67.txt && dir /b | findstr
[a-z].*dat>>i67.txt What it does?? --- It copies all
names(remember,only name except files itself which are ending with
extension .ovr .dat and .cpi ) present in a directory and copy it to a
text file(here name is i67.txt)
That will not actually do what you want for several reasons.
Windows file names are not case sensitive. Windows would treat NAME.OVR and name.ovr the same, so you should as well. That requires the /I option.
There is nothing in your search to anchor ovr to the extension. It will look for your pattern anywhere within the file name. And the dot is a meta character that represents any character - not a literal dot. The asterisk allows the dot to match any number of characters.
I can't be sure, but it looks like perhaps you only want to match files that begin with a letter. The following modification to my answer should do the trick:
dir /b /s *.ovr *.inc *.dat | findstr /ri "^[a-z].*\.ovr$ ^[a-z].*\.inc$ ^[a-z].*\.dat$"
The \R option forces a regular expression match instead of a literal. It is the default behavior for the given search, but it is a good idea to be explicit with regard to regex vs literal search.
^ anchors the search to the beginning of the name
[a-z] matches any letter (sort of). Remember it is not case sensitive because of the /I option. Without the /I option, it would not match upper case Z. See Why does findstr not handle case properly (in some circumstances)? for an explanation.
.* matches any number of characters, without restriction
\. matches a dot literal, marking the beginning of your extension
Then comes your extension
$ anchors the match to the end of the name
Looking for the command line (or batch) that will do the following,
I have 3 levels of directories and need to search for a specific string contained in text files, searching all files, 1 level subdirectory down.
So I know this will recursively search for the string
findstr /S "STRING" *
But I don't want the search to go father than only 1 subdirectory level down.
Seems like a simple thing but I could not find anything solid. The closest thing I could find was something like this thread..
windows batch to recursively scan files in 1 level subfolders
However, before going batch is there a simpler (command line) way to this rather, apparently simple task?
Thanks for your time!
This should do it from the command line and write file.log into the current folder
for /d %a in (*) do findstr "STRING" "%a\*">>file.log
Child Directory Search Only (No Parents or Grandchildren)
Here is a one line command to enumerate the current working directory children directories dir /ad/b and search each directory for files containing the search string. Results are saved out to a text file.
for /f "delims=" %A in ('dir /ad/b') do #findstr "STRING" "%~A\*" >> results.txt
Note: That as currently configured, a result line will be output for each matching line in a file. Meaning that multiple find results may be output for a single file.
Example of results.txt
ChildFolderA\File1.txt:line with STRING
ChildFolderA\File1.txt:another line with STRING in file1
ChildFolderA\File2.txt:another file and line with STRING
ChildFolderB\File5.txt:another folder with a file containing the search STRING
#echo off
setlocal enableextensions
(#for /d %%a in (*) do #for %%b in ("%%~fa\*") do #echo(%%~fb)|findstr /f:/ /c:"string" > file.txt
This will enumerate all the files one level down and send this list of files to a findstr command. findstr will search the string into the indicated list and send output to final file
To use from command line, replace the double percent signs %% with single ones %
I'd like to recursively search a directory and find files, which have NOT a certain extension, or precisely, which have NOT a certain set of extensions.
Sketch: find in "dir" all files without "ext1", "ext2", "ext3" and print results to .txt
I tried around several hours with DIR and ATTRIB, but unfortunately without bigger success.
Your consideration is highly regarded! Thanks.
Try this:
dir /b /s /a-d | findstr /vi ".ext1$ .ext2$ .ext3$"
The /a-d switch excludes directories, giving you only files. The findstr parameter lets you search the files for strings, and the /vi switch indicates to exclude files containing the next parameter, the search being case insensitive.
As Joey pointed out, the $ is necessary to indicate end of the line.