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,640 Postings in 12,231 Threads, 350 registered users, 49 users online (0 registered, 49 guests)
Index | Admin contact |   Forum Time: Apr 12, 2024 - 2:11 pm UTC  |  Hits:62,817,650  (13,181 Today )
RSS Feed