2
votes

I am trying to copy a large number of files from a Windows system to a Unix share using PowerShell. I seem to be able to get so far, but not the whole solution.

I seem to be able to copy the files across as long as I don't mind where they are stored as it loses all folder structure. I have tried various methods posted here and elsewhere online to no avail.

Code:

gci -path "e:\company folders\\*" | Where-Object {$_.LastAccessTime -le (Get-Date).addyears(-2)} | Copy-Item -destination "\\\networkfolder\archive\company folders\"

I have tried several variations of this script, including using -recurse after the -path and after the -destination

The most success came from

gci -path "e:\company folders\*" | Where-Object {$_.lastaccesstime -le (Get-Date).addyears(-2)} | Copy-Item -destination "\\\networkshare\archive\company folder\" -recurse -container

But this only copied five out of the 43 folders..

What am I doing wrong?

2
If you start a process which takes long or transfers a lot of files or deletes a lot of files, i suggest you to use the -whatif operation. This shows you what would happen if you go for your command. example : Get-Childitem C:\SomeFile*.txt -Recurse | Remove-Item -WhatIf ..... After you saw what the script would do, you know if your script is wrong or it stops simply because it's too much load for it.RayofCommand
Pretty much a duplicate of stackoverflow.com/questions/11694961/…SpellingD

2 Answers

4
votes

Use robocopy (Windows equivalent of rsync) if you want to replicate a folder structure:

$src = ':\company folders'
$dst = '\\networkfolder\archive\company folders'
$age = (Get-Date).AddYears(-2).ToString('yyyyMMdd')

& robocopy "$src" "$dst" /s /maxlad:$age

Replace /s with /e if you want to include empty subfolders as well.

0
votes

The following script copies the files and keeps the folder structure (just in case), uses a specific filter.

$sourceDir = 'c:\temp'
$targetDir = 'c:\backup'

#Get the files based on a filter and copy them including folder structure
dir -Path $sourceDir -Filter *interop*.* -Recurse | foreach {
    $targetFile = $targetDir + $_.FullName.Substring($sourceDir.Length);
    New-Item -ItemType File -Path $targetFile -Force;
    Copy-Item $_.FullName -Destination $targetDir;
} -Verbose

Based on this original response