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"
)

71 views      
 

Messages in this Thread

 
96,194 Postings in 12,153 Threads, 350 registered users, 29 users online (2 registered, 27 guests)
Index | Admin contact |   Forum Time: Jan 27, 2023 - 12:40 am UTC  |  Hits:53,203,339  (572 Today )
RSS Feed