2

I have a folder c:\graphics (root) and a program that creates subfolders e.g. aa, bm, czx, mjpq, zz (I have many subfolders, the length is 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; other two files aa_360.jpg, aa.html and a folder. The complete path is 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.

Another example is c:\graphics\mjpq\mjpq_360, should have 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 it doesn't 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 doesn't find the file path.


NOTE: I found a solution that is working, but it throw some errors in the 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}

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Working. i found errors, but it work. Create a file more ;). Thanks
I think you need the -include parameter in the Get-ChildItem command?
@Ant This uses the -filter parameter by position.
Ah great - still new to powershell (as you could probably already tell!)

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.