重命名(或删除前缀)多个文件至每个数字 [英] Rename (or remove prefix) multiple files to each ones number

查看:529
本文介绍了重命名(或删除前缀)多个文件至每个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更改当前文件夹中的所有文件名,并且尝试通过删除文件前缀(每个文件都有一个通用前缀)或更改其名称为计数(如果有5个文件)来实现此目的,文件名将为1.txt 2.txt 3.txt 4.txt 5.txt)。

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).

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

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.

我知道了,但只添加了一个前缀:

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

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

我尝试做相反的事情:

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.

推荐答案

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

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

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

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

set "fname="

下一步是实际执行重命名的命令。如果已经定义了fname,它将无法正常工作。

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

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

为每次迭代定义fname变量,然后语法%fname:* prefix =%用任何内容替换第一次出现的 prefix。棘手的事情是Windows在首次分析命令时首先尝试扩展%fname%。当然,这是行不通的,因为尚未定义。如果找不到该变量,则在命令行上保留百分比。 CALL会导致在设置变量后 后发生额外的扩展阶段,因此扩展有效。

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 after the variable has been set, so the expansion works.

如果在运行前定义了fname

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.

如果要运行命令,则每次迭代时都将尝试重命名该文件,而不是在循环中分配该值。再次使用不同的前缀,您将必须首先再次清除定义。

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

编辑-这是一个名为 RemovePrefix.bat的批处理文件。

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

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

假设您的文件名为 prefixName.txt ,那么您将通过执行

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

RemovePrefix  "prefix"  "*.txt"

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

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.

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

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.

这篇关于重命名(或删除前缀)多个文件至每个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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