简体   繁体   English

Powershell复制所有文件夹和文件夹中的文件

[英]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. 我想将所有文件夹和文件从此路径c:\\ default \\复制到此目标c:\\ environment \\ customfolder \\,在customerfolder文件夹中还有其他文件夹,它们的名称不同。 It should only copy the files and folders to the destination Where the customfolder contains the name DEMO_test 它只应将文件和文件夹复制到目标文件夹中自定义文件夹包含名称DEMO_test的位置

What is the best and easiest way to do that? 最好和最简单的方法是什么? should I use for-each? 我应该使用for-each吗?

thanks for your help. 谢谢你的帮助。

Sorry I should be more clear. 抱歉,我应该更清楚。 ;-) ;-)

I have a folder c:\\default 我有一个文件夹c:\\ default

All the files and sub-folders in that folder c:\\default 该文件夹中的所有文件和子文件夹c:\\ default

should be copied to these folders 应该复制到这些文件夹

c:\\environment\\customfolder\\demo_test c:\\ environment \\ customfolder \\ demo_test

c:\\environment\\customfolder\\demo_test01 c:\\ environment \\ customfolder \\ demo_test01

c:\\environment\\customfolder\\demo_test02 c:\\ environment \\ customfolder \\ demo_test02

c:\\environment\\customfolder\\demo_test03 c:\\ environment \\ customfolder \\ demo_test03

I know it should be possible to copy all files and sub-folders from this path (source)c:\\default\\ 我知道应该可以从此路径(源)复制所有文件和子文件夹c:\\ default \\

to this path (destination)c:\\environment\\customfolder\\ 到此路径(目标)c:\\ environment \\ customfolder \\

And only copy it to the folders if they have the name (like) demo_test* 并且仅将其复制到具有demo_test *的名称的文件夹中

Is that question better? 这个问题更好吗?

thanks. 谢谢。

Get a list of files: 获取文件列表:

Get-ChildItem -Path "C:\default\" -Recurse

The -Recurse parameter searches subfolders. -Recurse参数搜索子文件夹。

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 如果我从根本上理解您,则目录的结构扁平:源: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 您可以使用开关copy-TestdemoFolders -source 'C:\\Source' -filter "*demo_test*" -destination D:\\TEST -recursive:$true将文件从子文件夹递归复制到[lookupCatalog]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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