简体   繁体   中英

Powershell: find files based on directory name, copy and rename into same folder and keep original

I am migrating to a new Movie/DVD/BluRay management systems, and need to take existing cover art that is named based on the directory name (directoryname.jpg), copy and name the new files folder.jpg and mymovies-front.jpg. I also need to keep the original file in place.

## Q:\Test\2017\07\14\SO_45110819.ps1
Set-Location "X:\path\to\base"

Get-ChildItem * | Where-Object {$_.PSIsContainer}|ForEach-Object {
    $dirnamejpg = Join-Path $_.FullName ($_.BaseName+'.jpg')
    if (Test-Path ($dirnamejpg)) {
        copy-Item -Path $dirnamejpg -Destination (Join-Path $_.FullName ('folder.jpg')) -confirm
        copy-Item -Path $dirnamejpg -Destination (Join-Path $_.FullName ('mymovies-front.jpg')) -confirm
    }
}

Once the actions performed are OK, remove the -confirm parameter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM