比较Powershell中的两个目录并使用不相容的方法

时间:2012-10-10 14:54:41

标签: powershell move directory robocopy

我尝试比较2个文件夹。 A B B A 的备份文件夹。首先,我想检查文件夹 A 中是否删除了任何文件,如果是,我想从 B 中取出相应的文件并移动它。然后我可以再继续RoboCopy / mir A ,这将创建备份。

我的问题是如何比较两个目录(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

更新1

我一直在编写代码并发现问题似乎在这里:

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

它总是会返回false,因为我猜它并没有真正按照我的意图行事。它应该遍历 B 文件夹中的每个文件,并检查该文件是否存在于 A 中。

答案

我能够用latkins的答案完成脚本。 作为参考,这是完整的脚本。

#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

1 个答案:

答案 0 :(得分:1)

您可以为每个潜在文件构建完整路径,而不是依赖于可能代价高昂的递归通配符搜索。

每个$file来自$source的某个子目录,因此可以通过删除第一个$file.FullName将完整路径(由$source.Length给出)缩短为“相对”路径来自$file.FullName的字符。获取新的根路径并将其添加到此相对路径,并获得新的完整路径。

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