2012-03-19 8 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から直接DLLを呼び出すことができました(Pは知っていました)。彼らには広範な文書もありますが、これは常にプラスです。私はこの物事の真っ只中にいるので、もしあなたに似たような質問があれば教えてください。 –

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

:P

0

新しいAlphaFS 2.0はこれを本当に簡単にします。

例:再帰的にディレクトリをコピー

# 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

はrobocopyを調べてください。それはネイティブのPowerShellコマンドではありませんが、私はPowerShellスクリプトから常に呼び出しています。 xcopyと同様に動作するだけで、より強力です。

+1

私はその後robocopyを見つけました。私の気持ちは、ボックスからツリーコピーを引き出せなかったパワーシェルがかなり哀れなことです。 – Jim

関連する問題