Windows Powershell copying

11 views
Skip to first unread message

Jeff Hutchison

unread,
Feb 23, 2018, 5:10:06 PM2/23/18
to txrxlabs
Anybody here good with Powershell?

I'm trying to copy purchase orders from one folder to another on the network.  Every purchase order is located in a folder with the project number on it, but not all files have the project number on them.

I can copy the folders easily and re-running the code updates the destination with any changes

$Source = "K:\ConServ\Purchasing\6. POs\ECPs\*3Z341*"
$Destination = "K:\Projects\3Z341 Hilcorp\07.0 Purchasing\7.1 Purchase Orders\00 Powershell" 
Copy-Item $Source -Destination $Destination -Recurse -Verbose  

I would like to exclude any files that are already in the destination to avoid creating too much unnecessarily work.  I thought this should be easy - just look at the destination and add the files to the excluded list:

$ExcludeItems = gci -Recurse $Destination

Now we lose the ability to update the folders.... because if the folder exists, then it gets excluded.  So I add the -file parameter to the gci command and it doesn't list folders.

ExcludeItems = gci -Recurse $Destination -File

^ but remember, we're copying folders, not files... hahaha... so every file still gets copied over.

I can't seem to bridge the gap between needing to copy a folder and wanting to exclude files (instead of folders).

If somebody can do this with robocopy, that's fine - keep in mind that the files themselves have no identification, only the folders they are in, and I need ALL the files from every identified folder.  My attempts with robocopy have been met with speedy but incomplete results. I'm thinking maybe we can pipe the  $sourceFileList to robocopy with some switch somewhere to preserve folder structure? Then an /XO switch that keeps it from overwriting?



My copy-item script is below:  


#all subdirectories with the project number in them
$Source = "K:\ConServ\Purchasing\6. POs\ECPs\*3Z341*"

#all of the files located in source directories
#$sourceFileList = gci $Source -recurse

#project folder destination for the directories to be copied to (with every file regardless of name)
$Destination = "K:\Projects\3Z341 Hilcorp\07.0 Purchasing\7.1 Purchase Orders\00 Powershell"

#Do not update folders already found at destination
#$ExcludeItems = gci -Recurse $Destination

#Gives a list of files at the destination to be excluded
$ExcludeItems = gci -Recurse $Destination -File

#copies the FOLDERS from source (and all their contents) unless the FOLDER is on the excluded list
#Copy-Item $Source -Destination "$Destination" -Exclude $ExcludeItems -Recurse -Verbose

#copies the FILES from source - but doesn't preserve the directory structure
#Copy-Item $sourceFileList -Destination "$Destination" -Exclude $ExcludeItems -Recurse -Verbose

Jeff Hutchison

unread,
Feb 23, 2018, 9:17:42 PM2/23/18
to txrxlabs
On the drive home I had it 90% worked out... the real challenge was getting the directories recreated in the destination folder. The code below worked perfectly on my home network:

$src = gci 'M:\Photos\*2003*\'
$des = 'W:\Test\'

Foreach ($i in $src)
{
$srcfolder = split-path $i -Leaf
$desfolder = Join-Path $des $srcfolder
md $desfolder -ErrorAction SilentlyContinue

robocopy $i $desfolder /s /xo
}

Reply all
Reply to author
Forward
0 new messages