gpt4 book ai didi

powershell - 在 powershell $x.FullName 不返回完整路径

转载 作者:行者123 更新时间:2023-12-04 17:57:38 35 4
gpt4 key购买 nike

我在下面有一个 powershell 脚本,它需要一个配置文件并删除与正则表达式匹配的 x 天之前的文件。

配置文件:

path,pattern,days,testrun
C:\logs\,^data_access_listener.log,7,false

但是,这是输出:
Would have deleted 000a19f6-a982-4f77-88be-ca9cc51a2bcbuu_data_access_listener.log
Would have deleted 00189746-2d46-4cdd-a5bb-6fed4bee25a7uu_data_access_listener.log

我希望输出包含完整的文件路径,因为我使用的是 .FullName 属性,所以我希望输出如下:
Would have deleted C:\logs\000a19f6-a982-4f77-88be-ca9cc51a2bcbuu_data_access_listener.log
Would have deleted C:\logs\00189746-2d46-4cdd-a5bb-6fed4bee25a7uu_data_access_listener.log

如果我使用 $x.FullName 为什么我没有得到路径的全名 (C:\logs)?

谢谢
布拉德
$LogFile = "C:\deletefiles.log"
$Config = import-csv -path C:\config.txt

function DeleteFiles ([string]$path, [string]$pattern, [int]$days, [string]$testrun){
$a = Get-ChildItem $path -recurse | where-object {$_.Name -notmatch $pattern}
foreach($x in $a) {
$y = ((Get-Date) - $x.LastWriteTime).Days
if ($y -gt $days -and $x.PsISContainer -ne $True) {

if ($testrun -eq "false") {
write-output “Deleted" $x.FullName >>$LogFile
} else {
write-output “Would have deleted $x” >>$LogFile
}


}
}
}

foreach ($line in $Config) {
$path = $line.path
$pattern = $line.pattern
$days = $line.days
$testrun = $line.testrun

DeleteFiles $path $pattern $days
}

最佳答案

看起来像一个错字。在导致“Would have Deleted”的情况下,您没有使用 FullName。改变:

write-output “Would have deleted $x” >>$LogFile

到:
write-output “Would have deleted " $x.FullName >>$LogFile

要回答您的评论,如果您想调用在字符串中展开的变量的属性,则必须将其括在 $() 中。 ,以便解析器知道调用整个表达式。像这样:
write-output “Would have deleted $($x.FullName)" >>$LogFile

关于powershell - 在 powershell $x.FullName 不返回完整路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5356345/

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