Currently I have a folder C:\Call Archive\
with a XML file name index.xml
and a ton of .wav
files all with names that do not mean anything when you look at it. Names like AT1_ID1_TT3_ID6-1623089222.5534.WAV
What I would like to do is take the XML structure and rename each .wav
file based on the tags in XML.
Source Data:
<all>
<recording_index recapp_id="" recapp_name="" last_generated="0">
<day_index date="2021-06-07">
<recording target_name="Unknown recording" filename="warning%3A" filesize="0" date="1969-12-31 19:00" source="" destination=""/>
<recording target_name="10 Digit Dialing" filename="AT1_ID1_TT3_ID6-1623089222.5534.WAV" filesize="40K" date="2021-06-07 14:07" source="John Doe" destination="5555512345"/>
<recording target_name="10 Digit Dialing" filename="AT1_ID1_TT3_ID6-1623089304.5536.WAV" filesize="42K" date="2021-06-07 14:08" source="John Doe" destination="5555512345"/>
<recording target_name="10 Digit Dialing" filename="AT1_ID1_TT3_ID6-1623091000.5538.WAV" filesize="11K" date="2021-06-07 14:36" source="John Doe" destination="5555512345"/>
<recording target_name="10 Digit Dialing" filename="AT1_ID1_TT3_ID6-1623091138.5540.WAV" filesize="14K" date="2021-06-07 14:39" source="John Doe" destination="5555512345"/>
<recording target_name="10 Digit Dialing" filename="AT1_ID1_TT3_ID6-1623091201.5542.WAV" filesize="14K" date="2021-06-07 14:40" source="John Doe" destination="5555512345"/>
<recording target_name="10 Digit Dialing" filename="AT1_ID1_TT3_ID6-1623091227.5544.WAV" filesize="43K" date="2021-06-07 14:41" source="John Doe" destination="5555512345"/>
<recording target_name="10 Digit Dialing" filename="AT1_ID1_TT3_ID6-1623091276.5546.WAV" filesize="31K" date="2021-06-07 14:41" source="John Doe" destination="5555512345"/>
</day_index>
</recording_index>
</all>
The ideal filename should be 06-07-2021_John Doe_5555512345.wav
. Note that if possible the date would be in MM-DD-YYYY
format.
Anything would help!
You just need to loop through and do the renames. Something like below:
# Establishing test data:
$XML =
@"
<all>
<recording_index recapp_id="" recapp_name="" last_generated="0">
<day_index date="2021-06-07">
<recording target_name="Unknown recording" filename="warning%3A" filesize="0" date="1969-12-31 19:00" source="" destination=""/>
<recording target_name="10 Digit Dialing" filename="AT1_ID1_TT3_ID6-1623089222.5534.WAV" filesize="40K" date="2021-06-07 14:07" source="John Doe" destination="5555512345"/>
<recording target_name="10 Digit Dialing" filename="AT1_ID1_TT3_ID6-1623089304.5536.WAV" filesize="42K" date="2021-06-07 14:08" source="John Doe" destination="5555512345"/>
<recording target_name="10 Digit Dialing" filename="AT1_ID1_TT3_ID6-1623091000.5538.WAV" filesize="11K" date="2021-06-07 14:36" source="John Doe" destination="5555512345"/>
<recording target_name="10 Digit Dialing" filename="AT1_ID1_TT3_ID6-1623091138.5540.WAV" filesize="14K" date="2021-06-07 14:39" source="John Doe" destination="5555512345"/>
<recording target_name="10 Digit Dialing" filename="AT1_ID1_TT3_ID6-1623091201.5542.WAV" filesize="14K" date="2021-06-07 14:40" source="John Doe" destination="5555512345"/>
<recording target_name="10 Digit Dialing" filename="AT1_ID1_TT3_ID6-1623091227.5544.WAV" filesize="43K" date="2021-06-07 14:41" source="John Doe" destination="5555512345"/>
<recording target_name="10 Digit Dialing" filename="AT1_ID1_TT3_ID6-1623091276.5546.WAV" filesize="31K" date="2021-06-07 14:41" source="John Doe" destination="5555512345"/>
</day_index>
</recording_index>
</all>
"@
$XML = [XML]$XML
# This is the real work:
$xml.all.recording_index.day_index.recording |
ForEach-Object{
$Date = (Get-Date $_.Date).ToString('MM-dd-yyyy')
$Newname = "{0}_{1}_{2}.{3}" -f $Date, $_.source, $_.destination, $_.filename.Split('.')[-1]
$Newname
Rename-Item -Path $_.filename -NewName $Newname
}
For each XML element you're interested in you're just taking the properties and concatenating them to get the new name, then just execute the rename with Rename-Item
. I'm a little skeptical about the file locations as the full path of the original files isn't in the XML. You may need to run from or CD to the directory with the files.
I'm also concerned about naming collisions. I can't tell if that's a function of this being merely sample data, if is a problem you may need to get more granular with the time stamp to ensure unique file names.