格式化 Remove-Item 命令的详细输出 [英] Formatting the verbose output of a Remove-Item command

查看:39
本文介绍了格式化 Remove-Item 命令的详细输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下命令:

Get-ChildItem $build_path `
    -Include *.bak, *.orig, *.txt, *.chirp.config `
    -Recurse | Remove-Item -Verbose

从 VS 解决方案的构建文件夹中清除一些文件.我使用详细开关,以便我可以看到哪些文件被删除.它工作正常,但输出过于冗长:

to clear some files from the build folder of a VS solution. I use the Verbose switch so that I can see which files are being deleted. It works fine but the output is too verbose:

VERBOSE: Performing operation "Remove File" on Target "R:\Visual Studio 2010\Projects\SomeProject\SomeProject.Web.build\App_Readme\glimpse.mvc3.readme.txt".
VERBOSE: Performing operation "Remove File" on Target "R:\Visual Studio 2010\Projects\SomeProject\SomeProject.Web.build\App_Readme\glimpse.readme.txt".

我只需要看到类似的东西:

I just need to see something like that:

Removing file \App_Readme\glimpse.mvc3.readme.txt".
Removing file \App_Readme\glimpse.readme.txt".
...

我知道我可以用 foreach 语句和 Write-Host 命令来做到这一点,但我相信它可以通过一些流水线或其他东西来完成.有什么想法吗?

I know i can do this with a foreach statement and a Write-Host command, but I believe it can be done with some pipelining or something. Any ideas?

推荐答案

使用 ForEach-Object 非常简单:

Get-ChildItem $build_path `
    -Include *.bak, *.orig, *.txt, *.chirp.config `
    -Recurse | foreach{ "Removing file $($_.FullName)"; Remove-Item $_}

正如@user978511 指出的那样,使用详细输出更复杂:

As @user978511 pointed out, using the verbose output is more complicated:

$ps = [PowerShell]::Create()

$null = $ps.AddScript(@'
    Get-ChildItem $build_path `
        -Include *.bak, *.orig, *.txt, *.chirp.config `
        -Recurse | Remove-Item -Verbose
'@)

$ps.Invoke()
$ps.Streams.Verbose -replace '(.*)Target "(.*)"(.*)','Removing File $2'

这篇关于格式化 Remove-Item 命令的详细输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆