Should Copy-Item create the destination directory structure?

后端 未结 6 1561
广开言路
广开言路 2020-12-08 12:46

I\'m trying to copy a file to a new location, maintaining directory structure.

$source = \"c:\\some\\path\\to\\a\\file.txt\"
destination = \"c:\\a\\more\\dif         


        
6条回答
  •  有刺的猬
    2020-12-08 13:26

    Here's a oneliner to do this. Split-Path retrieves the parent folder, New-Item creates it and then Copy-Item copies the file. Please note that the destination file will have the same filename as the source file. Also, this won't work if you need to copy multiple files to the same folder as with the second file you'll get An item with the specified name already exists error.

    Copy-Item $source -Destination (New-Item -Path (Split-Path -Path $destination) -Type Directory)
    

提交回复
热议问题