xcopy - PowerShell to copy files to destination's subfolders while excluding certain folders in the destination -


so have danced off , on throughout day , timeless phrase "there's more 1 way skin cat" keeps coming mind decided take community.

scenario:

source folder "c:\updates" has 100 files of various extensions. need copied sub-folders only of "c:\prod\" overwriting duplicates may find.

the caveats:

  1. the sub-folder names (destinations) in "c:\prod" quite dynamic , change frequently.

  2. a naming convention used determine sub-folders in destination need excluded when source files being copied (to retain original versions). ease of explanation lets folder names starting "!stop" should excluded copy process. (!stop* if wildcards considered)

so, here wanting input of greater tackle in ps if i'm lucky. i've tinkered copy-item , xcopy today i'm excited hear other's input.

thanks!

-chris

give shot:

get-childitem -path c:\prod -exclude !stop* -directory ` | foreach-object { copy-item -path c:\updates\* -destination $_ -force } 

this grabs each folder (the -directory switch ensures grab folders) in c:\prod not match filter , pipes foreach-object command running copy-item command copy files directory.

the -directory switch not available in every version of powershell; not know version introduced in off top of head. if have older version of powershell not support -directory can use script:

get-childitem -path c:\prod -exclude !stop* ` | where-object { $_.psiscontainer } ` | foreach-object { copy-item -path c:\updates\* -destination $_ -force } 

Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -