在数组中使用子项结果对象 [英] Use child-item result object in array

查看:69
本文介绍了在数组中使用子项结果对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PowerShell在列出其子项时遇到问题.

I encountered a problem in PowerShell in listing its child items.

$file = Get-ChildItem \\compname\c$\folder\ -Recurse -Filter *filename.txt* |
        Select-Object -Property DirectoryName, FullName

当我尝试获取其对象时,它是空的:

When I try this to get its objects it was empty:

$file.FullName

$file.DirectoryName

如果该目录中有许多文件具有相同的文件名,那么如何通过在文件扩展名中添加.bak来备份同一文件夹中的那些文件.

If there is a many files in that directory with the same file name, how can I backup up those files in the same folder by by adding .bak on its file extension.

推荐答案

您仍在使用PowerShell v2或更早版本.这些早期版本不支持成员枚举在数组上,这将允许您通过数组对象本身访问数组元素的属性.相反,您得到的结果为空,因为数组对象没有属性DirectoryNameFullName.

You're still using PowerShell v2 or earlier. These early versions don't support member enumeration on arrays, which would allow you to access properties of array elements via the array object itself. Instead you get an empty result, because the array object does not have a property DirectoryName or FullName.

如果至少不能升级到PowerShell v3,则可以通过循环解决此问题:

If you can't upgrade to at least PowerShell v3 you can work around this issue with a loop:

$file | ForEach-Object { $_.FullName }

或通过扩展属性:

$file | Select-Object -Expand FullName

这篇关于在数组中使用子项结果对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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