How can I change the following code to look at all the .log files in the directory and not just the one file?
如何更改以下代码以查看目录中的所有.log文件而不仅仅是一个文件?
I need to loop through all the files and delete all lines that do not contain "step4" or "step9". Currently this will create a new file, but I'm not sure how to use the for each
loop here (newbie).
我需要遍历所有文件并删除所有不包含“step4”或“step9”的行。目前这将创建一个新文件,但我不知道如何在这里使用for each循环(新手)。
The actual files are named like this: 2013 09 03 00_01_29.log. I'd like the output files to either overwrite them, or to have the SAME name, appended with "out".
实际文件的名称如下:2013 09 03 00_01_29.log。我希望输出文件覆盖它们,或者具有SAME名称,附加“out”。
$In = "C:\Users\gerhardl\Documents\My Received Files\Test_In.log"
$Out = "C:\Users\gerhardl\Documents\My Received Files\Test_Out.log"
$Files = "C:\Users\gerhardl\Documents\My Received Files\"
Get-Content $In | Where-Object {$_ -match 'step4' -or $_ -match 'step9'} | `
Set-Content $Out
4 个解决方案
#1
250
Give this a try:
尝试一下:
Get-ChildItem "C:\Users\gerhardl\Documents\My Received Files" -Filter *.log |
Foreach-Object {
$content = Get-Content $_.FullName
#filter and save content to the original file
$content | Where-Object {$_ -match 'step[49]'} | Set-Content $_.FullName
#filter and save content to a new file
$content | Where-Object {$_ -match 'step[49]'} | Set-Content ($_.BaseName + '_out.log')
}
#2
43
To get the content of a directory you can use
获取可以使用的目录的内容
$files = Get-ChildItem "C:\Users\gerhardl\Documents\My Received Files\"
Then you can loop over this variable as well:
然后你也可以遍历这个变量:
for ($i=0; $i -lt $files.Count; $i++) {
$outfile = $files[$i].FullName + "out"
Get-Content $files[$i].FullName | Where-Object { !($_ -match 'step4' -or $_ -match 'step9') } | Set-Content $outfile
}
#3
14
If you need to loop inside a directory recursively for a particular kind of file, use the below command, which filters all the files of doc
file type
如果需要以递归方式为特定类型的文件循环内部目录,请使用以下命令,该命令将过滤doc文件类型的所有文件
$fileNames = Get-ChildItem -Path $scriptPath -Recurse -Include *.doc
$ fileNames = Get-ChildItem -Path $ scriptPath -Recurse -Include * .doc
If you need to do the filteration on multiple types, use the below command.
如果需要对多种类型进行过滤,请使用以下命令。
$fileNames = Get-ChildItem -Path $scriptPath -Recurse -Include *.doc,*.pdf
$ fileNames = Get-ChildItem -Path $ scriptPath -Recurse -Include * .doc,*。pdf
Now $fileNames
variable act as an array from which you can loop and apply your business logic.
现在$ fileNames变量充当一个数组,您可以从中循环并应用业务逻辑。
#4
0
Other answers are great, I just want to add... a different approach usable in PowerShell: Install GNUWin32 utils and use grep to view the lines / redirect the output to file http://gnuwin32.sourceforge.net/
其他答案很棒,我只想添加... PowerShell中可用的不同方法:安装GNUWin32 utils并使用grep查看行/将输出重定向到文件http://gnuwin32.sourceforge.net/
This overwrites the new file every time:
这会每次都覆盖新文件:
grep "step[49]" logIn.log > logOut.log
This appends the log output, in case you overwrite the logIn file and want to keep the data:
这会附加日志输出,以防您覆盖logIn文件并希望保留数据:
grep "step[49]" logIn.log >> logOut.log
Note: to be able to use GNUWin32 utils globally you have to add the bin folder to your system path.
注意:为了能够全局使用GNUWin32 utils,您必须将bin文件夹添加到系统路径中。
#1
250
Give this a try:
尝试一下:
Get-ChildItem "C:\Users\gerhardl\Documents\My Received Files" -Filter *.log |
Foreach-Object {
$content = Get-Content $_.FullName
#filter and save content to the original file
$content | Where-Object {$_ -match 'step[49]'} | Set-Content $_.FullName
#filter and save content to a new file
$content | Where-Object {$_ -match 'step[49]'} | Set-Content ($_.BaseName + '_out.log')
}
#2
43
To get the content of a directory you can use
获取可以使用的目录的内容
$files = Get-ChildItem "C:\Users\gerhardl\Documents\My Received Files\"
Then you can loop over this variable as well:
然后你也可以遍历这个变量:
for ($i=0; $i -lt $files.Count; $i++) {
$outfile = $files[$i].FullName + "out"
Get-Content $files[$i].FullName | Where-Object { !($_ -match 'step4' -or $_ -match 'step9') } | Set-Content $outfile
}
#3
14
If you need to loop inside a directory recursively for a particular kind of file, use the below command, which filters all the files of doc
file type
如果需要以递归方式为特定类型的文件循环内部目录,请使用以下命令,该命令将过滤doc文件类型的所有文件
$fileNames = Get-ChildItem -Path $scriptPath -Recurse -Include *.doc
$ fileNames = Get-ChildItem -Path $ scriptPath -Recurse -Include * .doc
If you need to do the filteration on multiple types, use the below command.
如果需要对多种类型进行过滤,请使用以下命令。
$fileNames = Get-ChildItem -Path $scriptPath -Recurse -Include *.doc,*.pdf
$ fileNames = Get-ChildItem -Path $ scriptPath -Recurse -Include * .doc,*。pdf
Now $fileNames
variable act as an array from which you can loop and apply your business logic.
现在$ fileNames变量充当一个数组,您可以从中循环并应用业务逻辑。
#4
0
Other answers are great, I just want to add... a different approach usable in PowerShell: Install GNUWin32 utils and use grep to view the lines / redirect the output to file http://gnuwin32.sourceforge.net/
其他答案很棒,我只想添加... PowerShell中可用的不同方法:安装GNUWin32 utils并使用grep查看行/将输出重定向到文件http://gnuwin32.sourceforge.net/
This overwrites the new file every time:
这会每次都覆盖新文件:
grep "step[49]" logIn.log > logOut.log
This appends the log output, in case you overwrite the logIn file and want to keep the data:
这会附加日志输出,以防您覆盖logIn文件并希望保留数据:
grep "step[49]" logIn.log >> logOut.log
Note: to be able to use GNUWin32 utils globally you have to add the bin folder to your system path.
注意:为了能够全局使用GNUWin32 utils,您必须将bin文件夹添加到系统路径中。