[英]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:解释:
($_.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
-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.