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."
Get-ChildItem -Recurse -Filter "*.txt" | ForEach-Object {
$content = Get-Content $.FullName
if (!$content) { continue }
Write-Output $.FullName
Select-String -Pattern $search -InputObject $content


Messages in this Thread

96,626 Postings in 12,229 Threads, 350 registered users, 75 users online (1 registered, 74 guests)
Index | Admin contact |   Forum Time: Feb 24, 2024 - 7:55 pm UTC  |  Hits:61,577,874  (22,544 Today )
RSS Feed