2012-10-10 1 views
0

2 개의 폴더를 비교하려고합니다. 및 b. 의 백업 폴더입니다. 첫째로 어떤 파일이 폴더에서 삭제되었는지 확인하고 싶습니다. 만약 그렇다면 해당 파일을 B에서 가져 와서 옮기고 싶습니다. 그런 다음 RoboCopy/mir A을 다시 실행하여 백업을 생성 할 수 있습니다.Powershell의 두 디렉토리를 비교하여 불일치로 작업하십시오.

내 문제는 두 개의 디렉토리 (A와 B가 하위 폴더 등을 가짐)를 비교 한 다음 삭제 된 파일을 이동하는 방법입니다. 이 작업을 수행해야하는 코드는 다음과 같습니다.

#region | Global Variables 
$backupFolder = "Sandbox_Backup" 
$UsbDisk = "C:\Users\pullrich\Desktop" 
#endregion 

$date = Get-Date -Format G 
# Create Log that Script has been started 
MyLog "--- Script has been started at $date ---" 0 
$testfolder = Test-Path "$UsbDisk\$backupFolder" 
# If a Backup folder already exist then Compare files and see if any changes have been made 
     if ($testfolder -eq $true) { #IF_2 

        # Copy deleted files to BackUp Folder 
        MyLog "Check for Deleted Files on Data_01:\" 0     
        $source = $testfolder + "\Data_01" 
        $sourcelist = Get-ChildItem $source -Recurse 
        $destination = "$UsbDisk\$backupFolder\Data_01\_DeletedFiles" 
        $testDestination = Test-Path $destination 
        if ($testDestination -eq $false){ 
         mkdir $destination 
        } 

        foreach ($file in $sourcelist){ 
        $result = test-path -path "C:\Users\pullrich\Desktop\Sandbox\Data_01*" -include $file.Name 
         if (-not $result){ 
         $CopyFile = $file.DirectoryName + "\" + $file.Name 
         Copy-Item $CopyFile -Destination $destination 
         Remove-Item $CopyFile 
         } 

        } 

        # Start Synchronizing Data_01:\ 
        MyLog "Start Synchronizing Data_01:\" 0 
        Robocopy "C:\Users\pullrich\Desktop\HP-Sandbox\Data_01" "$UsbDisk\$backupFolder\Data_01" /mir /r:2 /w:3 /M /XD VM_* 

        MyLog "Data_01:\ is up to Date" 0 
     } #IF_2 

UPDATE 1

나는 코드 작업을 계속하고 문제가 여기에있을 것 같다 것을 알아 냈 :

  $result = test-path -path "C:\Users\pullrich\Desktop\Sandbox\Data_01*" -include $file.Name 

정말하지 않기 때문에 그것은 항상 false를 돌려줍니다 내가 의도 한대로하는 것 같아. B 폴더에있는 모든 파일을 검토하고 해당 파일이 A에 있는지 확인해야합니다.

답변 나는 latkins '대답 할 스크립트를 얻을 수 있었다. 참고로 Full 스크립트가 있습니다.

#region | Global Variables 
$backupFolder = "Sandbox_Backup" 
$UsbDisk = "C:\Users\pullrich\Desktop" 
#endregion 

$date = Get-Date -Format G 
# Create Log that Script has been started 
MyLog "--- Script has been started at $date ---" 0 

     $testfolder = Test-Path "$UsbDisk\$backupFolder" 

     # If a Backup folder already exist then Compare files and see if any changes have been made 
     if ($testfolder -eq $true) { #IF_2 

        # Copy deleted files to BackUp Folder 
        MyLog "Check for Deleted Files on Data_01:\" 0 
        # Set Data_01 as Source 
        $source = "$UsbDisk\$backupFolder\Data_01" 
        # Get all Items 
        $sourcelist = Get-ChildItem $source -Recurse 
        # Choose as Destination the DeletedFiles Folder 
        $destination = "C:\Users\pullrich\Desktop\Sandbox\Data_01\_DeletedFiles" 
        mkdir $destination 

        # Check if a File or Folder exists in the BackUp folder but not on the Server 
        foreach ($file in $sourcelist){ 
         # First we have to check if the File we have in the Backup still exists on the Server; however, we do not care about the DeletedFiles Folder 
         if (-not ($file.Name -eq "_DeletedFiles")){ 
          $fullPath = "C:\Users\pullrich\Desktop\Sandbox\Data_01" + $file.FullName.Substring($source.Length) 
          $result = test-path $fullPath 
          # If it doesn't exist then we go ahead and Copy the File to the DeletedFiles Folder on the Server, 
          # which will later be copied to the BackUp and then deleted of the Server 
          if(-not $result){ 
           $CopyFile = $source + $file.FullName.Substring($source.Length) 
           Copy-Item $CopyFile -Destination $destination -force 
          } 

         } 

        } 

        # Start Synchronizing Data_01:\ 
        MyLog "Start Synchronizing Data_01:\" 0 
        Robocopy "C:\Users\pullrich\Desktop\Sandbox\Data_01" "$UsbDisk\$backupFolder\Data_01" /mir /r:2 /w:3 /M /XD VM_* 

        #Delete the DeletedFiles Folder from Server and keep it only on the BackUp 
        Remove-Item $destination 

        MyLog "Data_01:\ is up to Date" 0 
     } #IF_2 

     else { #Else_2 

        mkdir "$UsbDisk\$backupFolder" 

        # Start Copying Data 
        MyLog "Start Backing up Data_01:\" 0 
        Robocopy "C:\Users\pullrich\Desktop\Sandbox\Data_01" "$UsbDisk\$backupFolder\Data_01" /mir /r:2 /w:3 /XD VM_* 

     } 

# Delete All Files in _DeleteFiles Folder which are Older than 60 Days 
+0

내가 원하는 것을 이해하면 Robocopy가 이미이 작업을 수행합니다. – EBGreen

+0

RoboCopy가/mir로 자동 삭제할 파일을 저장하고 싶습니다. 그래서 "A 파일에서 삭제 된 파일"을 먼저보고 "_DeletedFiles"폴더로 옮기려고합니다. – seN

답변

1

잠재적으로 값 비싼 반복적 인 와일드 카드 검색에 의존하는 대신 잠재적 인 파일마다 전체 경로를 구성 할 수 있습니다.

$file$source의 일부 하위 디렉토리에서 온다, 그래서 ($file.FullName에 의해 주어진)의 전체 경로는 $file.FullName에서 처음 $source.Length 문자를 놓는 방법으로 "상대"경로를 단축 할 수있다. 새로운 루트 경로를이 상대 경로에 추가하면 새로운 전체 경로가 생깁니다.

$fullPath = "C:\Users\pullrich\Desktop\Sandbox\Data_01" + $file.FullName.Substring($source.Length) 
$result = Test-Path $fullPath