;------------------------------------------------------------------------------- ; Program Name: Search.lsp [Search R2] ; Created By: Terry Miller (Email: terrycadd@yahoo.com) ; (URL: http://web2.airmail.net/terrycad) ; Date Created: 7-11-05 ; Function: Searches for a word or phrase in files ; Note: Search requires functions inside of GetIcon.lsp. ;------------------------------------------------------------------------------- ; Revision History ; Rev By Date Description ;------------------------------------------------------------------------------- ; 1 TM 7-11-05 Initial version ; 2 TM 10-20-06 Revised dialog functions ;------------------------------------------------------------------------------- ; c:Search - Searches for a word or phrase in files ;------------------------------------------------------------------------------- (defun c:Sea ()(c:Search));Shortcut (defun c:Search (/ Browse: Chk_Path: Dcl_Id% FileName$ FileTitle$ FileName% First GetExtensions: List003@ Loop PathFilename$ Return# Save002$ SearchFiles@ SearchResults@ Text$ Var001$ Var002$ Var003$ Verify_Info:) (setvar "CMDECHO" 0) (princ "\nSearch for a word or phrase in files")(princ) (Search_Support) ;----------------------------------------------------------------------------- ; Chk_Path: - Checks if path is a valid folder ;----------------------------------------------------------------------------- (defun Chk_Path: (/ FileName$ FileName% Return@) (setq Var002$ (BackSlash Var002$)) (if (> (strlen Var002$) 0) (progn (if (/= (substr Var002$ (strlen Var002$)) (chr 92)) (setq Var002$ (strcat Var002$ (chr 92))) );if (set_tile "Edit002" Var002$) (setq Return@ (vl-directory-files Var002$ "*.*" 1)) );progn );if (if Return@ (progn (setq List003@ (GetExtensions: (strcat Var002$ "*.*"))) (if (not (member Var003$ List003@)) (setq Var003$ "*.*") );if (setq Save002$ Var002$) );progn (progn (GetOK "Search Message" (strcat "The folder entered to Look in was not found." "\nSelect the Browse button to locate it.") "inform") (setq Var002$ Save002$) (set_tile "Edit002" Var002$) (mode_tile "Browse" 2) );progn );if );defun Chk_Path: ;----------------------------------------------------------------------------- ; GetExtensions: - Returns a list of file extensions in PathFilename$ ;----------------------------------------------------------------------------- (defun GetExtensions: (PathFilename$ / Extension$ FileName$ FileName% Files@ PathName$ Return@) (setq PathName$ (GetPath PathFilename$)) (setq Files@ (vl-directory-files PathName$ "*.*" 1)) (setq Return@ (list "*.*")) (foreach FileName$ Files@ (setq Extension$ (strcat "*." (GetExtension FileName$))) (if (not (member Extension$ Return@)) (setq Return@ (append Return@ (list Extension$))) );if );foreach (setq Return@ (acad_strlsort Return@)) );defun GetExtensions: ;----------------------------------------------------------------------------- ; Browse: - Browse to select new search folder to look in ;----------------------------------------------------------------------------- (defun Browse: (/ PathFilename$) (if (setq PathFilename$ (getfiled " Select a File in a Folder to Look in:" Var002$ "" 2)) (progn (setq Var002$ (GetPath PathFilename$)) (setq Save002$ Var002$) (setq Var003$ (strcat "*." (GetExtension PathFilename$))) (setq List003@ (GetExtensions: PathFilename$)) (set_tile "Edit002" Var002$) (set_tile_list "List003" List003@ Var003$) (mode_tile "accept" 2) );progn );if );defun Browse: ;----------------------------------------------------------------------------- ; Verify_Info: - Verify information before exiting ;----------------------------------------------------------------------------- (defun Verify_Info: () (if (/= Var001$ "") (progn (setq Loop nil) (done_dialog 1) );progn (progn (GetOK "Search Message" "Enter a word or phrase to search for." "inform") (mode_tile "Edit001" 2) );progn );if );defun Verify_Info: ;----------------------------------------------------------------------------- ; Set Default Variables and List Values ;----------------------------------------------------------------------------- (if (not *Search@) (cond ((findfile "AcadDoc.lsp") (setq *Search@ (list (GetExtensions: (findfile "AcadDoc.lsp")) "" (GetPath (findfile "AcadDoc.lsp")) "*.lsp")) );case ((findfile "Acad.lsp") (setq *Search@ (list (GetExtensions: (findfile "Acad.lsp")) "" (GetPath (findfile "Acad.lsp")) "*.lsp")) );case (t (setq *Search@ (list (GetExtensions: "C:\\*.*") "" "C:\\" "*.*"))) );cond );if (setq List003@ (nth 0 *Search@)) (setq Var001$ (nth 1 *Search@) Var002$ (nth 2 *Search@) Var003$ (nth 3 *Search@) );setq (setq Save002$ Var002$) ;----------------------------------------------------------------------------- ; Load Dialog ;----------------------------------------------------------------------------- (setq Loop t) (while Loop (setq Dcl_Id% (load_dialog "Search.dcl")) (new_dialog "Search" Dcl_Id%) ;--------------------------------------------------------------------------- ; Set Dialog Initial Settings ;--------------------------------------------------------------------------- (setq Var002$ Save002$) (set_tile "Title" " Search") (set_tile "Text001" "Word or phrase:") (set_tile "Edit001" Var001$) (set_tile "Text002" "Look in:") (set_tile "Edit002" Var002$) (set_tile "Text003" "File type:") (set_tile_list "List003" List003@ Var003$) ;--------------------------------------------------------------------------- ; Dialog Actions ;--------------------------------------------------------------------------- (action_tile "Edit001" "(setq Var001$ $value)") (action_tile "Edit002" "(setq Var002$ $value)(Chk_Path:)") (action_tile "List003" "(set_list_value \"List003@\" \"Var003$\")") (action_tile "Browse" "(Browse:)") (action_tile "accept" "(Verify_Info:)") (setq Return# (start_dialog)) (unload_dialog Dcl_Id%) (if (= Return# 0) (exit)) );while (setq *Search@ (list List003@ Var001$ Var002$ Var003$) );setq (setq SearchFiles@ (vl-directory-files Var002$ Var003$ 1)) (setq SearchResults@ (list (strcat "Search of \"" Var001$ "\" in " Var003$ " file(s) in the " Var002$ " folder."))) (foreach FileName$ SearchFiles@ (setq FileTitle$ (strcat "[ " FileName$ " ]" (String$ (- 76 (strlen FileName$)) "-"))) (setq PathFilename$ (strcat Var002$ FileName$)) (setq First t) (setq FileName% (open PathFilename$ "r")) (while (setq Text$ (read-line FileName%)) (if (wcmatch (strcase Text$) (strcat "*" (strcase Var001$) "*")) (progn (if First (progn (setq First nil) (setq SearchResults@ (append SearchResults@ (list ""))) (setq SearchResults@ (append SearchResults@ (list FileTitle$))) );progn );if (setq SearchResults@ (append SearchResults@ (list Text$))) );progn );if );while (close FileName%) );foreach (if (> (length SearchResults@) 1) (progn (setq FileName% (open "C:\\Temp\\Search.txt" "w")) (foreach Text$ SearchResults@ (write-line Text$ FileName%) );foreach (close FileName%) (startapp "Notepad C:\\Temp\\Search.txt") );progn (GetOK "Search Message" (strcat "Search of \"" Var001$ "\" in " Var003$ " file(s)\nin the " Var002$ " folder\ndid not find any results to display.") "inform") );if (princ) );defun c:Search ;------------------------------------------------------------------------------- ; BackSlash - Changes forward slashes to back slashes in a string ; Arguments: 1 ; Str$ = String to change forward to back slashes. ; Returns: String with forward slashes changed. ;------------------------------------------------------------------------------- (defun BackSlash (Str$ / Cnt# NewStr$) (setq Cnt# 1 NewStr$ "") (while (<= Cnt# (strlen Str$)) (if (= (substr Str$ Cnt# 1) "/") (setq NewStr$ (strcat NewStr$ (chr 92))) (setq NewStr$ (strcat NewStr$ (substr Str$ Cnt# 1))) );if (setq Cnt# (1+ Cnt#)) );while NewStr$ );defun BackSlash ;------------------------------------------------------------------------------- ; GetExtension - Uses the format of the return of the findfile and getfiled ; functions and returns a string of the extension. ; Arguments: 1 ; PathFilename$ = Path and filename string ; Returns: Extension$ string of the extension. ;------------------------------------------------------------------------------- (defun GetExtension (PathFilename$ / Cnt# Mid# Extension$) (setq Cnt# 1 Mid# 0) (while (< Cnt# (strlen PathFilename$)) (if (= (substr PathFilename$ Cnt# 1) ".") (setq Mid# Cnt#) );if (setq Cnt# (1+ Cnt#)) );while (setq Extension$ (strcase (substr PathFilename$ (1+ Mid#)) t)) (if (= Extension$ (strcase PathFilename$ t)) (setq Extension$ "*") );if Extension$ );defun GetExtension ;------------------------------------------------------------------------------- ; GetFilename - Uses the format of the return of the findfile and getfiled ; functions and returns a string of the filename without the extension. ; Arguments: 1 ; PathFilename$ = Path and filename string ; Returns: Filename$ string without the extension. ;------------------------------------------------------------------------------- (defun GetFilename (PathFilename$) (vl-filename-base PathFilename$) );defun GetFilename ;------------------------------------------------------------------------------- ; GetFolder - Used to get the folder name from the path name. ; Arguments: 1 ; PathName$ = Path name ; Returns: Folder$ string name ;------------------------------------------------------------------------------- (defun GetFolder (PathName$ / Cnt# Num#) (setq PathName$ (BackSlash PathName$)) (setq PathName$ (substr PathName$ 1 (1- (strlen PathName$)))) (setq Cnt# 1 Num# 1) (repeat (strlen PathName$) (if (= (substr PathName$ Cnt# 1) (chr 92)) (setq Num# Cnt#) );if (setq Cnt# (1+ Cnt#)) );repeat (substr PathName$ (1+ Num#)) );defun GetFolder ;------------------------------------------------------------------------------- ; GetPath - Used to get the path from the path and filename. ; Arguments: 1 ; PathFilename$ = Path and filename string ; Returns: Pathname string. ;------------------------------------------------------------------------------- (defun GetPath (PathFilename$) (strcat (vl-filename-directory PathFilename$) "\\") );defun GetPath ;------------------------------------------------------------------------------- ; String$ - Adds a number of characters or strings together. ; Arguments: 2 ; Num# = Number of characters or strings to add together ; Char$ = Character or string to add ; Syntax: (String$ 80 "-") ; Returns: New string with number of characters or strings. ;------------------------------------------------------------------------------- (defun String$ (Num# Char$ / AddChars$) (setq AddChars$ "") (if (> Num# 0) (repeat Num# (setq AddChars$ (strcat Char$ AddChars$))) );if AddChars$ );defun String$ ;------------------------------------------------------------------------------- ; Search_Support - Checks to see if supporting functions are loaded ;------------------------------------------------------------------------------- (defun Search_Support () (if (or (not GetOK)(not EditBox)) (progn (if (or (not GetOK)(not EditBox)) (if (findfile "GetIcon.lsp") (load "GetIcon.lsp") );if );if (if (or (not GetOK)(not EditBox)) (progn (alert (strcat "Search requires the functions inside of GetIcon.lsp." "\nDownload the latest versions from AutoLISP Exchange," "\n(URL: http://web2.airmail.net/terrycad).") );alert (exit) );progn );if );progn );if );defun Search_Support ;------------------------------------------------------------------------------- (princ);End of Search.lsp