PowerShell 使用 XML 重命名文件
PowerShell rename files with XML
目前我有一个文件夹 C:\Call Archive\
,文件名 XML index.xml
和一大堆 .wav
文件,这些文件的名称在您查看时没有任何意义在它。像 AT1_ID1_TT3_ID6-1623089222.5534.WAV
这样的名字
我想做的是采用 XML 结构并根据 XML.
中的标签重命名每个 .wav
文件
源数据:
<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>
理想的文件名应该是06-07-2021_John Doe_5555512345.wav
。请注意,如果可能,日期将采用 MM-DD-YYYY
格式。
任何事情都会有所帮助!
您只需要遍历并进行重命名。如下所示:
# 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
}
对于您感兴趣的每个 XML 元素,您只需获取属性并将它们连接起来以获得新名称,然后只需使用 Rename-Item
执行重命名。我对文件位置有点怀疑,因为原始文件的完整路径不在 XML 中。您可能需要 运行 从或 CD 到包含文件的目录。
我也担心命名冲突。我不知道这是否只是示例数据的一个功能,如果是一个问题,您可能需要更细化时间戳以确保唯一的文件名。
目前我有一个文件夹 C:\Call Archive\
,文件名 XML index.xml
和一大堆 .wav
文件,这些文件的名称在您查看时没有任何意义在它。像 AT1_ID1_TT3_ID6-1623089222.5534.WAV
我想做的是采用 XML 结构并根据 XML.
中的标签重命名每个.wav
文件
源数据:
<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>
理想的文件名应该是06-07-2021_John Doe_5555512345.wav
。请注意,如果可能,日期将采用 MM-DD-YYYY
格式。
任何事情都会有所帮助!
您只需要遍历并进行重命名。如下所示:
# 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
}
对于您感兴趣的每个 XML 元素,您只需获取属性并将它们连接起来以获得新名称,然后只需使用 Rename-Item
执行重命名。我对文件位置有点怀疑,因为原始文件的完整路径不在 XML 中。您可能需要 运行 从或 CD 到包含文件的目录。
我也担心命名冲突。我不知道这是否只是示例数据的一个功能,如果是一个问题,您可能需要更细化时间戳以确保唯一的文件名。