Powershell help

Status
Not open for further replies.
Looking for some help with Powershell (which I'm totally new to). I'm trying to copy a number of files from one location to another using Copy-Item but can't get it to copy to the correct nested folders.

Roughly...

Code:
...
$files = git diff --name-only --diff-filter=ACMRTUXB 1843ac2 79f7aaa
$source = "D:\source"
$destination = "D:\destination"

for ($i=0; $i -lt $files.Length; $i++) {
   $files[$i] # just to show me the files
   Copy-Item $files[$i] $Destination
}
...

Each item of $Files is a filename and it's path relative to root, e.g.
directory1/file1.txt
directory1/file2.txt
directory2/file1.txt

etc.

Not sure if my loop method is the best but, it works... :)

The files are copying but being placed in the root of the destination folder rather than in appropriate sub folders (whether they exist or not)

Many thanks.
 
Bit of reading get me to...

Code:
...
foreach($file in $files){
   $file
   Copy-Item "$source\$file" "$Destination\$file"
}
...

instead. Works if destination folders already exists, fails if not. So need to loop through paths of file and do a New-Item on it first.

EDIT:

This works...

Code:
foreach($file in $files){
   $path = Split-Path $file -parent
   if(!(Test-Path -Path "$destination\$path")){
      New-Item -Path $destination -Name "$path" -ItemType "directory"
   }
   Copy-Item "$source\$file" "$Destination\$file"
}
:)
 
Status
Not open for further replies.