How to do a simple file search in cmd - windows

I want to quickly search for a file given its name or part of its name, from the windows command line (not power shell). This is similar to opening explorer and using the search box at the top.
Note: dir can search based on a string template but it will not search in the subdirectories.
Note2: findstr can be used to search for a token inside files and has a recursivity flag; it's funny that a more complex find can be easily discovered ...

dir /s *foo* searches in current folder and sub folders.
It finds directories as well as files.
where /s means(documentation):
/s Lists every occurrence of the specified file name within the
specified directory and all subdirectories.

dir /b/s *.txt
searches for all txt file in the directory tree. Before using it just change the directory to root using
cd/
you can also export the list to a text file using
dir /b/s *.exe >> filelist.txt
and search within using
type filelist.txt | find /n "filename"
EDIT 1:
Although this dir command works since the old dos days but Win7 added something new called Where
where /r c:\Windows *.exe *.dll
will search for exe & dll in the drive c:\Windows as suggested by #SPottuit you can also copy the output to the clipboard with
where /r c:\Windows *.exe |clip
just wait for the prompt to return and don't copy anything until then.
EDIT 2:
If you are searching recursively and the output is big you can always use more to enable paging, it will show -- More -- at the bottom and will scroll to the next page once you press SPACE or moves line by line on pressing ENTER
where /r c:\Windows *.exe |more
For more help try
where/?

dir *.txt /s /p
will give more detailed information.

Problem with DIR is that it will return wrong answers.
If you are looking for DOC in a folder by using DIR *.DOC it will also give you the DOCX. Searching for *.HTM will also give the HTML and so on...

You can search in windows by DOS and explorer GUI.
DOS:
1) DIR
2) ICACLS (searches for files and folders to set ACL on them)
3) cacls ..................................................
2) example
icacls c:*ntoskrnl*.* /grant system:(f) /c /t ,then use PMON from sysinternals
to monitor what folders are denied accesss.
The result contains
access path contains your drive
process name is explorer.exe
those were filters youu must apply

Related

Fastest way to search for a text string in all files & directories (recursively), then output the directory and filename

In my current script, i am using findstr (Windows) as follows:
findstr /s "string" C:\*.*
but this is extremely slow.
What is the fastest way to do this in Windows without using any additional software (e.g. python, c#, etc...).
Also, the files in the directories are constantly changing, so i'm unable to index the files and perform a search on the index.
The results need the full path and filename with the string match.
The full lines where the string matches need to be returned.
Only text based files need to be searched (e.g. xml, txt, etc...)
A possible batch file for this task would be:
#echo off
cd /D C:\
del "%USERPROFILE%\SearchResults.txt" 2>nul
%SystemRoot%\system32\findstr.exe /I /S /C:"string" *.htm *.html *.txt *.xml >"%USERPROFILE%\SearchResults.tmp"
ren "%USERPROFILE%\SearchResults.tmp" "SearchResults.txt"
First the current directory is changed to root of drive C:.
Next a perhaps existing search results file from a previous run on desktop of current user is deleted.
Then findstr is executed to search for the string case-insensitive in the files specified with wildcards on the command line. More file extensions can be appended. There is no file extension for "all text files".
findstr prints for each found occurrence the name of the file and the found line to stdout which is redirected into a text file with file extension tmp to desktop of current user. It is not advisable to give the results file a file extension being specified also on command line of findstr.
Finally the created results file is renamed to change file extension from tmp to txt.
But searching for a string in thousands of files with writing all found lines with name of file into a results file needs time. On second run the tasks finishes already faster because Windows will have many files already loaded in cache and does not access a second time the storage media for those files not changed in the meantime.
BTW: A case-sensitive search done with removing /I is much faster than a case-insensitive search.

Windows CMD search

Description:
Since my previous post of using a .bat file to search for file names (Find multiple files from the command line),
I have figured out how to run a .bat file to search a server for specific file names. Now, I want to do the same thing but I want to include in my search the entire contents of a directory.
All of this will be done in windows command prompt and of course a notepad file if needed. I do not have access to linux or unix so please no responses that include such.
Example -
I Drive
+Drawings
++Structural
So for the example above I want to take the entire contents of dir structural (which may be 1000s of .dwg) and using a .bat file search a server with it.
Also I put these commands in notepad and renamed it from a .txt to a .bat
My single file search
dir [file.dwg] /s/4
Entire Directory Search (which does not work this is what I am trying to do)
dir [original dir] /s/4
After i finished writing my .bat file in notepad I would simply put it in the server directory location that I needed to search and run it.
I hope I have made myself clear and I hope that you can help because Im not sure what to do here.
See(Folder Comparisons Via Command Line) for what I am trying to do only I need to compare the directory and all sub directories.
In an effort to understand what you want, here is an example batch file:
DIR G:\Structural /S /B
G:\Structural\cad2012.dwg
G:\Structural\cad2013.dwg
G:\Structural\cad2014.dwg
G:\Structural\photo2012.jpg
If you just want to find the files with 2012
DIR G:\Structural /S /B | FINDSTR 2012
G:\Structural\cad2012.dwg
G:\Structural\photo2012.jpg
If you just want to find the dwg files from 2012
DIR G:\Structural /S /B | FINDSTR 2012 | FINDSTR /R /I dwg$
G:\Structural\cad2012.dwg
Have a look at what I wrote in response to the question "batch file - Compare in windows command prompt" which I think is getting close to what you want to do.

windows cmd: searching files with specific path

Maybe trivial question but certainly hope someone can help me here..
Windows cmd:
I want to search for files in a root directory and its subdirectories where the files have a parent directory with a specific name. (in this example: \TO). This parent directory has always the same name but can be another directory (if the path is different)
I tried this but doesn't seem to work: dir \root /b /s *\TO *.java
I also tried to pipe the output of dir to find.. But cannot make 'find' to show the lines containing the desired paths..
From the cmd prompt:
for /r \ %a in (to\*.java) do #echo %a

Copy a file to all folders batch file?

I need copy credits.jpg from C:\Users\meotimdihia\Desktop\credits.jpg to D:\Software\destinationfolder and all subfolders
I read many and i write
/R "D:\Software\destinationfolder" %%I IN (.) DO COPY "C:\Users\meotimdihia\Desktop\credits.jpg" "%%I\credits.jpg"
then i save file saveall.bat but i run it , it dont work at all.
help me write 1 bat
Give this a try:
for /r "D:\Software\destinationfolder" %i in (.) do #copy "C:\Users\meotimdihia\Desktop\credits.jpg" "%i"
Of course, if it's to go into a batch file, double the '%'.
This was the first search I found on google for batch file copy file to all subfolders.
Here's a way with xcopy.
There's also robocopy but that would be too powerful for a simple task like this. (I've used that for entire drive backups because it can use multi-threading)
But, let us focus on xcopy.
This example is for saving to a file with the extension .bat. Just drop the additional % where there is two if running directly on the command line.
cd "D:\Software\destinationfolder"
for /r /d %%I in (*) do xcopy "C:\temp\file.ext" "%%~fsI" /H /K
cd "D:\Software\destinationfolder" change directory to the folder you want to copy the file to. Wrap this in quotes if the path has whitespaces.
the for loop - See help here. Or type for /? in a command prompt.
/r - Loop through files (recurse subfolders)
/d - Loop through several folders
%%I - %%parameter: A replaceable parameter
xcopy - Type xcopy /? in the command line for lots of help. You may need to press Enter to read the entire help on this.
C:\temp\file.ext - The file you want to copy
"%%~fsI" - Expands %%I to a full pathname with short names only
/H - Copies files with hidden and system file attributes. By default, xcopy does not copy hidden or system files
/K - Copies files and retains the read-only attribute on Destination files if present on the Source files. By default, xcopy removes the read-only attribute.
The last two parameters are just examples if you're having trouble with any read-only files and will retain the most important file properties.
Lots more xcopy parameters here
xcopy examples here
Just for completeness. This example below will copy the same file in each folder of the current directory and not any sub-folders. Just the /r option is removed for it to behave like this.
for /d %%I in (*) do xcopy "C:\temp\file.ext" "%%~fsI" /H /K
If you can use it: Here is a PowerShell solution (PowerShell is integrated in Windows 7 and available from XP and up):
$file = "C:\...\yourfile.txt"
$dir = "C:\...\YourFolder"
#Store in sub directories
dir $dir -recurse | % {copy $file -destination $_.FullName}
#Store in the directory
copy $file -destination $dir
I'm pretty sure that the last line can be integrated in dir ... but I'm not sure how (I do not use PowerShell very often).

How to search a hard drive for a directory name using DOS

Is there a way to find a file on the C: of a computer , using a DOS command, without having to CD to the root of the C: drive? I would like to do it using the DIR and the FINDSTR command only.
Ultimately, my goal is to search for a file and then store the parent directory name in a ENV variable without changing directories and hopefully without creating a temp file.
Have a look at How to find a file in MS-DOS.
dir *bob*.* /s
See also List of DOS commands
This gets the complete file name with directory in a useable form.
dir C:\FILENAME.EXT /s /b
c:> findstr /s /i *.ext
REM finds in drive c: every file called *.ext containing (case insensitive)

Resources