Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Database name
- $databaseName = "MY-DATABASE-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" } |
- Sort-Object -Property Length
- # Build paths for the source and destination data files
- $destinationDataFiles = $sourceDataFiles | ForEach-Object { Join-Path -Path $destinationFolder -ChildPath $_.Name }
- $totalFiles = $sourceDataFiles.Count
- $completedFiles = 0
- # Copy the data files with progress information
- 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
- $completedFiles++
- $copyPercentage = ($completedFiles / $totalFiles) * 100
- Write-Host "File copied. Progress: $($completedFiles) of $($totalFiles) files copied. Copy percentage: $($copyPercentage)%`n"
- }
- Write-Host "Database files for $databaseName copied successfully."
- } else {
- Write-Host "Source folder does not exist."
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement