在Windows中将多个文件的前缀重命名或删除为每个文件的编号 [英] Rename or remove prefix for multiple files to each ones' number in Windows

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

问题描述

我正在尝试更改当前文件夹中的所有文件名,并且尝试通过删除文件前缀(每个文件都有一个通用前缀)或将其名称更改为计数(如果有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"

该批处理文件将重命名您当前目录中的文件.除非批处理文件存在于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.

这篇关于在Windows中将多个文件的前缀重命名或删除为每个文件的编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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