powershell - “Get-ChildItem | Move-Item”链跳过顶级文件夹
问题描述
假设,您在当前文件夹中有下一个结构
f0
|-f1
| |-a.txt
|-f2
|-b.txt
如果您在此文件夹中执行下一个命令:
powershell -Command "Get-ChildItem -Path 'f0\*' -Recurse | Move-Item -Destination '.'"
文件a.txt和b.txt将被复制,但没有父文件夹f1和f2。
但是,如果您将任何文件添加到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'"
解决方案
解决方案基于评论。
您必须使用 -Filter 选项而不是在 -Path 选项中使用通配符
powershell -Command "Get-ChildItem -Path 'f0' -Filter '*' -Recurse | Move-Item -Destination '.'"
推荐阅读
- c# - 基于子类更改对象中属性的实际值
- c# - 如何在评分的帮助下将此代码列出前 20 部电影?
- javascript - node.js 中的并发命令和反应应用程序中的错误?
- java - 为什么 Hibernate/JPA 在查询中报告空指针异常?
- c# - 如何在 Visual Studio (C#) 中实例化 ISymbolScope 以访问其方法
- angular - Ngclass 没有更新 UI 中的类
- azure - 云'AzureCloud'中不存在xxx'的订阅
- reactjs - 如何使用 React 中的按钮传递 ID?
- gcc - GCC - 手动切换每个指令集而不使用 March 一次切换多个指令集?
- python - 在远程节点执行 bash 脚本并从服务器通过 python 控制