I'm trying to develop a script that copy .txt files from two source folders to one destination folder.
I would like as well to add 2 additional conditions:
- If a file already exists in the target directory, skip it
- The copied files must not be older than the current day
This is what I made, but it doesn't work as expected:
$SourceDirItemsMITEMAvail = "\\wipfs02\Data\webshop\test\ecomcloud_import\carhartt\product\*"
$SourceDirCaproBPRIC = "\\wipfs02\Data\webshop\test\hybris_import\carhartt\product\*"
$TargetDirB2B = "\\wipfs02\Data\webshop\test\ecomcloud_import\carhartt\b2b\product\"
$ExcludeSubDir = "archive", "IDISC Test", "save", "archive_B2B", "archive_tmp", "Errors"
# Copy only the specific files
$SpecificTypesEcomCloudImport = "avail*", "items*", "MITEM*"
$SpecificTypesHybrisImport = "BPRIC*", "CAPRO*"
# Get the current date
$CurrentDateFiles = (Get-Date).Date
# Copy items from source directories and transfer them to the B2B Folder
For ($i=0; $i -lt $SourceDirItemsMITEMAvail.Count -and $SourceDirCaproBPRIC.Count; $i ) {
# If the copied files don't already exist into the target directory, then copy them
if (-not(Test-Path -Path $SourceDirItemsMITEMAvail, $SourceDirCaproBPRIC)) {
try {
# The copied files must not be older that the current day
$CopyFilesEcomCloudImportDir = Copy-Item $SourceDirItemsMITEMAvail -Exclude $ExcludeSubDir -Destination $TargetDirB2B -PassThru -Verbose -Include $SpecificTypesEcomCloudImport | Where-Object { $_.LastWriteTime -eq $CurrentDateFiles}
Write-Host ""$CopyFilesEcomCloudImportDir.Count" file(s) have been copied from $SourceDirItemsMITEMAvail to $TargetDirB2B."
# The copied files must not be older that the current day
$CopyFilesHybrisImportDir = Copy-Item $SourceDirCaproBPRIC -Exclude $ExcludeSubDir -Destination $TargetDirB2B -PassThru -Verbose -Include $SpecificTypesHybrisImport | Where-Object { $_.LastWriteTime -eq $CurrentDateFiles}
Write-Host ""$CopyFilesHybrisImportDir.Count" file(s) have been copied from $SourceDirCaproBPRIC to $TargetDirB2B."
}
catch {
"Error: $($_.Exception.Message)"
}
}else {
Write-Host "Cannot copy $CopyFilesEcomCloudImportDir because a file with that name already exists in the destination folder."
Write-Host "Cannot copy $CopyFilesHybrisImportDir because a file with that name already exists in the destination folder."
}
}
Either it copies all the files from the sources directories, or it goes immediately to the else condition.
If I remove the if (Test-Path) condition, it copies all the files from the sources, even those who are older than the current day.
If I let the if (Test-Path) condition, it goes immediately to the else statement.
Could you help me to resolve this? Thanks!
CodePudding user response:
Using just PowerShell, you could do this:
$SourceDirItemsMITEMAvail = "\\wipfs02\Data\webshop\test\ecomcloud_import\carhartt\product"
$SourceDirCaproBPRIC = "\\wipfs02\Data\webshop\test\hybris_import\carhartt\product"
$TargetDirB2B = "\\wipfs02\Data\webshop\test\ecomcloud_import\carhartt\b2b\product"
$ExcludeSubDir = "archive", "IDISC Test", "save", "archive_B2B", "archive_tmp", "Errors"
# create a regex of the folders to exclude
# each folder will be Regex Escaped and joined together with the OR symbol '|'
$notThese = ($ExcludeSubDir | ForEach-Object { [Regex]::Escape($_) }) -join '|'
# Copy only the specific files
##############################
# if all files are .txt files, change the patterns below to include that:
# like "avail*.txt", "items*.txt", "MITEM*.txt","BPRIC*.txt", "CAPRO*.txt"
##############################
$FileNamePattern = "avail*", "items*", "MITEM*","BPRIC*", "CAPRO*"
# Get the current date
$today = (Get-Date).Date
Get-ChildItem -Path $SourceDirItemsMITEMAvail, $SourceDirCaproBPRIC -Include $FileNamePattern -File -Recurse |
Where-Object { $_.DirectoryName -notmatch $notThese -and $_.LastWriteTime.Date -eq $today } |
ForEach-Object {
$targetFile = Join-Path -Path $TargetDirB2B -ChildPath $_.Name
if (Test-Path -Path $targetFile -PathType Leaf) {
Write-Host "File '$targetFile' already exists. Skipping"
}
else {
$_ | Copy-Item -Destination $TargetDirB2B -Force
}
}