gpt4 book ai didi

powershell - 删除带有特殊字符的文件

转载 作者:行者123 更新时间:2023-12-02 01:50:27 43 4
gpt4 key购买 nike

我想从构建服务器中清除日志文件,保留最近几天。我的进程创建的日志文件没有问题。这工作正常:

$logFolders = Get-ChildItem $buildBase -Recurse -include logs -Attributes D
$logFolders | ForEach-Object { Get-ChildItem $_.fullname -filter *.log | where >{$_.lastwritetime -lt (get-date).adddays(-$purgeAfter) -and -not $_.psiscontainer} |% {remove-item $_.fullname -force} };

但是,构建工具生成的日志如下所示:
lrFR_WebHelp - Friday, February 28, 2014 [9.00 PM].mclog

我试过引用文件名:
Get-ChildItem $frReports\Reports -filter *.mclog | where {$_.lastwritetime -lt (get-date).adddays(-$purgeAfter) -and -not $_.psiscontainer}| % {write-host ("'" + $frReports + '\Reports'  + '\' + $_ + "'") }

输出:
'D:\scratch\Reports\lrFR_WebHelp - Friday, February 28, 2014 [9.00 PM].mclog'

但试图删除:
Get-ChildItem $frReports\Reports -filter *.mclog | where {$_.lastwritetime -lt (get-date).adddays(-$purgeAfter) -and -not $_.psiscontainer}| % {remove-item ("'" + $frReports + '\Reports'  + '\' + $_ + "'") }

输出:
remove-item : Cannot find drive. A drive with the name ''D' does not exist.
At line:1 char:145
+ ... container}| % {remove-item ("'" + $frReports + '\Reports' + '\' + $_ + "'") }
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: ('D:String) [Remove-Item], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand

更改为 unix 斜杠 ('/') 或转义斜杠 ('\\') 不会改变结果

我试着绕过这条路。没有错误,但也没有删除任何文件。

这个:
    $filesToDelete = Get-ChildItem $frReports\Reports -filter *.mclog | where {$_.lastwritetime -lt (get-date).adddays(-$purgeAfter) -and -not $_.psiscontainer}

Push-Location -path D:\scratch\Reports

$filesToDelete | ForEach-Object {
$fname = ("'" + $_ + "'");
write-host $fname;
remove-item $fname -Force;
remove-item $_;
}

输出这个:
'lrFR_WebHelp - Friday, February 28, 2014 [9.00 PM].mclog'
'lrFR_WebHelp - Friday, March 14, 2014 [10.24 PM].mclog'

但不会删除文件。

最后,在网上窥探线索后,我尝试了 fso 对象
   $fso = New-Object -ComObject Scripting.FileSystemObject
$buildBase = "D:\scratch";
$logFolders = Get-ChildItem $buildBase -Recurse -include Reports -Attributes D


$logFolders | ForEach-Object { Get-ChildItem $fso.GetFile($_.FullName).ShortPath -filter *.MCL | where {$_.lastwritetime -lt (get-date).adddays(-$purgeAfter) -and -not $_.psiscontainer}} #|% {write-host $_.fullname} };

输出:
Exception calling "GetFile" with "1" argument(s): "Exception from HRESULT: 0x800A0035 (CTL_E_FILENOTFOUND)"
At line:1 char:32
+ $logFolders | ForEach-Object { Get-ChildItem $fso.GetFile($_.FullName).ShortPath ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation

没有任何运气在 PowerShell 中获得有关 $fso.GetFile 的更多信息。谷歌这次让我失望了。

任何人都可以帮忙吗?我更喜欢纯粹的 PowerShell 解决方案,但会感谢任何有效的解决方案。

最佳答案

您的问题是由文件名中的方括号引起的。当您将带有路径的字符串传递给(隐式)-Path Remove-Item 的参数,方括号被解释为 wildcard characters .一个子串 [9.00 PM]因此将匹配单个字符,即 M , P , 9 , 0 、一个点或一个空格。您可以使用 -LiteralPath而不是 -Path通过改变这个来避免这种行为:

... | % { Remove-Item $_.FullName -Force }

进入这个:
... | % { Remove-Item -LiteralPath $_.FullName -Force }

然而, Remove-Item接受管道输入,因此您可以完全删除循环并直接从管道读取项目:
... | Remove-Item -Force

那样 Remove-Item获取全 FileInfo 来自 Get-ChildItem 的对象并自动执行 The Right Thing™。

附带说明:您可以通过删除循环来进一步简化代码,因为不仅 Remove-Item从管道中读取,但 Get-ChildItem以及。像这样的事情应该可以正常工作:
$limit = (Get-Date).AddDays(-$purgeAfter)

Get-ChildItem $buildBase -Recurse -Include logs -Attributes D |
Get-ChildItem -Filter *.mclog |
Where-Object { $_.LastWriteTime -lt $limit -and -not $_.PSIsContainer } |
Remove-Item -Force

关于powershell - 删除带有特殊字符的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23042356/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com