简体   繁体   English

Powershell 多个文件名中的偏移数由一个常数

[英]Powershell offset number in name of multiple files by a constant

I have multiple files from camera with names "00010001", "00010002", etc. Those are multiple file types (JPG, CR2, xmp, MOV) in one folder (lets say C:\camera).我有来自相机的多个文件,名称为“00010001”、“00010002”等。这些是一个文件夹中的多种文件类型(JPG、CR2、xmp、MOV)(比如说 C:\camera)。

I need to add 10000 to number in all file names, so it becomes "00020001", "00020002", etc. I guess this could be done with a simple script in powershell, but I have absolute no experience with it.我需要在所有文件名中添加 10000,所以它变成“00020001”、“00020002”等。我想这可以通过 powershell 中的简单脚本来完成,但我绝对没有经验。 I would be very grateful if someone helped me with it.如果有人帮助我,我将不胜感激。 Thank you.谢谢你。

If the code should also handle files in subfolders of 'C:\Camera', you can use the -Recurse and -Include parameters:如果代码还应处理“C:\Camera”子文件夹中的文件,则可以使用-Recurse-Include参数:

(Get-ChildItem -Path 'C:\Camera' -File -Include '*.JPG','*.CR2','*.XMP','*.MOV' -Recurse) | 
    Where-Object { $_.BaseName -match '\d{8}' } | 
    Rename-Item -NewName { '{0:D8}{1}' -f ([int]$_.BaseName + 10000), $_.Extension } -WhatIf

If however this should NOT go any deeper that the root folder 'C:\Camera', then do this:但是,如果这不应该 go 比根文件夹“C:\Camera”更深,那么执行以下操作:

(Get-ChildItem -Path 'C:\Camera' -File) | 
    Where-Object { $_.BaseName -match '\d{8}' -and $_.Extension  -match '\.(JPG|CR2|XMP|MOV)$' } | 
    Rename-Item -NewName { '{0:D8}{1}' -f ([int]$_.BaseName + 10000), $_.Extension } -WhatIf

Once you are satisfied the code would create the new filenames as you wish, remove the safety switch -WhatIf一旦您满意,代码将根据需要创建新文件名,请移除安全开关-WhatIf

Explanation:解释:

  • Get the files in the folder that have BaseNames consisting of 8 digits ($_.BaseName -match '\d{8}' ) and that have an extension of either JPG , .CR2 , .XMP or .MOV获取文件夹中 BaseNames 由 8 位数字组成的文件($_.BaseName -match '\d{8}' ) 并且扩展名为JPG.CR2.XMP.MOV
  • Convert that BaseNaem into an interger number and add 10000将该 BaseNaem 转换为整数并加上 10000
  • Reassemble the file name with the -f format operator, where the template string {0:D8}{1} makes sure all (new) numbers are prefixed with zeroes up to 8 digits使用-f格式运算符重新组合文件名,其中模板字符串{0:D8}{1}确保所有(新)数字都以零为前缀,最多 8 位

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在多个文件中搜索字符串并在 Excel 或 Powershell 中的 csv 中返回带有行号/文本的文件名 - How to search a string in multiple files and return file name with line number/text in an Excel or csv in Powershell 使用Powershell计数具有特定名称的子文件夹中的文件数 - Using powershell to count number of files in subfolder with specific name 使用powershell重命名名称中间带有递增数字的文件 - Renaming files with incremented number in the middle of the name using powershell 根据文件名中的奇数移动文件 - powershell - Move files based on odd number in file name - powershell Powershell月份名称到编号 - Powershell month name to number PowerShell - xml 文件在同一元素名称上具有冲突的多个命名空间 - PowerShell - xml files with conflicting multiple namespaces on the same element name 查找多个 csv 文件中的公共服务器数量 powershell - find number of common servers count in multiple csv files powershell PowerShell:旋转多个图像文件并将其保存到新名称的目录中 - PowerShell: Rotate multiple image files and save them to a directory with new name 将多个文件移动到 PowerShell 中另一个磁盘中的匹配名称文件 - Moving multiple files to matching name ones in another disk in PowerShell Powershell 按名称对文件进行分组 - Powershell Group files by name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM