Windows CMD search - windows

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.

Related

dir command output different results between local dirves and network drives [duplicate]

This question already has an answer here:
Why are also *.tiff files output by Windows command DIR on searching for *.tif files?
(1 answer)
Closed 2 years ago.
I just test a piece of cmd on my computer:
C:\Users\user>dir D:\test\*.xls /b
test - copy.xls
test - copy.xlsx
test.xls
test.xlsx
C:\Users\user>dir Y:\test\*.xls /b
test - copy.xls
test.xls
The two test file fold is absolutely the same. Dirve "D" is a local dirve and dirve "Y" is a network dirve. I checked the version of my machine and the source machine of dirve "Y", they were different indeed. Here are my questions:
I only plan to run my program on local machine, is there an option to always output the second results?
If it is "No" to question 1, may I add some option like -*.xlsx to remove *.xlsx results from my program?
Thanks in advance.
I can exclude *.xlsx like this:
dir D:\test\*.xls /b | findstr /v /i "\.xlsx$"
Will exclude even xls.* too:
dir D:\test\*.xls /b | findstr /v /i "\.xls.$"
Obviously on your local computer short 8dot3 file names are enabled.
You can see that when running dir /x ...
There are different ways to generate 8dot3 file names. Usually an extension with more than three letters is truncated, e.g
.xlxs leads to .XLS
The dir command always searches in both long filenames and 8dot3 file names.
You have some options to change that behaviour:
Disable 8dot3 file names. This may be done with fsutil.exe, either per drive or for the complete system.
Change the way how 8dot3 file names are generated in the registry.
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem]
"Win95TruncatedExtensions"=dword:00000000
In this case .xlsx would lead to .XL~
Note:
When you do any of these changes, it works first for newly created files.
For older files you should copy them.
But when you want to disable all 8dot3 file names you can use fsutil.exe to strip 8dot3 file names in older files.

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.

VB Script; Identifying file paths of more than 255 characters

I am currently trying to write a VB script in order to identify file paths that are too large (255+), in a large hierarchical network structure. These are usually truncated and end with a tilde (~). I need to get the file paths outputted onto a text file, so that someone can manually decide what to do with them. Scanning the whole server would be too large a job, and so I was hoping to be able to run the script on certain folders and their sub-folders.
I am quite comfortable with VB for access, but have never used VB Script to manipulate directories like this.
I'm using windows 7, and the directories could be server based.
Any help would be greatly appreciated. Thanks :)
If your problematic paths ends with a ~, you can use
dir /s /b x:\someFolder1 | findstr /e /c:"~" > paths.txt
dir /s /b x:\someFolder2 | findstr /e /c:"~" >> paths.txt
....

verifying whether file exists or not in a specified drive and its subfolders?

I am using windows OS. I am trying to find out whether a specific file exists or not in a directory. currently i am using below command.
d:\> c:\Desktop\some.exe
but the problem is above command does not verify in sub folders of d drive. how can i write the command to look even in subfolders?
Thanks!
You can get a list of all files in subdirectories with this command
dir "yourfilename.ext" /s /b
If you want to know is how many times your file "yourfilename.ext" is present then you could extend the previous command with
dir "yourfilename.ext" /s /b | find /C "yourfilename.ext"

How to do a simple file search in cmd

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

Resources