0

I want to check .jpg file in the 2nd folder. 2nd folder has some subfolder. if .jpg exist in the subfolder of 2nd folder, I will copy a file from 1st folder to subfolder of 2nd folder based on the base name. I can do this part refer to this answer How to copy file based on matching file name using PowerShell?

But I want to do limitation while I copy a file from 1st folder. I will NOT copy the file, if I already copy the same file from 1st folder to 2nd folder 3 times.

This is the code from the reference.

$Job_Path = "D:\Initial"
$JobError = "D:\Process"

Get-ChildItem -Path "$OpJob_Path\*\*.jpg" | ForEach-Object {
    $basename = $_.BaseName.Substring(15)
    $job = "$Job_Path\${basename}.png"
    if (Test-Path $job) {
        $timestamp = Get-Date -Format 'yyyyMMddhhmmss'
        $dst = Join-Path $_.DirectoryName "${timestamp}_${basename}.gif"
        Copy-Item $job $dst -Force
    }

enter image description here

Anyone can help me to solve this problem please. Thank you.

Updated

$Job_Path = "D:\Initial"
$JobError = "D:\Process"

Get-ChildItem -Path "$OpJob_Path\*\*.jpg" | ForEach-Object {
    $basename = $_.BaseName.Substring(15)
    $job = "$Job_Path\${basename}.png"
    if (Test-Path $job) {
        $timestamp = Get-Date -Format 'yyyyMMddhhmmss'
        $dst = Join-Path $_.DirectoryName "${timestamp}_${basename}.gif"
        $Get = (Get-ChildItem -Name "$OpJob_Path\*\*$basename.jpg*" | Measure-Object).Count
        $Get
        if ($Get -eq "3") {
            Write-Host "Continue Other Process"
            NEXT_PROCESS
        } else {
            Write-Host "Less than 3"
        }
        Copy-Item $job $dst -Force
    }
12
  • 1
    once you have your list of files, you can use the Group-Object cmdlet with a calculated property to group the files. the result will have a .Count property that you can use. [grin] something like this >>> $FileList | Group-Object -Property {$_.BaseName.Split('_')[-2,-1] -join '_'} <<< will give you two groups - three 0908_x & two 0907_y items. Commented Oct 16, 2019 at 8:45
  • 1
    that is a totally different question. [grin] use the -Directory parameter of Get-ChildItem to get a list of dirs & then iterate thru that to get a list of files in each dir. Commented Oct 16, 2019 at 8:54
  • 1
    Create a hashtable where you increment the copy count for each file and copy only if the count for the given file doesn't exceed your threshold. Commented Oct 16, 2019 at 10:21
  • 1
    arg! [frown] it looks like AnsgarWiechers has a workable idea for you. i will go back to lurking. Commented Oct 16, 2019 at 10:42
  • 1
    @Joe - perhaps your test should be $Get -ge 3 instead of $Get -eq "3" the 2nd is testing against a STRING and requires and exact match. Commented Oct 17, 2019 at 3:36

1 Answer 1

1

$Get is not a hashtable, and it's also not keeping track of what's been copied already. You need to define the hashtable outside the loop

$copy_count = @{}
Get-ChildItem -Path "$OpJob_Path\*\*.jpg" | ForEach-Object {
    ...
}

and then update it whenever you copy a file

if ($copy_count[$_.Name] -le 3) {
    Copy-Item ...
    $copy_count[$_.Name]++
} else {
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.