Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Copy-item including folder structure and not overwrite

$
0
0

Hi,

I have a script that does a GCI on 4 folders, it then filters out specific file that do not need copying, it then creates the top level folder in the destination folder then copies the file and the -container folder.

The problem I am having is that for each folder in the source if there is folder withing with files or folders in it doesn't get copied.  I've tried -recurse and also -contains for copy-item.

Any ideas?

#root of archive
$root = 'f:\Archive'
#Root plus time stamp folder
$archive = "f:\Archive\Archive_$(Get-Date -f dd_MM_yyyy)"
#Make Destination Datestamp Folder
New-Item -Path $archive -ItemType Directory
#find latest folder
$todayFold = (Get-ChildItem -Path $root | Where-Object {$_.PSIsContainer} | Sort-Object LastWriteTime -Descending | Select-Object -First 1)
$todayFold = $todayFold.fullname
#arrays for path and exclude
$filepath = $Desktop, $MyDocs, $Downloads, $MyRecordings

#copy logic

Foreach ($folder in $filepath){
$folderEnd = split-path $folder -leaf
$fullfinalpath = join-path $todayFold -childpath $folderEnd
CD $folder
New-Item -ItemType directory -Path $fullfinalpath
$Files = GCI -path $folder -recurse | Where-object {$_.name -ne 'cleanup' -and $_.name -ne 'TurningPoint 5' -and $_.extension -ne '.ps1' -and $_.name -ne 'CountDownTimer' -and $_.name -ne 'Templates' -and $_.name -ne 'Cyberlink' -and $_.name -ne 'My Recordings' -and $_.name -ne 'TurningPoint' -and $_.extension -ne '.Lnk'}

#I think this might be the cause

foreach ($file in $files){copy-item -path $file.fullname -destination $fullfinalpath -container} #; remove-item -recurse $file.fullname }

}

#set location for zip (by CD)
set-location $root
#Pass todays archive folder to zip function
create-7zip $todayfold $securepassword
#remove the folder after zipping
#remove-item -recurse $todayfold -ErrorAction SilentlyContinue -Confirm:$false
invoke-item $root


Alter De Ruine



Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>