Apologies in advance for likely confusion arising from my redefinition of the following terms for this question;
- directory (the top level directory)
- subdirectory (a directory below the top level directory)
- sub_subdirectory (a subdirectory of the subdirectory)
- contents of directory (files and subdirectories)
- contents of subdirectory (files and sub_subdirectories)
Question
I'm trying to use Windows 7 Powershell 3.0 Copy-Item
function to copy the six subdirectories of one directory, into another directory; fairly simple stuff. Note, these directories reside on a network, the UNC paths of which have been mapped to drives in windows environment as in;
H:\ = \\servername\path
In the following example, the top level directory is itself copied, rather than just its contents (which happen to be six subdirectories and their contents)
$from = "H:\temp\toplvl_dir - backup"
$to = "H:\temp\newdir"
Copy-Item $from -Destination $to -Recurse
# Creates:
# H:\temp\newdir\toplvl_dir - backup
This is undesirable.
Progress was made in borrowing the format of the David-Tchepak solution using " * " postfix $from = "H:\temp\toplvl_dir\* "
which partly worked in that it did copy only contents...
$from = "H:\temp\toplvl_dir - backup\*"
$to = "H:\temp\newdir"
Copy-Item $from -Destination $to -Recurse
# Creates:
# H:\temp\newdir\{copied contents of .\toplvl_dir - backup\NN_subdir\}
...but unfortunately those contents originated from one level below the desired level: i.e. the contents of the subdirectory .\toplvl_dir - backup\NN_subdir\
, rather than the contents of the directory .\toplvl_dir...
The $from
directory in the example above in fact contains six subdirectories "\01_subdir", "\02_subdir"...."\06_subdir"
, each of which contains an identical 'tree' consisting a lone subdirectory which itself contains lots of files. The full path looks like this;
e.g. H:\temp\toplvl_dir - backup\NN_subdir\lone_subdirectory\{lots of files}
Copy-Item
seemingly tries to copy the sub_subdirectory i.e. lone_subdirectory
, reporting an error for each NN_subdir
; "container cannot be copied onto existing leaf item". I understand in so far that each copy iteration of the contents of NN_subdir
beyond the first 01_subdir
, is a repetition of lone_subdirectory
which will give rise to a directory name conflict, but I do not understand why with the syntax applied, Copy-Item
is trying to copy the sub_subdirectories rather than the subdirectory .\toplvl_dir - backup\NN_subdir\
How to make a simple call to Copy-Item
to copy the contents of the top level directory .\toplvl_dir - backup\*
into .\newdir
i.e. the six subdirectories themselves and their contents!?
Please feel free to say if I am being unreasonable in expecting to be able to complete this operation in a single line call to Copy-Item
?
# Desired Result using a one-line call to Copy-Item:
H:\temp\newdir\01_subdir\lone_subdirectory\{lots of files}
H:\temp\newdir\02_subdir\lone_subdirectory\{lots of files}
H:\temp\newdir\03_subdir\lone_subdirectory\{lots of files}
H:\temp\newdir\04_subdir\lone_subdirectory\{lots of files}
H:\temp\newdir\05_subdir\lone_subdirectory\{lots of files}
H:\temp\newdir\06_subdir\lone_subdirectory\{lots of files}
:-S