How to copy file based on matching file name using PowerShell?

后端 未结 1 1159
南笙
南笙 2021-01-29 04:02

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 subfolde

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-29 04:24

    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
        }
    }
    

    0 讨论(0)
提交回复
热议问题