Reputation: 625
EDIT:
This seems to work. Hope it helps someone:
Get-ChildItem C:\Users\$env:username\Desktop\RD |
Where-Object {$_.Name -match "^[^8]*141.txt"} |
Foreach-Object {$_
Get-Content -Path C:\Users\$env:username\Desktop\RD\$_ |
Out-File C:\Users\$env:username\Desktop\RD\141Master.txt
}
I'm trying to work out filtering file names for a more explicit appending process. So I can do this:
Get-Content C:\erik\*.txt | Out-File C:\erik\whatever.txt
And all the text files append. Then I can do this:
Get-Content C:\erik\*101.txt | Out-File C:\erik\whatever.txt
And all the files with 101 in them append. But when I try something like this:
Get-Content C:\erik\^[^8]*141.txt | Out-File C:\erik\whatever.txt
I get:
Get-Content : An object at the specified path C:\Users\edarling\Desktop\RD\^[^8]*141.txt does not exist, or has been filtered by the -Include or -Exclude parameter. At line:1 char:1 + Get-Content C:\Users\edarling\Desktop\RD\^[^8]*141.txt | Out-File C:\Users\edarl ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (System.String[]:String[]) [Get-Content], Exception + FullyQualifiedErrorId : ItemNotFound,Microsoft.PowerShell.Commands.GetContentCommand
I've been trying to pipe Get-ChildItem to Get-Content, but can't quite figure it out. Any suggestions out there?
Thanks
Upvotes: 0
Views: 2782
Reputation: 200193
Get-Content
supports only globbing, not regular expressions. With the former you can only do things like this:
141.txt
: *141.txt
foo
and end with a d
or t
: foo*[dt]
.doc
: ??.doc
Globbing does not allow you to form an expression to match a name that does not contain particular characters. To Get-Content
your expression ^[^8]*141.txt
means "a file name that begins with a caret followed by either another caret or the character 8 and ends with 141.txt".
If you need to filter by regular expression you have to use the -match
operator:
Get-ChildItem 'C:\some\folder' | ? { $_.Name -match '^[^8]*141\.txt$' }
Note that in regular expressions you need to escape dots if you want to match literal dots (\.
). Unescaped dots matche any character except a line feed. You should also anchor your expression on both sides. Otherwise a regular expression ^[^8]*141.txt
would match not only abc141.txt
, but also something like 141_txt.doc
.
Upvotes: 1