ZTree.com  | ZEN  | About...  

 Index   Back

[OT] Example BAT script converted to PowerShell   [OT]

By: Slobodan Vujnovic   Homepage   France  
Date: Jan 16,2023 at 22:49
In Response to: [OT] Example BAT script (Slobodan Vujnovic)

PROMPT: Convert this BAT script to PowerShell:

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

$search = $args[0]
$dir = Get-Location
if (!$search) {
Write-Output "Please provide a search string."
exit
}
Get-ChildItem -Recurse -Filter "*.txt" | ForEach-Object {
$content = Get-Content $.FullName
if (!$content) { continue }
Write-Output $.FullName
Select-String -Pattern $search -InputObject $content
}

54 views      
 

Messages in this Thread

 
96,194 Postings in 12,153 Threads, 350 registered users, 25 users online (1 registered, 24 guests)
Index | Admin contact |   Forum Time: Jan 27, 2023 - 1:54 am UTC  |  Hits:53,204,247  (1,480 Today )
RSS Feed