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
}

531 views      
 

Messages in this Thread

 
96,656 Postings in 12,233 Threads, 350 registered users, 26 users online (0 registered, 26 guests)
Index | Admin contact |   Forum Time: May 18, 2024 - 4:20 am UTC  |  Hits:63,516,527  (1,231 Today )
RSS Feed