lördag 22 Nov 2008, 16:12
…from a command box with these batch files i made to emulate the "locate" functionality in many Linux distros:
Create these files in a convenient location, e.g. "C:\index\". Create if needed.
index.bat:
@echo offfindex.bat:
dir /s /a %1:\ > %~dp0files_%1.txt
@echo offUsage:
rem Regular expression version:
rem findstr /NIR "%2" %~dp0files_%1.txt
findstr /NI /C:%2 %~dp0files_%1.txt
index <drive letter>
Will store the file info on the specified drive in <location of batch file>files_<drive letter>.txt
Uses about 0.6 MB per GB of small files.
findex <drive letter> "<search term(s)>"
That will show you the hits and the line numbers you need to look up the folders they're in. You can also skip this step and use your text viewer to search.
If you don't want to look up the path manually, install Python (pre-installed with OS X and many Linux distros) and use this:
locate.bat:
@echo offlocate.py:
python %~dp0locate.py %1 %2
# Locate files using dir cache.Speaking of paths, your system checks specified paths for the executable you wish to run; add Python and batch file paths to your environment variables so you can run them from any location.
# By CT, 22nov08.
# 27nov08 v1.1 - Handle lowercase drive input. Multiple drives at once. (python locate.py "c,d,e" "stuff you want to find") (no spaces in drive list)
# 28dec08 v1.2 - Default drives.
# 05jan09 v1.2.1 - Fix path when called from other location.import os, re, sys
if len(sys.argv) == 2:
drives = "C,D,E,I,J".split(",")
term = sys.argv
else:
drives = sys.argv.upper().split(",")
term = sys.argvmy_dir = os.path.dirname(sys.argv)
for drive in drives:
#print "Searching %s for %r…" % (drive, term)
fp = open(my_dir+"\\files_"+drive+".txt", "r")
drive_path = drive + ":\\"
last_folder = ""
current_folder = ""while 1:
line = fp.readline()
if not line:
break
i = line.find(drive_path)
if i >= 0:
current_folder = line
elif re.search(term, line, re.I):
if current_folder != last_folder:
print current_folder
last_folder = current_folder
print line
If you wish to modify this, i suggest SciTE and the built-in help of Python, e.g.:
python -c "import os; help(os.path.split)"
Introspection is useful, too:
python -c "import os; print dir(os.path)"
Beware that mistakes are easy to make in a command line interface, especially with rightclicks possibly executing your clipboard.
Comments and questions are welcome.