0

我尝试比较 2 个文件夹。BA的备份文件夹。首先,我想检查文件夹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文件夹中的每个 File 并检查该 File 是否存在于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
4

1 回答 1

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
于 2012-10-10T18:22:16.783 回答