2つのフォルダを比較しようとしています。AとB。 BはAのバックアップ フォルダです。まず、フォルダーAでファイルが削除されているかどうかを確認したい場合は、対応するファイルをBから取得して移動します。次に、RoboCopy /mir Aをもう一度実行して、バックアップを作成します。
私の問題は、2 つのディレクトリ (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