批量重命名替换为部分原始文件名 DOS 和 Bash

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11737170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:29:42  来源:igfitidea点击:

Batch renaming replace with part of the original file name DOS and Bash

bashdosbatch-rename

提问by stackmeistergeneral

I know this has been covered many times, but i still can't seem to get a good solution to this particular aspect. I have the same problem in both bash and DOS. I have a file of many .csv files named abcYYMMDDf.csv and I want to change the file name to the YYMMDD part which corresponds to the date it was created. Note the "f" after the YYMMDD, so really i would like an equivalent of excel's mid() to take the middle date.

我知道这已经被多次覆盖,但我似乎仍然无法找到这个特定方面的好的解决方案。我在 bash 和 DOS 中都有同样的问题。我有一个包含许多 .csv 文件的文件,名为 abcYYMMDDf.csv,我想将文件名更改为与创建日期相对应的 YYMMDD 部分。注意 YYMMDD 后面的“f”,所以我真的想要一个相当于 excel 的 mid() 来取中间日期。

In bash I understand the rename function, and the * wildcard I tried variations on

在 bash 中,我了解重命名功能,以及我尝试变体的 * 通配符

rename abc*f.csv *.csv abc*

thinking that the * would stand in for YYMMDD but obviously it didn't. I saw another method involving selecting s/([a-z]*) but that would just take the whole file name surely?

认为 * 会代表 YYMMDD 但显然它没有。我看到了另一种涉及选择 s/([az]*) 的方法,但这肯定会取整个文件名吗?

In DOS I tried

在DOS中我试过

ren abc*f.csv *.csv

回答by chepner

Bash parameter expansion can be used to strip the 'abc' and the 'f' in two steps, using prefix removal and pattern substitution, respectively.

Bash 参数扩展可用于分两步去除 'abc' 和 'f',分别使用前缀删除和模式替换。

for f in abc*f.csv; do
    new_f=${f#abc}
    new_f=${new_f/%f.csv/.csv}
    mv $f $new_f
done

You could use sedin place of parameter expansion to remove 'abc' and 'f' in one step:

您可以使用sed代替参数扩展一步删除“abc”和“f”:

for f in abc*f.csv; do
    new_f=$(echo $f | sed 's/abc\(.*\)f//')
    mv $f $new_f
done

I cannot answer the DOS version; you may be better off asking that in a separate question.

我无法回答DOS版本;你最好在一个单独的问题中提出这个问题。

回答by dbenham

For the "DOS" part of the question: Presuming you are on Windows using a command prompt (cmd.exe), and not on true DOS using command.com

对于问题的“DOS”部分:假设您使用命令提示符(cmd.exe)在 Windows 上,而不是在使用 command.com 的真正 DOS 上

@echo off
setlocal enableDelayedExpansion
for %%F in (abc*f.csv) do (
  set "name=%%F"
  ren "%%F" "!name:~1,6!.csv"
)


Update


更新

The rules for how REN works with wildcards can be found at How does the Windows RENAME command interpret wildcards?. Unfortunately, the rules don't help in this situation.

REN 如何使用通配符的规则可以在 Windows RENAME 命令如何解释通配符中找到?. 不幸的是,规则在这种情况下无济于事。

回答by Valentin Huber

For the bash part (with two steps):

对于 bash 部分(分两步):

rename abc "" abc*
rename f.csv .csv *f.csv

should work, since you want to substitute the "abc" part with the empty string (i.e., delete the abc part). With rename you have to specify only the part you would like to replace, not the whole filename (see the man page of rename for details).

应该可以工作,因为您想用空字符串替换“abc”部分(即删除 abc 部分)。使用重命名,您只需指定要替换的部分,而不是整个文件名(有关详细信息,请参阅重命名的手册页)。

The second step takes care of removing the trailing 'f'.

第二步负责删除尾随的“f”。

For the DOS part:

对于 DOS 部分:

I don't think you'll be able to solve that with rename only. It might be worthwhile to look at some Powershell commands...

我认为您无法仅通过重命名来解决该问题。看看一些 Powershell 命令可能是值得的......

回答by drunkenvash

I have a solution for DOS, You can create the strings you need in excel then copy and paste them into the command line. It will automatically execute every line that you copied into the command line.

我有一个DOS解决方案,您可以在excel中创建您需要的字符串,然后将它们复制并粘贴到命令行中。它将自动执行您复制到命令行中的每一行。

REN "folderpath\filename" "newName"

REN "文件夹路径\文件名" "newName"

REN "C:\Users\Charlie\Desktop\_Working General\L2795187.HH293.11.xml.csv" "HH293-11.csv"

I created the above in excel with formulas and just pasted it into cmd. A cell for every single file I want to rename in column A and it worked perfectly.

我用公式在excel中创建了上面的内容,然后将其粘贴到cmd中。我想在 A 列中重命名每个文件的单元格,它工作得很好。