2012-03-19 5 views
2

PowerShell에서 다음 작업을 수행 할 수 있습니까?PowerShell을 사용하여 디렉터리 구조 복사

Copy-Item \\m1\C$\Online\* -Recurse -Destination \\m2\C$\Config-Backup -include *.config 

을하지만 루트에는 구성 파일이없는 아마 때문에, 아무것도하지 않는 :

xcopy \\m1\C$\Online\*.config \\m2\C$\Config-Backup /s 

나는이 시도했다. 어떻게해야합니까?

답변

2

당신이 (타사 .NET 모듈 : P) 기본 PowerShell을 사용하려는 경우 긴 파일 경로 (> 255 자)하게하고 싶지 않아도하고를 중단 복사, 당신은이를 사용할 수 있습니다

# Import AlphaFS .NET module - http://alphafs.codeplex.com/ 
Import-Module C:\Path\To\AlphaFS\DLL\AlphaFS.dll 

# Variables 
$SourcePath = "C:\Temp" 
$DestPath = "C:\Test" 

# RecursePath function. 
Function RecursePath([string]$SourcePath, [string]$DestPath){ 

    # for each subdirectory in the current directory..  
    [Alphaleonis.Win32.Filesystem.Directory]::GetDirectories($SourcePath) | % { 

     $ShortDirectory = $_ 
     $LongDirectory = [Alphaleonis.Win32.Filesystem.Path]::GetLongPath($ShortDirectory) 

     # Create the directory on the destination path. 
     [Alphaleonis.Win32.Filesystem.Directory]::CreateDirectory($LongDirectory.Replace($SourcePath, $DestPath)) 

     # For each file in the current directory..            
     [Alphaleonis.Win32.Filesystem.Directory]::GetFiles($ShortDirectory) | % { 

      $ShortFile = $_ 
      $LongFile = [Alphaleonis.Win32.Filesystem.Path]::GetLongPath($ShortFile) 

      # Copy the file to the destination path.                  
      [Alphaleonis.Win32.Filesystem.File]::Copy($LongFile, $LongFile.Replace($SourcePath, $DestPath), $true)        

     } 

    # Loop. 
    RecursePath $ShortDirectory $DestPath 
    } 
} 

# Execute! 
RecursePath $SourcePath $DestPath 

이 코드 내 훨씬 더 큰 프로젝트에서 제거,하지만 난 그것을 빠른 테스트를주고 그것을 작동하는 것 같다되었다 유의하시기 바랍니다. 희망이 도움이!

+0

언급 한 라이브러리가 멋지다고 생각됩니다. http://alphafs.codeplex.com/ –

+0

철저한 답변 주셔서 감사합니다. 나는 M $에 대한 믿음을 잃어 가고 있습니다. 이것은 우습다. 나는 xcopy를 고수 할 것이다. 간결하고 간결합니다. Powershell은 thedailywtf에 속합니다. – Jim

+0

그래, AlphaFS 프로젝트는 훌륭합니다. 우리는 그룹 공유에 긴 파일 경로가있는 환경에서 커다란 쟁점을 가지고 있으며이를 통해 쉽고 편리하게 극복 할 수있었습니다. 처음에는 네이티브 C# 코드를 작성했지만 PowerShell (누가 알았습니까 : P)에서 직접 DLL을 호출 할 수 있다는 것을 깨달았습니다. 그들은 또한 광범위한 문서를 가지고 있으며, 항상 더하기입니다. 나는 지금이 물건의 한가운데에있어. 그래서 비슷한 질문이 있으면 알려줘. –

2
Start-Process xcopy "\\m1\C$\Online\*.config \\m2\C$\Config-Backup /s" -NoNewWindow 

: P

0

새로운 AlphaFS 2.0은 정말 쉽습니다.

예 : ROBOCOPY에 반복적으로

# Set copy options. 
 
PS C:\> $copyOptions = [Alphaleonis.Win32.Filesystem.CopyOptions]::FailIfExists 
 

 
# Set source and destination directories. 
 
PS C:\> $source = 'C:\sourceDir' 
 
PS C:\> $destination = 'C:\destinationDir' 
 

 
# Copy directory recursively. 
 
PS C:\> [Alphaleonis.Win32.Filesystem.Directory]::Copy($source, $destination, $copyOptions)

AlphaFS on GitHub

0

봐 디렉토리를 복사합니다. 기본 PowerShell 명령은 아니지만 PowerShell 스크립트에서 항상 호출합니다. xcopy와 유사하게 작동하지만 더 강력합니다.

+1

나는 robocopy를 발견했다. 내 감정은 나무 상자를 나무 상자에서 꺼내는 데 실패한 파워 쉘이 꽤 한심하다는 것입니다. – Jim

관련 문제