1

I'm new, so apologies if I'm not asking correctly.

I'm selecting a part of a text file and generating a new one with just that part with this code:

Select-String -Path .\input_file.txt -Pattern '\b\d{1,2}\.\d{1,2}\b' -AllMatches | % { $_.Matches } | % { $_.Value } > .\output_file.txt

This works when I execute it on PowerShell but now I need to do the same with a bunch of files in a directory... Is there a way to do the same as a batch script?

I'm not able to figure it out :(

Many thanks in advance!

4
  • batch and powershell are two different scripting languages. Batch is very much inferior to powershell. I therefor recommend to use Powershell only. If you meant batch processing, then you should remove the batch tag, as it is implying you are looking for a .bat or .cmd script, rather than a .ps1 script.
    – LPChip
    Commented Nov 22, 2022 at 15:39
  • 1
    Why combine Powershell and Batch together? Whatever you wanted that batch script to do can surely be done as a Powershell script. You can also run Powershell scripts from explorer just like batch scripts, it's not only an interactive console.
    – Romen
    Commented Nov 22, 2022 at 15:52
  • Yes, you can run that powershell one liner from batch. Why people are so passionate about you using only powershell is beyond me. It's your environment, mix and match them as you please. Here is a simple example for /f %%a in ('powershell -Command "Get-Date -format yyyy_MM_dd__HH-mm-ss"') do set datetime=%%a Do something line PowerShell -Command "YOUR POWERSHELL CODE HERE" .. You will need to escape your > to get it to work. First make it work going to the screen. Then add the > part and escape it. Sometimes, the escaping is so cumbersome and just isn't worth it. Commented Nov 22, 2022 at 16:49
  • On second thought, you can put the >yourfile.txt OUTSIDE of the quoted powershell command and you won't need to escape it. Commented Nov 22, 2022 at 16:50

1 Answer 1

0

With Powershell, you can iterate through a list of files and execute a command afterwards.

For example:

# get all textfiles in a variable.
$files = get-childitem *.txt

# Loop collection and perform action on each iteration
$files | foreach-object {
    $filename = $_.name
    Select-String -Path $filename -Pattern '\b\d{1,2}\.\d{1,2}\b' -AllMatches | `
        % { $_.Matches } | % { $_.Value } >> .\output_file.txt

}
4
  • Many thanks! That works and I don't need a batch file... There's just one problem: The "output_file.txt" only saves the information from the last text file. Is it posible to save that information on different files? For instance, from the first text file (text_001.txt) to another text file (text_match_001.txt) and so on. I'm sure that this is very basic but I'm not an expert whit scripts... I'm just learning. Thank you so much for your help!
    – Ninu
    Commented Nov 23, 2022 at 7:33
  • Nevermind, I figured it out: .\'Match_'$filename :)
    – Ninu
    Commented Nov 23, 2022 at 9:24
  • Ah, I made an edit so it now appends to the file rather than overwriting it. :) >> instead of >
    – LPChip
    Commented Nov 23, 2022 at 10:59
  • Awesome! Thanks! :)
    – Ninu
    Commented Nov 23, 2022 at 11:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .