[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
本帖最后由 wrove 于 2019-1-26 00:37 编辑
  1. function Copy-ItemWithKeepTheDirectoryStructure {
  2.     <#
  3.     .EXAMPLE
  4.     Copy-ItemWithKeepTheDirectoryStructure -FromFolder D:\ `
  5.         -ToFolder test -Extensions txt, xml `
  6.         -SearchSubFolders bin, data
  7.     .EXAMPLE
  8.     Copy-ItemWithKeepTheDirectoryStructure -FromFolder e:\ `
  9.         -ToFolder test -Extensions txt, doc
  10.     #>
  11.     param(
  12.         [Parameter(Mandatory = $true,
  13.                    Position = 1)]
  14.         [ValidateScript({ Test-Path $_ })]
  15.         [string]$FromFolder,
  16.         [Parameter(Mandatory = $true,
  17.                    Position = 2)]
  18.         [string]$ToFolder,
  19.         [Parameter(Mandatory = $true)]
  20.         [string[]]$Extensions,
  21.         [Parameter(Mandatory = $false)]
  22.         [string[]]$SearchSubFolders
  23.     )
  24.     #0.检查并标准化参数
  25.     if(!(Test-Path $ToFolder)) {
  26.         New-Item -ItemType Directory -Path $ToFolder | Out-Null
  27.     }
  28.     $ToFolder = (Resolve-Path $ToFolder).Path
  29.     $FromFolder = (Resolve-Path $FromFolder).Path
  30.     if($FromFolder -eq $ToFolder) {
  31.         throw [System.ArgumentException]::new(
  32.             'FromFolder与ToFolder必须指向不同的目录')
  33.     }
  34.     $Extensions = $Extensions |
  35.         ForEach-Object {
  36.             if($_.StartsWith('*.')) {
  37.                 $_
  38.             } else {
  39.                 '*.' + $_
  40.             }
  41.         }
  42.     #1.切换目录
  43.     Push-Location
  44.     Set-Location $FromFolder
  45.     #2.开始处理
  46.     if($SearchSubFolders -eq $null) {
  47.         Get-ChildItem -Recurse -File -Include $Extensions |
  48.             ForEach-Object {
  49.                 $sPath = Resolve-Path $_.FullName -Relative
  50.                 $lPath = Split-Path $sPath
  51.                 $rPath = Split-Path $sPath -Leaf
  52.                 $dPath = $ToFolder + $sPath.Substring(1)
  53.                 $dFolder = $ToFolder + $lPath.Substring(1)
  54.                 if(!(Test-Path $dFolder)) {
  55.                     New-Item -ItemType Directory -Path $dFolder | Out-Null
  56.                 }
  57.                 Copy-Item -LiteralPath $sPath -Destination $dPath | Out-Null
  58.             }
  59.     } else {
  60.         Get-ChildItem -Recurse -Directory -Include $SearchSubFolders |
  61.             ForEach-Object {
  62.                 Get-ChildItem -Path ($_.FullName + '\*') -File `
  63.                     -Include $Extensions
  64.             } | ForEach-Object {
  65.                 $sPath = Resolve-Path $_.FullName -Relative
  66.                 $lPath = Split-Path $sPath
  67.                 $rPath = Split-Path $sPath -Leaf
  68.                 $dPath = $ToFolder + $sPath.Substring(1)
  69.                 $dFolder = $ToFolder + $lPath.Substring(1)
  70.                 if(!(Test-Path $dFolder)) {
  71.                     New-Item -ItemType Directory -Path $dFolder | Out-Null
  72.                 }
  73.                 Copy-Item -LiteralPath $sPath -Destination $dPath | Out-Null
  74.             }
  75.     }
  76.     #3.切换回原有的当前目录
  77.     Pop-Location
  78. }
复制代码

TOP

返回列表