Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Function to calculate hash value for a file
- function Get-FileHashValue($filePath) {
- $hashAlgorithm = 'SHA256' # You can change this to other algorithms like MD5, SHA1, etc.
- $fileHash = Get-FileHash -Path $filePath -Algorithm $hashAlgorithm
- return $fileHash.Hash
- }
- # Database name
- $databaseName = "MY-DATABASE-NAME-HERE"
- # Source and destination folders
- $sourceFolder = "D:\Database\Data"
- $destinationFolder = "F:\Database\Data"
- # Check if the source folder exists
- if (Test-Path $sourceFolder -PathType Container) {
- # Create the destination folder if it doesn't exist
- if (-not (Test-Path $destinationFolder -PathType Container)) {
- New-Item -ItemType Directory -Force -Path $destinationFolder
- }
- # Get all files in the source folder and filter based on the database name
- $sourceDataFiles = Get-ChildItem -Path $sourceFolder | Where-Object { $_.Name -like "$databaseName*.ndf" -or $_.Name -like "$databaseName*.mdf" }
- # Build paths for the source and destination data files
- $destinationDataFiles = $sourceDataFiles | ForEach-Object { Join-Path -Path $destinationFolder -ChildPath $_.Name }
- $totalFiles = $sourceDataFiles.Count
- $completedFiles = 0
- $failedFiles = @()
- # Copy the data files with progress information and check hash values
- foreach ($file in $sourceDataFiles) {
- $destinationPath = Join-Path -Path $destinationFolder -ChildPath $file.Name
- Write-Host "Copying $($file.FullName) to $($destinationPath)..."
- Copy-Item -Path $file.FullName -Destination $destinationPath -Force
- # Check hash values
- $sourceHash = Get-FileHashValue -FilePath $file.FullName
- $destinationHash = Get-FileHashValue -FilePath $destinationPath
- if ($sourceHash -eq $destinationHash) {
- Write-Host "File copied successfully. Hash values match."
- } else {
- Write-Host "Error copying file: $($file.FullName). Hash values do not match."
- $failedFiles += $file.Name
- }
- $completedFiles++
- $copyPercentage = ($completedFiles / $totalFiles) * 100
- Write-Host "Progress: $($completedFiles) of $($totalFiles) files copied. Copy percentage: $($copyPercentage)%`n"
- }
- if ($failedFiles.Count -eq 0) {
- Write-Host "All files copied successfully."
- } else {
- Write-Host "Files with hash mismatches: $($failedFiles -join ', ')"
- }
- } else {
- Write-Host "Source folder does not exist."
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement