English 中文(简体)
How can I copy complex directories with PowerShell s robocopy, including exception files?
原标题:

I have a problem in a complex copy.

Below is an example of my script and CSV with source and destination paths. The powershell script I created works well in part but not totally, it makes the copies based on the CSV, however in the example we see the directories "2018", "2019", "2020", "2021", "2022" need to be copied to the destination directory "Archive" and the directory "2023" under the destination directory "Years", so far the script is doing its job.

CSV model Source;Destination "\data.valampe.comInformation technologyRobotics DepartmentYears";"\data.valampe.comUITRobotics DepartmentYears" "\data.valampe.comInformation technologyRobotics DepartmentYears2018";"\data.valampe.comUITRobotics DepartmentYearsArchives2018" "\data.valampe.comInformation technologyRobotics DepartmentYears2019";"\data.valampe.comUITRobotics DepartmentYearsArchives2018" "\data.valampe.comInformation technologyRobotics DepartmentYears2020";"\data.valampe.comUITRobotics DepartmentYearsArchives2020" "\data.valampe.comInformation technologyRobotics DepartmentYears2021";"\data.valampe.comUITRobotics DepartmentYearsArchives2021" "\data.valampe.comInformation technologyRobotics DepartmentYears2022";"\data.valampe.comUITRobotics DepartmentYearsArchives2022" "\data.valampe.comInformation technologyRobotics DepartmentYears2023";"\data.valampe.comUITRobotics DepartmentYears2023" "\data.valampe.comInformation technologyRobotics DepartmentAI Lab";"\data.valampe.comUITRobotics DepartmentAI Lab" "\data.valampe.comInformation technologyRobotics DepartmentAI Lab2018";"\data.valampe.comUITRobotics DepartmentAI LabArchives2018" "\data.valampe.comInformation technologyRobotics DepartmentAI Lab2019";"\data.valampe.comUITRobotics DepartmentAI LabArchives2018" "\data.valampe.comInformation technologyRobotics DepartmentAI Lab2020";"\data.valampe.comUITRobotics DepartmentAI LabArchives2020" "\data.valampe.comInformation technologyRobotics DepartmentAI Lab2021";"\data.valampe.comUITRobotics DepartmentAI LabArchives2021" "\data.valampe.comInformation technologyRobotics DepartmentAI Lab2022";"\data.valampe.comUITRobotics DepartmentAI LabArchives2022" "\data.valampe.comInformation technologyRobotics DepartmentAI Lab2023";"\data.valampe.comUITRobotics DepartmentAI Lab2023"

Script model

`# Delete all existing variables Remove-Variable -Name * -ErrorAction SilentlyContinue

$date = Get-Date -Format "yyyy-MM-dd-HH-mm-ss"`

# Request Business name $BUsinessLineName = Read-Host "Enter the Business name without space and without special characters"

# Path to CSV file containing source and destination information $csvPath = "E:powershell commandCopyMultifoldersMultifoldersEncryptedTest.csv"

# Read CSV file with specified delimiter (;) $csvContent = Import-Csv -Path $csvPath -Delimiter ";"

# Report name log $reportName = $BUsinessLineName + "_" + $date + "_Test.log" $sourcelogs = "E:powershell commandCopyMultifoldersLogs" + $reportName

# Create an associative array to store previously copied directories $copiedDirectories = @{}

# Browse each line of the CSV file and make the copy foreach ($row in $csvContent) {

`# Get source and destination paths for each line $sourcePath = $row.Source.Trim( " ) $destinationPath = $row.Destination.Trim( " )

Write-Host "Source: $sourcePath"
Write-Host "Destination: $destinationPath"`

# Check if the destination directory already exists if (Test-Path $destinationPath) { Write-Host "Skipping copy of directory: $sourcePath (already exists at $destinationPath)" continue }

# Check if the directory has already been copied to this destination if ($copiedDirectories.ContainsKey($destinationPath) -and $copiedDirectories[$destinationPath] -contains $sourcePath) { Write-Host "Skipping copy of directory: $sourcePath (already copied to $destinationPath)" continue }

# Copy source directory to destination chcp 1252 robocopy $sourcePath $destinationPath /E /r:0 /w:0 /XO /XJ /XJF /XD ~BROMIUM* /XF sourceonefilesmanifest* ~$*.* /LOG:$sourcelogs

# Check if the source directory copy was successful if (!(Test-Path $destinationPath)) { Write-Host "Failed to copy directory: $sourcePath" } else { Write-Host "Successfully copied directory: $sourcePath" # Add the copied directory to the table of copied directories if ($copiedDirectories.ContainsKey($destinationPath)) { $copiedDirectories[$destinationPath] += $sourcePath } else { $copiedDirectories[$destinationPath] = @($sourcePath) } } }

However, the parent source directories "Years" & "AI Lab" contain files which, unfortunately, are not copied. Another problem is that during incremental copies the source files/directories that could potentially be modified during the incremental copy phase are not updated at the destination. Could someone help me?

问题回答

暂无回答




相关问题
Mutually exclusive powershell parameters

SCENARIO I m writing a cmdlet for Powershell 2.0 using Visual Studio 2008 and .NET 3.5 the cmdlet requires 3 arguments. my intended grammar of the cmdlet is something like this: cmdletname [foo|...

Run a program from PowerShell with timeout

I ll write a script that runs a program and wait for it finished. But if the program is not finished within a specified time I want that the program is killed.

How to transpose data in powershell

I have a file that looks like this: a,1 b,2 c,3 a,4 b,5 c,6 (...repeat 1,000s of lines) How can I transpose it into this? a,b,c 1,2,3 4,5,6 Thanks

Powershell v2 remoting and delegation

I have installed Powershell V2 on 2 machines and run Enable-PsRemoting on both of them. Both machines are Win 2003 R2 and are joined to the same active directory domain and I can successfully run ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

热门标签