Reputation: 39
Probably very easy for you guys.
I want to copy all the folders and files from this path c:\default\ to this destination c:\environment\customfolder\ in the folder customerfolder are other folders with different names. It should only copy the files and folders to the destination Where the customfolder contains the name DEMO_test
What is the best and easiest way to do that? should I use for-each?
Sorry I should be more clear. ;-)
I have a folder c:\default
All the files and sub-folders in that folder c:\default
should be copied to these folders
c:\environment\customfolder\demo_test
c:\environment\customfolder\demo_test01
c:\environment\customfolder\demo_test02
c:\environment\customfolder\demo_test03
I know it should be possible to copy all files and sub-folders from this path (source)c:\default\
to this path (destination)c:\environment\customfolder\
And only copy it to the folders if they have the name (like) demo_test*
Is that question better?
thanks.
Upvotes: 1
Views: 15609
Reputation: 111
if i understood you corectly you have flat structure of catalogs: SOURCE: C\Catalog[lookupCatalogs]\files DEST: c:\Catalog\SomeCatlogs[lookupCatalogs]\files
if yes,
this function should be ok:
function copy-TestdemoFolders
{
param ($source,
$destination,
$filter,
$recursive = $false)
$folders = Get-ChildItem $source -filter $filter -Directory
$folders | % {
$copyPath = Join-Path $destination $_.name
if (!(Test-Path $copyPath))
{
New-Item $copyPath -ItemType Directory | Out-Null
"Created New Folder: $($_.name)"
}
$scriptBlock = { Get-ChildItem $_.Fullname }
if ($recursive -eq $true)
{
$scriptBlock = { Get-ChildItem $_.Fullname -Recurse }
}
Invoke-Command -ScriptBlock $scriptBlock | %{
Copy-Item $_.Fullname $copyPath -ErrorAction SilentlyContinue
if (!($?)) { $error[0].Exception.Message }
}
}
}
copy-TestdemoFolders -source 'C:\Source' -filter "*demo_test*" -destination D:\TEST
You can recursively copy files from subfolders to [lookupCatalog] with switch copy-TestdemoFolders -source 'C:\Source' -filter "*demo_test*" -destination D:\TEST -recursive:$true
Upvotes: 0
Reputation: 17171
Get a list of files:
Get-ChildItem -Path "C:\default\" -Recurse
The -Recurse
parameter searches subfolders.
Now filter the list to show only files that fit a certain pattern
Get-ChildItem -Path "C:\default\" -Recurse |
Where-Object Name -like "*test*"
Note that the pipe |
is effectively chaining these commands together.
Now copy the filtered list of items to a destination folder:
Get-ChildItem -Path "C:\default\" -Recurse |
Where-Object Name -like "*test*" |
Copy-Item -Destination "C:\destination\"
Upvotes: 4