windows 重命名(或删除前缀)多个文件到每个编号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10202540/
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 09:28:03  来源:igfitidea点击:

Rename (or remove prefix) multiple files to each ones number

windowscmdrenaming

提问by Shadow

I am trying to change all the files names in a current folder and I am trying to achieve this either by removing the files prefix (every file has a common prefix) or changing their names to their count (if there are 5 files, the filenames will be 1.txt 2.txt 3.txt 4.txt 5.txt).

我正在尝试更改当前文件夹中的所有文件名,我试图通过删除文件前缀(每个文件都有一个公共前缀)或将它们的名称更改为它们的计数(如果有 5 个文件,文件名将是 1.txt 2.txt 3.txt 4.txt 5.txt)。

Now I have found the ren command in cmd and played with it a little but so far I was not able to achieve the result and I can only run this in cmd, so no batch files can be used.

现在我已经在 cmd 中找到了 ren 命令并使用了它,但到目前为止我无法实现结果,我只能在 cmd 中运行它,所以不能使用批处理文件。

This is the closest that I got, but it only adds a prefix:

这是我得到的最接近的,但它只添加了一个前缀:

FOR %f IN (*.*) DO ren %f prefix%f

I have tried doing the opposite:

我试过做相反的事情:

FOR %f IN (*.*) DO ren prefix%f %f

But of course, didn't work so I am now asking for help and some explanation if possible (I like to understand how these things work). Thanks in advance for any help.

但是,当然,没有用,所以如果可能的话,我现在寻求帮助和一些解释(我喜欢了解这些东西是如何工作的)。在此先感谢您的帮助。

回答by Alnair

A much simpler solution is provided in
https://superuser.com/a/871799/497147

https://superuser.com/a/871799/497147 中提供了一个更简单的解决方案

I copied it here for easier access.

我把它复制到这里以便于访问。

Forget about complicated scripts for this.

renameis a very old and never properly completed command. If you do not use it properly, the result might surprise you.

For example to remove a prefix abcdfrom abcd1.txt, abcd2.txt, abcd3.txtetc. in order to get 1.txt, 2.txt, 3.txtsimply use

rename "abcd*.txt" "////*.txt"

You need the same number of /as the number of initial characters you would like to remove.

Do place double quotes for both arguments.

忘记复杂的脚本吧。

rename是一个非常古老且从未正确完成的命令。如果您没有正确使用它,结果可能会让您感到惊讶。

例如删除前缀abcdabcd1.txtabcd2.txtabcd3.txt等为了得到1.txt2.txt3.txt简单地使用

rename "abcd*.txt" "////*.txt"

您需要与/要删除的初始字符数相同的数量。

请为两个参数放置双引号。

回答by dbenham

I don't understand why you can't use a batch file. But here is a solution that should work with most file names.

我不明白你为什么不能使用批处理文件。但这是一个适用于大多数文件名的解决方案。

Critical - first you must make sure you have an undefined variable name, I'll use fname

关键 - 首先你必须确保你有一个未定义的变量名,我将使用 fname

set "fname="

Next is the command to actually do the renaming. It won't work properly if fname is already defined.

接下来是实际进行重命名的命令。如果 fname 已经定义,它将无法正常工作。

for %a in (prefix*.txt) do @(set "fname=%a" & call ren "%fname%" "%fname:*prefix=%")

The fname variable is defined for each iteration and then the syntax %fname:*prefix=%replaces the first occurrence of "prefix" with nothing. The tricky thing is Windows first attempts to expand %fname% when the command is first parsed. Of course that won't work because it hasn't been defined yet. On the command line the percents are preserved if the variable is not found. The CALL causes an extra expansion phase that occurs afterthe variable has been set, so the expansion works.

fname 变量是为每次迭代定义的,然后语法%fname:*prefix=%将第一次出现的“前缀”替换为空。棘手的是 Windows 在第一次解析命令时首先尝试扩展 %fname% 。当然这行不通,因为它还没有被定义。如果未找到变量,则在命令行上保留百分比。CALL 导致在设置变量发生额外的扩展阶段,因此扩展工作。

If fname is defined prior to running the command, then it will simply try to rename that same file for each iteration instead of the value that is being assigned within the loop.

如果在运行命令之前定义了 fname,那么它将简单地尝试为每次迭代重命名同一个文件,而不是在循环中分配的值。

If you want to run the command again with a different prefix, you will have to first clear the definition again.

如果要使用不同的前缀再次运行该命令,则必须首先再次清除定义。

EDIT- Here is a batch file named "RemovePrefix.bat" that does the job

编辑- 这是一个名为“RemovePrefix.bat”的批处理文件,它完成了这项工作

::RemovePrefix.bat  prefix  fileMask
@echo off
setlocal
for %%A in ("%~1%~2") do (
  set "fname=%%~A"
  call ren "%%fname%%" "%%fname:*%~1=%%"
)

Suppose you had files named like "prefixName.txt", then you would use the script by executing

假设您有名为 like 的文件"prefixName.txt",那么您将通过执行来使用该脚本

RemovePrefix  "prefix"  "*.txt"

The batch file will rename files in your current directory. The batch file will also have to be in your current directory unless the batch file exists in a directory that is in your PATH variable. Or you can specify the full path to the batch file when you call it.

批处理文件将重命名当前目录中的文件。批处理文件也必须在您的当前目录中,除非批处理文件存在于 PATH 变量中的目录中。或者您可以在调用时指定批处理文件的完整路径。

The rules for expansion are different in a batch file. FOR variables must be referenced as %%A instead of %A, and %%fname%% is not expanded initially, instead the double percents are converted into single percents and then %fname% is expanded after the CALL. It doesn't matter if fname is already defined with the batch file. The SETLOCAL makes the definition of fname temporary (local) to the batch file.

批处理文件中的扩展规则不同。FOR 变量必须被引用为 %%A 而不是 %A,并且 %%fname%% 最初不会扩展,而是将双百分比转换为单百分比,然后在 CALL 之后扩展 %fname%。fname 是否已在批处理文件中定义并不重要。SETLOCAL 使 fname 的定义临时(本地)到批处理文件中。

回答by Tamer

Following is straight way to trim the prefix with renamefor all the files

以下是rename为所有文件修剪前缀的直接方法

rename -d your_prefix *