简体   繁体   中英

Powershell: Copy selected files in subfolders

I have a folder c:\\graphics (root) and a program creates subfolders aa , bm , czx , mjpq , zz (this is a example, i have many subfolders, length it's variable). In each subfolder, i have 26 files and one folder. For example, aa folder has: *aa_000.jpg*, *aa_001.jpg*,..., *aa_023.jpg*; others two files *aa_360.jpg*, aa.html and a folder, path complete it's C:\\graphics\\aa\\aa_360 . I need to move 24 files *aa_000.jpg*, *aa_001.jpg*,..., *aa_023.jpg* in subfolder c:\\graphics\\aa\\aa_360 and also equal to each subfolder. Other example it's c:\\graphics\\mjpq\\mjpq_360 should has mjpq_000.jpg,...,mjpq_023.jpg.

I started the script, thinking move all .jpg (25 files) in the subfolder (later, I had to think like extract the *xxx_360.jpg*) but not work:

Get-ChildItem c:\graphics -rec | where-object {$_.extension -match "jpg"} | ForEach-object {$newPath =(Get-Location).toString()+(Get-Location).ToString().SubString(10)+"_360"); Move-Item $_.FullName $newPath}

but Get-Location not found file path. Thanks for advanced.

NOTE: i found a solution. Working, but i found errors in console:

Get-ChildItem c:\graphics -rec | where-object {$_.extension -match "jpg" -and (!($_.name -like "*360*"))} | ForEach-Object {$newPath =($_.DirectoryName).toString()+($_.DirectoryName).ToString().SubString(10)+"_360"; Move-Item $_.FullName $newPath}

It's my first powershell script ;)

I think you can simplify this to:

Get-ChildItem c:\graphics -rec *.jpg | Move-Item -Dest {$_.Directory.FullName + "\" +
    $_.Directory.Name + "_360\" } -WhatIf

Remove the -whatif if the suggested move operations look correct.

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