gpt4 book ai didi

powershell - 带有通配符的奇怪Powershell GCI递归结果

转载 作者:行者123 更新时间:2023-12-03 00:10:27 25 4
gpt4 key购买 nike

我正在尝试将GCI与Recurse参数一起使用,以获取指定路径的子文件夹内的所有文件的列表。

我正在使用以下行:

gci 'C:\temp\TestRecurse\*\*' -Recurse

在TestRecurse文件夹下,我具有以下内容:
TestRecurse
|---b.txt
|---dir1
|------a.txt

我希望返回a.txt。但是,我得到a.txt和b.txt。如果我将a.txt放入另一个子文件夹,则对我仍然陌生:
TestRecurse
|---b.txt
|---dir1
|------dir2
|---------a.txt

上面的同一条语句仅返回a.txt。我对返回b.txt时如何弄乱a.txt的位置感到困惑。有人可以向我解释为什么会发生这种情况,以及为什么根本不会返回b.txt吗?

更新

我应该提到,虽然他们很感激,但我不一定要寻找解决方法。这是我们环境中较大脚本的一部分,该脚本负责在保持灵活性的同时以各种方式移动文件。它的行为不像我预期的那样,所以我试图了解为什么它按原样工作。正如PetSerAl所指出的那样,理解Get-ChildItem可能比它值得的麻烦更多。

谢谢!

最佳答案

您要为父目录(TestRecurse\*)添加一个通配符,因此也要获取其中包含的文件。尝试获取TestRecurse的文件夹列表,然后对其进行遍历。

结构体:

TestRecurse\b.txt
TestRecurse\dir1
TestRecurse\dir1\a.txt

码:
Get-ChildItem 'C:\tmp\TestRecurse\' | `    # Get the list of items within TestRecurse
? {$_.PSIsContainer} | ` # Filter items that are folders
% {Get-ChildItem $_.FullName -Recurse} # Iterate through and get all items within those folders

这仅返回dir1内的文件夹和文件,而不返回dir1本身。

关于powershell - 带有通配符的奇怪Powershell GCI递归结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50260456/

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