Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

powershell - unable to exclude folders during recursive copy

Tags:

powershell

I am new to powershell and am running into a problem while trying to exclude certain directories during recursive copy. Any help is appreciated! Thanks in advance.

$Date = Get-Date
$Date = $Date.adddays(-1)

$destPath = "\\destination\test"
$srcPath = "H:\program files\symphony\Save"
$srcPathRemits = “H:\program files\symphony\files"
$destDrive = "X:"
$User = "user"
$Password = "password"

$exclude = @('H:\program files\symphony\files\Temp\*','H:\program files\symphony\files\Other\*','H:\program files\symphony\files\etc\*','H:\program files\symphony\files\ParsedXML\*')

$net = new-object -ComObject WScript.Network
$net.MapNetworkDrive($destDrive, $destPath, $false, $User, $Password)

gci -recurse -path $srcPathRemits -Exclude $exclude | ? {!($_.psiscontainer) -AND $_.lastwritetime -gt $Date} | %  { write-host $_.fullname; Copy-Item -path $_.fullname -destination $destDrive} 
$net.RemoveNetworkDrive($destDrive,"true","true")
like image 658
Gbgk Avatar asked Nov 13 '12 00:11

Gbgk


People also ask

How do I exclude a subfolder in PowerShell?

To exclude directories, use the File parameter and omit the Directory parameter, or use the Attributes parameter. To get directories, use the Directory parameter, its "ad" alias, or the Directory attribute of the Attributes parameter.

How do I get a list of files in a directory and subfolders using PowerShell?

Using PowerShell Get-ChildItem cmdlet and PSIsContainer to list files in the directory or list all files in the directory and subdirectories.


1 Answers

You didn't say what the problem was, but I'll assume that the directories ($exclude) were not properly excluded. Try this instead, for the gci line:

Get-Item -Path H:\program files\symphony\files\* -Exclude Temp, Other, etc, ParsedXML | Get-ChildItem -recurse | ? {!($_.psiscontainer) -AND $_.lastwritetime -gt $Date} | %  { write-host $_.fullname; Copy-Item -path $_.fullname -destination $destDrive}
like image 114
David Avatar answered Sep 27 '22 20:09

David