Powershell - 将文件复制到另一个文件夹,同时保持文件夹结构

Powershell - 将文件复制到另一个文件夹,同时保持文件夹结构

我有一个包含许多子目录和文件的大文件夹。我只想将特定文件连同文件夹结构一起从该大文件夹复制到新文件夹。我正在使用 Powershell 来实现此目标。现在我感兴趣的是复制具有文件夹结构的单个文件。完成后,我将在其上使用循环。

我可以通过用 python 或 java 编码来实现这一点,但我必须在只能访问 Powershell 的远程服务器上运行它。我对 Powershell 没有什么经验。

我尝试搜索文档和互联网并得出以下两个命令:

1) Copy-Item -Path LargeFolder\folder1\sub-folder1\file1.txt -Destination NewFolder\ -Recurse
2) Get-ChildItem -Path LargeFolder\folder1\sub-folder1\file1.txt | Copy-Item -Destination NewFolder\ -Recurse -Container

这两个命令都仅将复制到没有文件夹结构的file1.txt位置NewFolder

两个命令的输出:

NewFolder
 |- file1.txt

预期产出:

NewFolder
  |-folder1
    |-sub-folder1
      |-file1.txt

//or even this I don't mind an extra root folder
NewFolder
  |-LargeFolder
    |-folder1
      |-sub-folder1
        |-file1.txt

我有点迷茫了。我怎样才能保留文件夹结构?

答案1

您必须先创建子文件夹结构,然后才能将文件复制到其中:

$Destination = 'NewFolder'
$FileToCopy = 'LargeFolder\folder1\sub-folder1\file1.txt'

Get-Item $fileToCopy | Copy-Item -Destination {
    $DestPath = Join-Path $Destination (( $_.DirectoryName -Split 'LargeFolder' )[-1] )
    If ( ! ( Test-Path )) { mkdir $DestPath | out-null }
    $DestPath
}

答案2

您可以使用 Robocopy(使用或不使用 PowerShell)和/或 Copy-Item 执行此操作,您需要的所有内容都在帮助文件中:

‘使用 robocopy 的 PowerShell’

YouTube -'PowerShell 复制项目'

Robocopy /?
-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows                              
-------------------------------------------------------------------------------

  Started : Wednesday, 26 August, 2020 08:40:06
              Usage :: ROBOCOPY source destination [file [file]...] [options]

             source :: Source Directory (drive:\path or \\server\share\path).
        destination :: Destination Dir  (drive:\path or \\server\share\path).
               file :: File(s) to copy  (names/wildcards: default is "*.*").

::
:: Copy options :
::
                 /S :: copy Subdirectories, but not empty ones.
                 /E :: copy subdirectories, including Empty ones.
             /LEV:n :: only copy the top n LEVels of the source directory tree.

                 /Z :: copy files in restartable mode.
                 /B :: copy files in Backup mode.
                /ZB :: use restartable mode; if access denied use Backup mode.
                 /J :: copy using unbuffered I/O (recommended for large files).
            /EFSRAW :: copy all encrypted files in EFS RAW mode.

  /COPY:copyflag[s] :: what to COPY for files (default is /COPY:DAT).
                       (copyflags : D=Data, A=Attributes, T=Timestamps, X=Skip alt data streams).
                       (S=Security=NTFS ACLs, O=Owner info, U=aUditing info).

 
               /SEC :: copy files with SECurity (equivalent to /COPY:DATS).
           /COPYALL :: COPY ALL file info (equivalent to /COPY:DATSOU).
            /NOCOPY :: COPY NO file info (useful with /PURGE).
            /SECFIX :: FIX file SECurity on all files, even skipped files.
            /TIMFIX :: FIX file TIMes on all files, even skipped files.

             /PURGE :: delete dest files/dirs that no longer exist in source.
               /MIR :: MIRror a directory tree (equivalent to /E plus /PURGE).
 ...

YouTube'PowerShell 复制项目'

# Get specifics for a module, cmdlet, or function
(Get-Command -Name Copy-Item).Parameters
(Get-Command -Name Copy-Item).Parameters.Keys
# Results
<#
Path
LiteralPath
Destination
Container
Force
Filter
Include
Exclude
Recurse
PassThru
Credential
Verbose
Debug
ErrorAction
WarningAction
InformationAction
ErrorVariable
WarningVariable
InformationVariable
OutVariable
OutBuffer
PipelineVariable
WhatIf
Confirm
UseTransaction
FromSession
ToSession
#>
Get-help -Name Copy-Item -Examples
# Results
<#
Copy-Item "C:\Wabash\Logfiles\mar1604.log.txt" -Destination "C:\Presentation"
Copy-Item "C:\Logfiles" -Destination "C:\Drawings" -Recurse
Copy-Item C:\Logfiles -Destination C:\Drawings\Logs -Recurse
Copy-Item "\\Server01\Share\Get-Widget.ps1" -Destination "\\Server12\ScriptArchive\Get-Widget.ps1.txt"
$Session = New-PSSession -ComputerName "Server01" -Credential "Contoso\PattiFul"
 Copy-Item "D:\Folder001\test.log" -Destination "C:\Folder001_Copy\" -ToSession $Session
$Session = New-PSSession -ComputerName "Server02" -Credential "Contoso\PattiFul"
 Copy-Item "D:\Folder002\" -Destination "C:\Folder002_Copy\" -ToSession $Session
$Session = New-PSSession -ComputerName "Server04" -Credential "Contoso\PattiFul"
 Copy-Item "D:\Folder003\" -Destination "C:\Folder003_Copy\" -ToSession $Session -Recurse
$Session = New-PSSession -ComputerName "Server04" -Credential "Contoso\PattiFul"
 Copy-Item "D:\Folder004\scriptingexample.ps1" -Destination "C:\Folder004_Copy\scriptingexample_copy.ps1" -ToSession $Session
$Session = New-PSSession -ComputerName "Server01" -Credential "Contoso\PattiFul"
 Copy-Item "C:\MyRemoteData\test.log" -Destination "D:\MyLocalData\" -FromSession $Session
$Session = New-PSSession -ComputerName "Server01" -Credential "Contoso\PattiFul"
 Copy-Item "C:\MyRemoteData\scripts" -Destination "D:\MyLocalData\" -FromSession $Session
$Session = New-PSSession -ComputerName "Server01" -Credential "Contoso\PattiFul"
 Copy-Item "C:\MyRemoteData\scripts" -Destination "D:\MyLocalData\scripts" -FromSession $Session
#>
Get-help -Name Copy-Item -Full
Get-help -Name Copy-Item -Online

相关内容