【问题标题】:Compare two Directories in Powershell and work with the incomparisons比较 Powershell 中的两个目录并使用不比较
【发布时间】:2012-10-01 02:25:06
【问题描述】:

我尝试比较 2 个文件夹。 ABBA 的备份文件夹。首先我想检查文件夹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

【问题讨论】:

  • 如果我明白你想做什么,Robocopy 已经做到了。
  • 问题是我想保存文件 RoboCopy 会用 /mir 自动删除。这就是为什么我想首先查看 A 上删除了哪些文件,然后将它们移动到“_DeletedFiles”文件夹。

标签: powershell move directory robocopy


【解决方案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

【讨论】:

    猜你喜欢
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 2021-04-05
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多