This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearcher.bat
75 lines (61 loc) · 1.92 KB
/
searcher.bat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
@echo off
setlocal enabledelayedexpansion
rem Usage: search_string.bat "search_term" "directory" "log_file" "exclude_files" "exclude_dirs"
set "search_term=%~1"
set "search_dir=%~2"
set "log_file=%~3"
set "exclude_files=%~4"
set "exclude_dirs=%~5"
if "%search_term%"=="" (
echo Usage: %0 "search_term" "directory" "log_file" "exclude_files" "exclude_dirs"
exit /b 1
)
if "%search_dir%"=="" (
echo Usage: %0 "search_term" "directory" "log_file" "exclude_files" "exclude_dirs"
exit /b 1
)
if "%log_file%"=="" (
echo Usage: %0 "search_term" "directory" "log_file" "exclude_files" "exclude_dirs"
exit /b 1
)
if not exist "%search_dir%" (
echo Directory not found: %search_dir%
exit /b 1
)
rem Parse the exclude files into an array
set "exclude_files_array="
for %%e in (%exclude_files%) do (
set "exclude_files_array=!exclude_files_array!%%e "
)
rem Parse the exclude directories into an array
set "exclude_dirs_array="
for %%d in (%exclude_dirs%) do (
set "exclude_dirs_array=!exclude_dirs_array!%%d "
)
echo Searching for "%search_term%" in %search_dir% and logging to %log_file%
echo --- Search results for "%search_term%" in %search_dir% --- > "%log_file%"
for /r "%search_dir%" %%f in (*) do (
set "exclude_file=0"
set "exclude_dir=0"
rem Check if the file should be excluded
for %%e in (%exclude_files_array%) do (
if "%%~nxf"=="%%~nxe" (
set "exclude_file=1"
)
)
rem Check if the file is in an excluded directory
for %%d in (%exclude_dirs_array%) do (
if "!exclude_dir!"=="0" (
echo %%f | findstr /i "%%d\\" > nul
if !errorlevel! equ 0 (
set "exclude_dir=1"
)
)
)
if "!exclude_file!"=="0" if "!exclude_dir!"=="0" (
echo Searching in %%f
findstr /n /i "%search_term%" "%%f" >> "%log_file%"
)
)
echo Search complete. Results saved to %log_file%.
exit /b 0