Author Posts

July 27, 2016 at 9:16 am

Hi, 
Can anyone be help on the below query:-

I have a requirement in Powershell to exclude certain files and folders while copying em into destination.

Source structure(Original):
-----------------------------------
Rootfolder>Subfolder1>Subfolder1_1>demo.txt
Rootfolder>Subfolder2
Rootfolder>Subfolder3
Rootfolder>sample.txt

Script:
--------
$ExcludeFolders = @("subfolder2")
Get-ChildItem -Path $FROM -Exclude $ExcludeFolders |
Copy-Item -Destination $TO -force -recurse

When the above script is run, the result is given below-

Destination structure (Not expected):
-----------------------------------------------
Rootfolder>Subfolder1>Subfolder1_1>demo.txt
Rootfolder>Subfolder3
Rootfolder>sample.txt
Rootfolder>demo.txt     

But, Destination structure (Expected) = Source structure(Original)

I am able to exclude the required folders and files while performing the copy-item. However, what is bothering me is , one of the files from 2nd level sub folder is being copied into the root folder too leading to the duplicate file in the destination file structure.

In my above sample, 'demo.txt' is the file that is residing in 'Subfolder1_1' which is in 'Subfolder1' and in turn this folder is in 'Root' folder.But when the script is run, I could see the demo.txt file being copied in 'Root' folder too. (Root>demo.txt). Which means, the file 'demo.txt' is existing in its original folder (SubFolder1_1) as well as in 'Root' folder.

How do I get rid of unwanted duplicate files as discussed in the above scenario?

July 27, 2016 at 12:49 pm

It might be easier if you separated your commands out to generate your folder list first with something like:

 #Populate list of folders
$folderlist = Get-ChildItem -Path $FROM -Exclude $ExcludeFolders -Recurse | Where {$_.PSIsContainer -eq $True}
ForEach ($folder in $folderlist)
{
   Get-ChildItem -Path $folder -Exclude $ExcludeFiles | Copy-Item -Destination $TO -force -recurse
}
  • This reply was modified 2 years ago by  Edmond Yee.

July 28, 2016 at 3:30 am

Hi Edmon,

The above script is giving me the below error.

I have modified the script according to my need. Please have a look-

$ExcludeFolders = @("DES", "Help","LoadBalancer")
$ExcludeFiles=@("web.config")
$folderlist = Get-ChildItem -Path "c:\temp\" -Exclude $ExcludeFolders -Recurse | Where {$_.PSIsContainer -eq $True}

ForEach ($folder in $folderlist)

{

Get-ChildItem -Path $folder -Exclude $ExcludeFiles | Copy-Item -Destination "d:\july28\" -force -recurse

}

The error i get is:-

Copy-Item : The filename, directory name, or volume label syntax is incorrect.
At line:3 char:63
+ Get-ChildItem -Path $folder -Exclude $ExcludeFiles | Copy-Item < <<< -Destination "d:\july28\" -force -recurse + CategoryInfo : NotSpecified: (:) [Copy-Item], IOException + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemCommand Copy-Item : The filename, directory name, or volume label syntax is incorrect. At line:3 char:63 + Get-ChildItem -Path $folder -Exclude $ExcludeFiles | Copy-Item <<<< -Destination "d:\july28\" -force -recurse + CategoryInfo : NotSpecified: (:) [Copy-Item], IOException + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemCommand

August 1, 2016 at 3:53 pm

I just copied and pasted your code, setup the following folders:

C:\temp\DES
C:\temp\Help
C:\temp\LoadBalancer
C:\temp\test

and in the test folder created the files test.txt and web.config.

Once I ran the script, it ran as expected without errors. Is the destination valid for you? Meaning does the July28 folder already exist on D:\ when you run the script or are you expecting it to create it for you as well? If so, you would use something like this:

$date = get-date -uformat %B%d
$destination = "E:\$date"
If ((Test-Path $destination) -eq $False)
{
    New-Item "$destination" -ItemType Directory
}

August 19, 2016 at 5:52 am

Yeah Edmond!

This is perfect. Thanks a ton! 🙂
Apologize for the late response. I was out of station.

  • This reply was modified 2 years ago by  lpk ..