Batch Append Filenames

by Mondeoman at 2012-08-22 17:59:27

Hi all, I am trying to find a powershell script that would help me with creating and renaming in the following scenario:
I have lots of single video files ("Videonames.avi") in seperate folders, also included in each folder is a single image file ("Pictures.jpg"). What I would like to do is create a 2nd identical Image File ("Pictures.jpg") and rename this Image to the single Video Filename

Example Before:
C:\Temp\Videos\Terminator\Terminator (1984).avi

Example After:
C:\Temp\Videos\Terminator\Terminator (1984).avi
C:\Temp\Videos\Terminator\Terminator (1984).jpg

Many thanks to anyone that can help.
As I am a noobie at powershell, I do have other questions to ask later if this can be solved.
by JeffH at 2012-08-22 18:20:59
This is a little tricky and certainly not intuitive for a beginner. But assuming the video folders are all top level, such as under C:\temp\videos, something like this will work.

dir c:\temp\videos | where {$.PSIscontainer} | foreach {
$avi=dir $
.fullname -filter *.avi
$jpg=dir $.fullname -filter Picture.jpg
$newname=Join-path $
.fullname -child "$($avi.basename).jpg"
$jpg | copy-item -dest $newname -passthru

The first part gets just the directories under C:\Temp\Videos. Then for each directory I’m getting the AVI and JPG file objects. This will only work if there is a single AVI. I get the basename from the AVi, and use it to define a new name. Finally I copy the jpg using the new name as the destination.
by Mondeoman at 2012-08-23 03:19:31
Excellent Jeff and thank you.
Will be back shortly with a few more tricky questions.