Powershell ISE - 如何复制多个文件并同时重命名它们?
Powershell ISE - How do I copy many files and rename them at the same time?
我正在尝试使用 Powershell ISE 来帮助我执行以下操作:
- 搜索某个文件夹下的多个文件(扩展名为*props.tmpl)并包括所有子目录。
- 找到后,我想将该文件复制到其当前位置,但扩展名为 *.tmpl2(我真正想要的是跳过此步骤并将 *props.tmpl 复制到名为 * 的文件道具)
- 然后重命名所有 *.tmpl2 文件并完全删除 tmpl2,只留下 *.props 扩展名。
理想情况下,我想要的是使用新名称将现有文件复制到同一目录。似乎我在 Powershell ISE 上 运行 的所有搜索都没有提供我需要的正确信息(或者我没有在寻找正确的方法 - 尝试 'powershell ise copy many files with new names' 没有帮帮忙。
我已经准备好替换件并开始工作,但我不想再删除原始的 tmpl 文件(它们是模板,所以我可能想稍后查看它们的原始内容)。
我用来替换它们的是这样的:
Get-ChildItem -Filter "*props.tmpl" -Recurse |
Rename-Item -NewName { $_.name -replace '.tmpl',''}
除了完全删除原始文件之外,效果很好。
我开始尝试拼凑一些东西,但我不明白如何正确命名副本并在此时停止,只是出现错误(这是试图跳过额外的副本,只是简单地重命名副本而不是添加“*.tmpl2”的额外步骤):
# Get all *props.tmpl files
Get-ChildItem -Filter "*props.tmpl" -Recurse |
# Iterate through each found file
ForEach-Object {
Copy-Item $_.name |
Rename-Item -NewName { $_.name -replace '.props.tmpl','.props' }
}
任何帮助将不胜感激(不是 Powershell 的人,但我正在努力学习,因为 Powershell 往往比老式批处理脚本更动态)。
提前致谢
根据@ssennett 的帮助此脚本的最终版本
这是我的最终版本:
# Get all *props.tmpl files
Get-ChildItem -Filter "*props.tmpl" -Recurse |
# Iterate through each found file and copy it to non-template form in same location
ForEach-Object {
Copy-Item $_.FullName ($_.Name -replace '.tmpl','')
}
你离答案不远了!这就是 Copy-Item 的处理方式。
在没有指定目标的情况下,Copy-Item 将有效地尝试将文件复制到自身。您可以使用 -Destination 参数处理重命名,而不是通过管道将其传递给 Rename-Item,如下所示。
$files = Get-ChildItem -Filter "*props.tmpl" -Recurse
$files | % { Copy-Item -Path $_.FullName -Destination ($_.Name -replace 'props.tmpl','.props') }
这会将名为 RandomFileprops.tmpl 的文件复制到另一个文件 RandomFile.props 中。如果要删除原始文件,可以使用具有相同参数的 Move-Item cmdlet,这会有效地重命名原始文件。
我正在尝试使用 Powershell ISE 来帮助我执行以下操作:
- 搜索某个文件夹下的多个文件(扩展名为*props.tmpl)并包括所有子目录。
- 找到后,我想将该文件复制到其当前位置,但扩展名为 *.tmpl2(我真正想要的是跳过此步骤并将 *props.tmpl 复制到名为 * 的文件道具)
- 然后重命名所有 *.tmpl2 文件并完全删除 tmpl2,只留下 *.props 扩展名。
理想情况下,我想要的是使用新名称将现有文件复制到同一目录。似乎我在 Powershell ISE 上 运行 的所有搜索都没有提供我需要的正确信息(或者我没有在寻找正确的方法 - 尝试 'powershell ise copy many files with new names' 没有帮帮忙。
我已经准备好替换件并开始工作,但我不想再删除原始的 tmpl 文件(它们是模板,所以我可能想稍后查看它们的原始内容)。
我用来替换它们的是这样的:
Get-ChildItem -Filter "*props.tmpl" -Recurse |
Rename-Item -NewName { $_.name -replace '.tmpl',''}
除了完全删除原始文件之外,效果很好。
我开始尝试拼凑一些东西,但我不明白如何正确命名副本并在此时停止,只是出现错误(这是试图跳过额外的副本,只是简单地重命名副本而不是添加“*.tmpl2”的额外步骤):
# Get all *props.tmpl files
Get-ChildItem -Filter "*props.tmpl" -Recurse |
# Iterate through each found file
ForEach-Object {
Copy-Item $_.name |
Rename-Item -NewName { $_.name -replace '.props.tmpl','.props' }
}
任何帮助将不胜感激(不是 Powershell 的人,但我正在努力学习,因为 Powershell 往往比老式批处理脚本更动态)。
提前致谢
根据@ssennett 的帮助此脚本的最终版本
这是我的最终版本:
# Get all *props.tmpl files
Get-ChildItem -Filter "*props.tmpl" -Recurse |
# Iterate through each found file and copy it to non-template form in same location
ForEach-Object {
Copy-Item $_.FullName ($_.Name -replace '.tmpl','')
}
你离答案不远了!这就是 Copy-Item 的处理方式。
在没有指定目标的情况下,Copy-Item 将有效地尝试将文件复制到自身。您可以使用 -Destination 参数处理重命名,而不是通过管道将其传递给 Rename-Item,如下所示。
$files = Get-ChildItem -Filter "*props.tmpl" -Recurse
$files | % { Copy-Item -Path $_.FullName -Destination ($_.Name -replace 'props.tmpl','.props') }
这会将名为 RandomFileprops.tmpl 的文件复制到另一个文件 RandomFile.props 中。如果要删除原始文件,可以使用具有相同参数的 Move-Item cmdlet,这会有效地重命名原始文件。