0

enter image description here

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 tried this code, I can check the existence of .jpg, then match the file to 1st folder file. My problem, I can not copy if the file .jpg more than 1 and when I copy the file, I can not specify which subfolder that I should copy.

I tried this:

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

if (Test-Path -Path "$JobError\*\*.jpg") {
    Write-Host "Error Exist"
    $L_Name = "15"
    $ErrorFile = Get-ChildItem -Path "$JobError\*\*.jpg" |
                 ForEach-Object { $_.BaseName.Substring($L_Name) }

    $Path_ = Get-ChildItem -Path "$JobError\*\*.jpg"
    $Split = Split-Path -Path $Path_

    $NewJob = @(Get-ChildItem -Path "$JobInit\*.png" -File -Recurse |
              Where-Object { "$ErrorFile" -contains $_.BaseName })
    Write-Host $NewJob

    $Timestamp = Get-Date -Format yyyyMMddhhmmss
    $CopyJob = Copy-Item $NewJob -Destination "$Split"
    $Rename = Get-ChildItem "$Split\*.png" |
              Rename-Item -NewName {"$Timestamp`_" + $_.Name.Replace('.png','.gif')}
}
1
  • To me, your question is not very clear.. Please could you edit the question and add a schematic example of what the folderstructure now looks like and what the desired result should be? Commented Oct 1, 2019 at 8:01

1 Answer 1

1

Assuming that I understood your question correctly and you want to replace existing JPEG files in the "Process" folder if they have a corresponding PNG file in the "Initial" folder, the following should do the trick:

$L_Name = 15
Get-ChildItem -Path "$JobError\*\*.jpg" | ForEach-Object {
    $basename = $_.BaseName.Substring($L_Name)

    $png = "$JobInit\${basename}.png"
    if (Test-Path $png) {
        $timestamp = Get-Date -Format 'yyyyMMddhhmmss'
        $dst = Join-Path $_.DirectoryName "${timestamp}_${basename}.jpg"
        Copy-Item $png $dst -Force
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hi @Ansgar, Thank you for your advice. I updated the code. I dont want to change the existing JPEG file in the process. The file that I want to change is .png from Initial Folder.
@Joe Since you're getting the current timestamp for every copied file the existing JPEG files shouldn't be replaced (unless they were created with timestamps in the future and you somehow just happen to hit those exact same timestamps when copying the file). If you want the extension to be gif instead of jpg, just change it in the code above. Otherwise please take a step back and describe the actual problem you're trying to solve instead of what you perceive as the solution (since I can't think of any valid reason for renaming a PNG file to .jpg - or .gif for that matter).
hi @Ansgar. Thank you. It works for me now. But I found a problem, Once I use infinite loop for this script. This part ` Copy-Item $png $dst -Force` does not overwrite the file. So I have a lot of duplicate file. Could you please help me. Thanks

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.