使用连续数字后缀在 PowerShell 中批量重命名文件 [英] Bulk renaming of files in PowerShell with sequential numeric suffixes

查看:75
本文介绍了使用连续数字后缀在 PowerShell 中批量重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 14,000 张照片按年和月分类到文件中,但使用多台相机拍摄,我希望文件名反映拍摄日期.例如,1998 年 10 月 16 日的图片位于名为 1998\10 October\19981016 的文件夹中.

I have 14,000 pictures sorted into files by year and month but taken with multiple cameras and I want the file name to reflect the date taken. For example October 16, 1998 pictures are in a folder called 1998\10 October\19981016.

我希望将所有图片命名为 19981016_0001 19981016_0002 等.我尝试了多种建议,但都没有奏效.

I want the all the pictures to be named 19981016_0001 19981016_0002 etc. I tried multiple suggestions but it hasn't worked.

我可以看到它列出了我想要更改的文件夹,但我无法实际更改它.我所有的图片都是.jpg.

I can get to the point where it lists the folder I want to change but I'm unable to actually change it. All of my pictures are .jpg.

尝试我创建了一个临时副本文件,以防我搞砸了.我首先输入 cd "C:\Documents and Settings\Brooke LastName\Desktop\Temp" 然后在成功加载我的文件后,我使用了我在这个论坛上找到的公式.

Attempts I created a temp file of copies in case I messed it up. I started by typing cd "C:\Documents and Settings\Brooke LastName\Desktop\Temp" then after successfully getting my file to load I used a formula I found on this forum.

ls *jpg | Foreach {$i=1} {Rename-Item _ -NewName ("$($.19981016){0:00000000#} .jpg" -f $i++) -whatif}

我说的错误

Unexpected token ' .19981016' in expression or statement.

At line:1 char:12 + $.19981016 <<<<

错误重复了几次.

我在网上找到了几个公式,但大多数创建的文件都会用括号编号,例如 vacation (1).jpg.我想要在约会结束时在下划线后有一个四位数的计数器,例如19981016_0001

I found several formulas on the web but most created files that would number with parentheses for example vacation (1).jpg. I want a four digit counter after an underscore at the end of my date, e.g. 19981016_0001

推荐答案

语法太离谱了.几个问题:

The syntax is way off. A few issues:

  • 我不确定 $($.19981016) 打算产生什么,但是 $( ) 计算括号中的表达式并将结果插入到字符串,并且您收到错误,因为 $.19981016 不是有效的表达式.目录中的每个 .jpg 文件都会重复该错误.
  • {0:00000000#} 在格式化字符串中将创建一个由零填充的 9 位数字,但更短的方法是 {0:D9}.但是,我认为您希望用零填充的数字有 4 位数字,因此应该是 {0:0000#}{0:D4}.
  • 我不确定 Foreach {$i=1} { [...] 应该做什么.关键字foreach 可以表示foreach 循环,也可以表示Foreach-Object.在这种情况下,从管道接收输入,必然是后者,但无论哪种方式,这种语法都是不正确的.
  • I'm not sure what $($.19981016) was intended to produce, but $( ) evaluates the expression in the parentheses and interpolates the result into the string, and you're getting the error because $.19981016is not a valid expression. The error would be repeated for each .jpg file in the directory.
  • {0:00000000#} in a formatted string will create a zero-padded number of 9 digits, but a shorter way to do that is {0:D9}. However, I thought you wanted the zero-padded number to have 4 digits, so that should be {0:0000#} or {0:D4}.
  • I'm not sure what Foreach {$i=1} { [...] is supposed to do. The keyword foreach can mean a foreach loop, or it can be shorthand for Foreach-Object. In this context, receiving input from a pipeline, it's necessarily the latter, but this syntax is incorrect either way.

如果我正确理解描述,这将满足您的要求:

This will do what you want, if I understand the description correctly:

$i = 1
Get-ChildItem *.jpg | %{Rename-Item $_ -NewName ('19981016_{0:D4}.jpg' -f $i++)}

文件名将是19981016_0001.jpg19981016_0002.jpg19981016_0003.jpg

一些注意事项:

  • 你说你想要像 19981016_0001 这样的文件名,但我假设你想要保留 .jpg 扩展名.
  • 您需要初始化 $i,否则如果尚未定义,它将从 0 开始,或者更糟的是,如果之前使用过 $i,则从其他数字开始在同一个 PowerShell 会话中.例如,如果您的目录中有 1,432 个 .jpg 文件,并且您先使用 -WhatIf 运行命令,然后实际运行它,则数字将从 1432 开始.
  • $i++$i 增加 1.但是,如果您将它用作值,它会在 之后增加该值读;这就是为什么如果 $i 未定义,它将从 0 开始.
  • Get-ChildItemls 相同.我使用了本地 PowerShell 名称,但它们可以互换(lsdirgci 都是 Get-ChildItem 的别名).
  • %{ }Foreach-Object
  • 的简写
  • You said that you want filenames like 19981016_0001, but I'm assuming you want to keep the .jpg extension.
  • You need to initialize $i, otherwise it will start from 0 if it's not yet defined, or worse yet, from some other number if $i was used previously in the same PowerShell session. For example, if you have 1,432 .jpg files in the directory, and you run the command first with -WhatIf, and then run it for real, the numbers will start from 1432.
  • $i++ increments $i by 1. However, if you're using it as a value, it increments after the value is read; that's why if $i is undefined, it will start from 0.
  • Get-ChildItem is the same as ls. I used the native PowerShell name, but they're interchangeable (ls, dir, and gci are all aliases for Get-ChildItem).
  • %{ } is shorthand for Foreach-Object

这篇关于使用连续数字后缀在 PowerShell 中批量重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆