Novice here, so please excuse my simple question, however, in the way the code below is scripted to copy from one location to the other, it’s not so simple selecting the nested folder.
The code works completey fine, which is, it grabs the contents of the source directories and copies the files to the destination location as long as the same name as the source folder exists. My problem is, I want to have an Archive Folder within the destination and copy to this. I can’t seem to work out how to do this.
Also, please ignore the Write-Progress code, I have been trying to get this working, but can’t get an accurate display (percentage complete) as there isn’t an actual foreach loop doing the copying… ?
Any help is much appreciated.
Regards
Chris
# First, get a list of all target directories (i.e.: directories under $destination that have a match in $source)
$source = "c:\folder script\copy"
$destination = "c:\folder script\test"
$countFiles = dir -Path $source -Recurse
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | out-null;
$targetDirs = dir -Path $source -Recurse -Force |
?{ $_.psIsContainer } |
%{ $_.FullName -replace [regex]::Escape($source), $destination } |
%{ if (Test-Path $_) { $_ }}
$counter=1
# Then, enumerate all files from source and copy them only if the corresponding target dir exists
dir -Path $source -Recurse -Force |
?{ -not $_.psIsContainer } |
?{ Test-Path (Split-Path ($_.FullName -replace [regex]::Escape($source), $destination) -Parent)} |
copy -Force -Destination { $_.FullName -replace [regex]::Escape($source), $destination
$status = "Copying total of {1} Files" -f $counter,$countFiles.Count, $countFiles
Write-Progress -Activity "Copying data to DLAB" $status -PercentComplete ($counter / $countFiles.Count*100)
}
}
I’m unsure what you’re trying to do and how it is failing. The Copy-Item cmdlet has a -Recurse switch which will recursively copy a directory structure.
the code currently looks at a set of source folders, then checks to make sure in the target directory, there is a subfolder with the same name as all the source folders. Then it will copy ONLY files to the destination only if the destination and source folder name match.
Thing I want to do though is copy the files to a subfolder within the destination folder structure called ‘Archive’.
So, we have let’s say:
\source\Folder1
\source\Folder2
\source\Folder3
copy Files from Folder1, 2, 3 to Folder???\Archive when Folder1 and Folder??? etc match.
no - you had it originally Aaron, but I was trying something out, but still didn’t work. By adding it back yes, it does copy files, but the issue still remains as I posted earlier… The output is looking like this… Check the WhatIf statement…
Destination: C:\folder script\test\aabd1612\Archive(b)aabd1612(/b)" it’s adding the $dir.Name inside the archive folder which is not what I am trying to do.
the Archive should be the lowest level.
I’ve tried all sorts of things, but haven’t worked out how yet.
If the Archive directory exists when you run your script,Copy-Item assumes you want the source directory inside the destination directory. Look at your last WhatIf message:
[blockquote]
What if: Performing operation “Copy Directory” on Target “Item: C:\folder script\copy\test Destination: C:\folder script\test\test\Archive”.
[/blockquote]
It looks like that one was going to copy correctly, probaby because C:\folder script\test\test\Archive didn’t exist.
The only way to work around this is to check if the destination directory exists, and if it does, loop through all the contents of the source directory, and copy those to the destination:
Aaron your latest test-Path is returning the following error.
Test-Path : Cannot bind parameter because parameter ‘Path’ is specified more than once. To provide multiple values to parameters tha
t can accept multiple values, use the array syntax. For example, “-parameter value1,value2,value3”.
At C:\folder script\test_copy.ps1:12 char:44
Get-ChildItem is converting $dir from a DirectoryInfo to a string. It does that by calling $dir.ToString(), which returns just its name. Oops. Change it to