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"


Messages in this Thread

96,640 Postings in 12,231 Threads, 350 registered users, 65 users online (0 registered, 65 guests)
Index | Admin contact |   Forum Time: Apr 12, 2024 - 3:11 pm UTC  |  Hits:62,818,531  (14,062 Today )
RSS Feed