Bash/批处理多个文件、单个文件夹、增量重命名脚本;用户提供的文件名前缀参数
我有一个需要重命名的文件文件夹。
我需要首先提供一个命名约定,然后递增以确保文件夹内文件名的完整性,而不是简单的递增数字重命名函数。
假设我有文件:
wei12346.txt
wifr5678.txt
dkgj5678.txt
需要重命名为:
Eac-345-018.txt
Eac-345-019.txt
Eac-345-020.txt
每次运行脚本时,命名可能不同,随之而来的数字增量也可能不同:
Ebc-345-010.pdf
Ebc-345-011.pdf
Ebc-345-012.pdf
所以我需要向用户请求提供的参数,我认为这可能有用,因为要索引的文件列表中的前一个文件名例如:Eac-345-017.txt
我不确定增量的另一件事是脚本将如何处理增量 099到 100 或 999 到 1000,因为我不知道这个过程是如何进行的。
有人告诉我这是 Perl 中的一个简单脚本,但是我在工作中的 Windows 计算机上运行 cygwin,并且只能访问 bash 和 Windows shell 才能执行该脚本。
任何让我继续前进的指示将不胜感激,我有一些编程经验,但脚本编写几乎是全新的。
谢谢, 克雷格
(我知道已经有很多关于此类事情的帖子,但似乎没有一个提供任何简洁的答案,因此我的问题)
I have a folder of files which need to be renamed.
Instead of a simple incrimental numeric rename function I need to first provide a naming convention which will then incriment in order to ensure file name integrity within the folder.
say i have files:
wei12346.txt
wifr5678.txt
dkgj5678.txt
which need to be renamed to:
Eac-345-018.txt
Eac-345-019.txt
Eac-345-020.txt
Each time i run the script the naming could be different and the numeric incriment to go along with it may also be ddifferent:
Ebc-345-010.pdf
Ebc-345-011.pdf
Ebc-345-012.pdf
So i need to ask for a provided parameter from the user, i was thinking this might be useful as the previous file name in the list of files to be indexed eg: Eac-345-017.txt
The other thing I am unsure about with the incriment is how the script would deal with incrimenting 099 to 100 or 999 to 1000 as i am not aware of how this process is carried out.
I have been told that this is an easy script in perl however I am running cygwin on a windows machine in work and have access to only bash and windows shells in order to execute the script.
Any pointers to get me going would be greatly appreciated, i have some experience programming but scripting is almost entirely new.
Thanks,
Craig
(i understand there are allot of posts on this type of thing already but none seem to offer any concise answer, hence my question)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
保存文件,像这样调用它:
注意:在您的示例中,您将
.txt
“重命名”为.pdf
,但上面我假设扩展名将保持不变。如果您真的只想更改扩展名,那么这将是一个微不足道的更改。如果您想实际转换文件格式,那么它会稍微复杂一些。另请注意,我已使用
%03d
格式化递增数字。这意味着您的数字序列将是例如,这意味着它将被零填充到三个位置,但如果数字更大,则会自动溢出。如果您希望保持一致性(始终为 4 位数字),则应将填充更改为
%04d
。Save the file, invoke it like this:
Note: In your example you "renamed" a
.txt
to a.pdf
, but above I presumed the extension would stay the same. If you really wanted to just change the extension then it would be a trivial change. If you wanted to actually convert the file format then it would be a little more complex.Note also that I have formatted the incrementing number with
%03d
. This means that your number sequence will be e.g.Meaning that it will be zero padded to three places but will automatically overflow if the number is larger. If you prefer consistency (always 4 digits) you should change the padding to
%04d
.好的,您可以执行以下操作。您可以先询问用户前缀,然后询问起始序列号。然后,您可以使用 bash 中的内置 printf 对数字进行正确的格式化,但您可能必须决定提供足够的数字宽度来容纳所有数字顺序,因为这将导致更同质的名称。您可以使用
read
读取用户输入:注意:您也可以提取扩展名。那不是问题。另外,这里我选择了 4 个数字作为序列号,计算到变量
$fp
中。OK, you can do the following. You can ask the user first the prefix and then the starting sequence number. Then, you can use the built-in
printf
frombash
to do the correct formatting on the numbers, but you may have to decide to provide enough number width to hold all the sequence, because this will result in a more homogeneous names. You can useread
to read user input:Note: You can extract the extension also. That wouldn't be a problem. Also, here I selected 4 numbers fot the sequence number, calculated into the variable
$fp
.