ZTree.com  | ZEN  | About...  

 Index   Back

[OT] Example BAT script   [OT]

By: Slobodan Vujnovic   Homepage   France  
Date: Jan 11,2023 at 16:57
In Response to: [OT] ChatGPT needs to learn a lot more (Slobodan Vujnovic)

If you can't wrap your mind around nested FOR loops, this can
get you started. By the way, I've never seen such use of double
quotes in the SET command.

@echo off
:: ChatGPT prompt used: windows batch script to find strings in *.txt files
setlocal enabledelayedexpansion
set "search=%1"
set "dir=%cd%"
if "%search%"=="" (
echo Please provide a search string.
exit /b
)

for /f "delims=" %%a in ('dir /b /s /a-d "%dir%\*.txt"') do (
set "content="
:: SV: To work with files with spaces, quote "%%a" below
for /f "delims=" %%b in ("%%a") do set "content=!content!%%b"
if "!content!"=="" goto continue
echo %%a
findstr /c:"%search%" "%%a"
)

340 views      
 

Messages in this Thread

 
96,626 Postings in 12,229 Threads, 350 registered users, 73 users online (0 registered, 73 guests)
Index | Admin contact |   Forum Time: Feb 24, 2024 - 6:27 pm UTC  |  Hits:61,576,034  (20,704 Today )
RSS Feed