4

I have trouble to use several commands in one Foreach or Foreach-Object loop

My situation is -

  1. I have many text files, about 100. So they are read Get-ChildItem $FilePath -Include *.txt
  2. Every file's structure is same only key information is different. Example

    User: Somerandomname

    Computer: Somerandomcomputer

With -Replace command I remove "User:" and "Computer:" so $User = Somerandomname and $computer = "Somerandomcomputer. In each circle $user and $Computer with -Append should be written to one file. And then next file should be read.

foreach-object { $file = $_.fullname;

should be used, but I can not figure out the right syntax for it. Could someone help me with it?

5
  • Are you using a foreach loop, or the foreach-object cmdlet? I see it should work in the following: Get-ChildItem $FilePath -Include *.txt | ForEach-Object {Processing the files here?} Commented Aug 13, 2013 at 15:18
  • Are User: ... and Computer: .... on the same line, or subsequent lines? i.e. do you asking for a solution that reads 2 lines at a time? Commented Aug 13, 2013 at 17:43
  • Perhaps it's just me, but do you want to read from those 100 files? Or do you want to write to them? And if you want to write to them, where does the data you want to write come from and what structure does it have? What do you want to append where? Commented Aug 13, 2013 at 18:08
  • @latkin User and Computer are different lines Commented Aug 14, 2013 at 6:07
  • @Ansgar Wiechers Data is read from 100 files and written to one with extracted data. Commented Aug 14, 2013 at 6:08

3 Answers 3

5

Assuming you've defined $FilePath, $user, and/or $computer elsewhere, try something like this.

$files = Get-ChildItem $FilePath\*.txt
foreach ($file in $files)
{
  (Get-Content $file) | 
  Foreach-Object { $content = $_ -replace "User:", "User: $user" ; $content -replace "Computer:", "Computer: $computer" } | 
  Set-Content $file
}

You can use ; to delimit additional commands in within the Foreach-Object, for example if you wanted to have separate commands for your user and computer name. If you don't enclose the Get-Content cmdlet with parenthesis you will get an error because that process will still have $file open when Set-Content tries to use it.

Also note that with Powershell, strings in double quotes will evaluate variables, so you can put $user in the string to do something like "User: $user" if you so desired.

Sign up to request clarification or add additional context in comments.

2 Comments

This looks almost what I need but the problem is that I need to set $User and $Computer in loop as well. I have 100 files. Example... File nr.1 contains - Computer: pc1 User: name1 File nr.2 contains - Computer: pc1 User: name2 ...and so on. I write this all to one single file. Edit:I think I will write this as answer, I can format it there better.
Sounds like you can achieve that by using some simple string manipulation and then piping to Out-File per @Ansgar's answer. You will want to iterate a value in your outer loop, and append that value to the current string value.
0

Try this:

gci $FilePath -Include *.txt | % {
  gc $_.FullName | ? { $_ -match '^(?:User|Computer): (.*)' } | % { $matches[1] }
} | Out-File 'C:\path\to\output.txt'

Comments

0

If User and Computer are on separate lines, you need to read the lines two at a time. The ReadCount parameter of Get-Content allows you to do that.

Get-ChildItem $FilePath -Include *.txt `
| Get-Content -ReadCount 2 `
| %{ $user = $_[0] -replace '^User: ', ''; $computer = $_[1] -replace '^Computer: ', ''; "$user $computer" } `
| Out-File outputfile.txt

This makes the assumption that every file contains only lines of the exact form

User: someuser
Computer: somecomputer
User: someotheruser
Computer: someothercomputer
...

If this is not the case, you will need to provide whatever is the exact file format.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.