首页 > 解决方案 > “Get-ChildItem | Move-Item”链跳过顶级文件夹

问题描述

假设,您在当前文件夹中有下一个结构

f0
|-f1
| |-a.txt
|-f2
  |-b.txt

如果您在此文件夹中执行下一个命令:

powershell -Command "Get-ChildItem -Path 'f0\*' -Recurse | Move-Item -Destination '.'"

文件a.txtb.txt将被复制,但没有父文件夹f1f2

但是,如果您将任何文件添加到f0文件夹中

f0
|-f1
| |-a.txt
|-f2
| |-b.txt
|-fake.txt

相同的 powershell 命令将按预期分叉

f0
f1
|-a.txt
f2
|-b.txt
fake.txt

我错过了什么吗?

谢谢。

更新

初始任务是以自动方式部署 cmake。这是源脚本:

echo Downloading https://github.com/Kitware/CMake/releases/download/v3.15.3/cmake-3.15.3-win64-x64.zip
powershell -Command "(New-Object System.Net.WebClient).DownloadFile('https://github.com/Kitware/CMake/releases/download/v3.15.3/cmake-3.15.3-win64-x64.zip', 'cmake-3.15.3-win64-x64.zip')"
powershell -Command "Expand-Archive -LiteralPath cmake-3.15.3-win64-x64.zip -DestinationPath ."
powershell -Command "Get-ChildItem -Path 'cmake-3.15.3-win64-x64\*' -Recurse | Move-Item -Destination '.'"
powershell -Command "Remove-Item 'cmake-3.15.3-win64-x64'"
powershell -Command "Remove-Item 'cmake-3.15.3-win64-x64.zip'"

标签: powershell

解决方案


解决方案基于评论。

您必须使用 -Filter 选项而不是在 -Path 选项中使用通配符

powershell -Command "Get-ChildItem -Path 'f0' -Filter '*' -Recurse | Move-Item -Destination '.'"

推荐阅读