1

Assume, you have next structure inside current folder

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

If than you execute next command inside this folder:

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

files a.txt and b.txt will be copied, but without parent folders f1 and f2.

But, if you add any file into f0 folder

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

The same powershell command will fork as expected

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

Am I missing something?

Thanks.

Update

Initial task is to deploy cmake in automatic way. This is source script:

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'"
  • What is the goal you want to achieve? – papanito Sep 28 '19 at 19:59
  • Possible duplicate of [Rename all Items in a directory](https://stackoverflow.com/questions/45828188/rename-all-items-in-a-directory), [How to prevent PowerShell -Recurse from renaming first file twice?](https://stackoverflow.com/q/57795862/1701026) and [Powershell, rename-item doesn't work as expected](https://stackoverflow.com/q/42470793/1701026), use brackets: `powershell -Command "(Get-ChildItem -Path 'f0\*' -Recurse) | Move-Item -Destination '.'"` – iRon Sep 28 '19 at 22:22
  • @iRon thanks for the suggestion, but brackets don't solve the issue. Even more, when fake.txt presents, powershell script output error `Move-Item : Cannot move item because the item at 'Microsoft.PowerShell.Core\FileSystem::D:\projects\cmake\test\f0\f1\a.txt' does not exist.` – Vladyslav Kurmaz Sep 29 '19 at 06:54
  • @papanito I've updated question, with more details – Vladyslav Kurmaz Sep 29 '19 at 06:59
  • 1
    Instead of adding the wildcard to the `-Path`, I guess you need to use the [`-Filter`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-6) parameter for this: `Get-ChildItem -path f0 -filter * -recurse | Move-Item -Destination .` – iRon Sep 29 '19 at 07:44
  • @iRon thanks, you suggestion fixes the problem – Vladyslav Kurmaz Sep 29 '19 at 13:45
  • 1
    Please do not edit an answer into your question. You're free to post an answer of your own. – Ansgar Wiechers Sep 30 '19 at 07:52

1 Answers1

0

Solution is based on comments.

You have to use -Filter option instead of using wildcard inside -Path option

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