使用批处理文件在文件名中添加前缀 [英] Add prefix to filenames using batch files

查看:259
本文介绍了使用批处理文件在文件名中添加前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用以下命令在一系列文本文件中添加前缀:

I can add a prefix to a series of text files using:

:: rename files
for %%a in (*.txt) do (
ren "%%a" "Seekret file %%a"
:: ECHO %%a Seekret file %%a
)

这将变成

 a.txt
 b.txt
 c.txt

进入

 Seekret file a.txt
 Seekret file b.txt
 Seekret file c.txt

但是,上面的代码似乎用前缀将第一个文件重命名了两次.我最终得到

However, the above code seems to rename the first file twice with the prefix. I end up with

Seekret文件Seekret文件a.txt

,我不知道为什么.有什么想法吗?

and I have no idea why. Any ideas?

推荐答案

是的,可能会发生这种情况,尤其是在FAT32和exFAT驱动器上,因为这些文件系统不会将通配符模式匹配的目录项列表返回给调用可执行文件按字母顺序排列. for依次处理与*.txt匹配的目录条目,命令ren导致目录条目的更改,即在迭代文件名列表的同时对其进行修改.

Yes, this can happen, especially on FAT32 and exFAT drives because of these file systems do not return the list of directory entries matched by a wildcard pattern to calling executable in an alphabetic order. for processes the directory entries matching *.txt one after the other and the command ren results in changing the directory entries, i.e. the file names list is modified while iterating over it.

该解决方案正在使用:

for /F "eol=| delims=" %%I in ('dir *.txt /A-D /B 2^>nul') do ren "%%I" "Seekret file %%I"

在这种情况下,

FOR 在后台%ComSpec% /c中运行,并在'之间指定命令行,这意味着Windows安装在目录C:\ Windows:

FOR runs in this case in background %ComSpec% /c with the command line specified between ' which means with Windows installed into directory C:\Windows:

C:\Windows\System32\cmd.exe /C dir *.txt /A-D /B 2>nul

因此,在后台启动了另一个命令过程,该过程执行 DIR ,其中

So one more command process is started in background which executes DIR which

  • 在当前目录中搜索
  • 仅由于选项/A-D(属性而不是目录)而仅用于文件
  • 包括设置了隐藏属性的文件(使用/A-D-H排除隐藏文件)
  • 匹配通配符模式*.txt
  • 由于选项/B,仅以裸格式输出文件名.
  • searches in current directory
  • just for files because of option /A-D (attribute not directory)
  • including files with hidden attribute set (use /A-D-H to exclude hidden files)
  • matching the wildcard pattern *.txt
  • and outputs in bare format just the file names because of option /B.

DIR 输出的错误消息,用于处理 STDERR ,如果找不到与这些条件匹配的目录条目,可以通过将其重定向到设备 NUL 来抑制该错误消息strong>.

An error message output by DIR to handle STDERR in case of not finding any directory entry matching these criteria is suppressed by redirecting it to device NUL.

阅读有关使用命令重定向运算符以获取2>nul的解释.当Windows命令解释器在执行命令 FOR 之前处理此命令行时,重定向操作符>必须在 FOR 命令行上转义字符^进行转义,以将其解释为文字字符. >,它将在后台启动的单独命令过程中执行嵌入的dir命令行.

Read the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line in a separate command process started in background.

没有路径的文件名由 DIR 输出,以处理后台命令过程的 STDOUT .该输出分别由 FOR 和执行批处理文件的命令进程捕获.

The file names without path are output by DIR to handle STDOUT of background command process. This output is captured by FOR respectively the command process executing the batch file.

启动命令过程本身终止后, FOR 处理捕获的文件名列表.由于这个原因,在循环迭代期间对目录所做的所有更改都不再重要了.文件名列表不再更改.

After started command process terminated itself, FOR processes the captured list of file names. All changes done on directory during the loop iterations do not matter anymore for that reason. The file names list does not change anymore.

需要选项eol=| delims=才能获得一个完整的文件名,即使以;开头或包含空格字符也要依次分配给循环变量I. eol=|将默认的行尾字符;重新定义为任何文件名都不能包含的竖线. delims=定义了一个空的定界符列表,以禁用普通空格和水平制表符上的默认行拆分行为.

The options eol=| delims= are needed to get the complete file names assigned one after the other to loop variable I even on starting with ; or containing a space character. eol=| redefines default end of line character ; to a vertical bar which no file name can contain. delims= defines an empty list of delimiters to disable default line splitting behavior on normal spaces and horizontal tabs.

注意:::是无效标签,而不是注释.不允许在命令块内添加标签,并且在执行该命令块时通常会导致未定义的行为.使用命令 REM (备注).

Note: :: is an invalid label and not a comment. Labels inside a command block are not allowed and usually result in undefined behavior on execution of the command block. Use command REM (remark) for a comment.

更好的是:

for /F "eol=| delims=" %%I in ('dir *.txt /A-D /B 2^>nul ^| %SystemRoot%\System32\findstr.exe /B /I /L /V /C:"Seekret file "') do ren "%%I" "Seekret file %%I"

FINDSTR 在此处用于从 DIR 输出的文件名列表中输出,并重定向到 FINDSTR STDIN >所有文件名

FINDSTR is used here to output from list of file names output by DIR and redirected to STDIN of FINDSTR all file names which

  • 由于/V(反转结果)而
  • 开始,因为选择了/B
  • 不区分大小写,因为有选项/I
  • 具有字面意义的解释是由于选项/L(对于/C:而言是多余的)
  • 字符串Seekret file .
  • do not because of /V (inverted result)
  • begin because of option /B
  • case-insensitive because of option /I
  • with the literally interpreted because of option /L (redundant to /C:)
  • string Seekret file .

选项/C:来指定包含两个空格的搜索字符串,因为仅使用"Seekret file"会导致在行首直接搜索Seekretfile且不区分大小写.在仅用"..."指定的搜索字符串中, FINDSTR 将每个空格解释为一个Perl正则表达式字符串中的|之类的OR表达式.

Option /C: is needed to specify the search string containing two spaces as using just "Seekret file" would result in searching literally and case-insensitive for either Seekret OR file at begin of a line. In a search string specified with just "..." each space is interpreted by FINDSTR as an OR expression like | in a Perl regular expression string.

/C:指定的搜索字符串被隐式解释为原义字符串,但是使用/R(而不是/L),可以将该字符串解释为正则表达式字符串,并在其上解释一个空格作为空间而不是OR表达式.可以多次使用/C:指定多个搜索字符串.

A search string specified with /C: is interpreted implicitly as literal string, but with using /R (instead of /L) it would be possible to get this string interpreted as regular expression string on which a space is interpreted as space and not as OR expression. It is possible to specify multiple search strings using multiple times /C:.

我对使用 FINDSTR 的建议:始终使用/L/R来明确 FINDSTR 以及命令行的每个读者如何 FINDSTR 应该解释用"..."/C:"..."指定的搜索字符串.

My recommendation on using FINDSTR: Use always either /L or /R to make it clear for FINDSTR and for every reader of the command line how FINDSTR should interpret the search string(s) specified with "..." or with /C:"...".

这篇关于使用批处理文件在文件名中添加前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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