简体   繁体   中英

powershell copy all folders and files in folder

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?

thanks for your help.

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.

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\"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM