简体   繁体   中英

PowerShell Copy-Item not creating subfolders

I want Copy-Item to copy to the destination file and create any subfolders on the way, but I can't seem to get that to work.

Copy-Item $fullsrc $fulldst -Recurse -Force -Verbose

$fullsrc and $fulldst are full paths with filenames as the destination filename is different from the source filename. Is there a way to get Copy-Item to create the subfolders and then copy the file over?

VERBOSE: Performing the operation "Copy File" on target "Item: D:\mypath\logs\001.123.log
Destination: D:\newpath\newlogs\123.234.log".
Copy-Item : Could not find a part of the path 'D:\newpath\newlogs\123.234.log'.

Copy-item have't function to create a folder, you need to previously create it

 Copy-Item $fullsrc $(new-item -ItemType Directory -Path $fulldst) -Recurse -Force -Verbose -ErrorAction SilentlyContinue

You have to create the parent directory of the destination file on your own.

# Split-Path with single parameter outputs the parent directory
$null = New-Item (Split-Path $fulldst) -ItemType Directory

Copy-Item $fullsrc $fulldst -Force -Verbose

Note that -Recurse switch has no use when you specify full source and destination file paths, so I've removed it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM