4

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

1 Answers1

3

Just to clarify, If I have a folder structure like this:

C:\src\01\lone\a.txt
C:\src\02\lone\b.txt
C:\dest

You are looking for a 1-line Copy-Item command to produce a directory structure that looks like this?

C:\dest\01\lone\a.txt
C:\dest\02\lone\b.txt

Is that correct? If so, this command worked for me (albeit Win7, PS version 5):

Copy-Item -Path 'C:\src\*' -Destination 'C:\dest' -Recurse
Caleb Seelhoff
  • 261
  • 1
  • 8
  • Yes, thanks you have interpreted what I'm trying to do perfectly. – smashingMyPC36timesaday Jul 03 '17 at 10:15
  • So I created a mirror of your structure on my local `C:\ ` The next part is confusing; I created a ps1 script with syntax as suggested, in `H:\ ` which is a network drive (mandatory practice), and a preliminary `del .\dest` cmd to erase prior attempts. Running the script from `H:\do_copy.ps1`, generated the familiar leaf error. **Then**, running a ps1 script locally, `C:\do_copy.ps1` *was* successful; *once*! But after re-running `H:\do_copy.ps1`, unsuccessfully, *subsequent running of the local script `C:\do_copy.ps1` is **also** unsuccessful...* – smashingMyPC36timesaday Jul 03 '17 at 10:36
  • `#do_copy.ps1 if (Test-Path 'C:\dest') { Remove-Item 'C:\dest' - Recurse } Copy-Item -Path 'C:\src\*' -Destination 'C:\dest' -Recurse ` – smashingMyPC36timesaday Jul 03 '17 at 12:52
  • 1
    It looks like you are deleting your destination directory and its contents. Maybe try to modify your script with this added in: #do_copy.ps1 if (Test-Path 'C:\dest') { Remove-Item 'C:\dest' - Recurse } New-Item 'C:\dest' Copy-Item -Path 'C:\src\\*' -Destination 'C:\dest' -Recurse – Caleb Seelhoff Jul 03 '17 at 14:29
  • 1
    Thank you Caleb! I feel stupid, and I love you. This worked for me; `New-Item 'C:\dest' -ItemType 'directory'` – smashingMyPC36timesaday Jul 03 '17 at 15:09