批处理脚本重命名文件前缀 [英] Batch Script Renaming File Prefix
问题描述
我有一个批处理文件,该文件本质上可以找到文件夹中的所有pdf文件,并为它们提供前缀.例如: file1.pdf
& file2.pdf
.批处理文件运行时,它将为每个文件添加一个前缀,例如"new".新-file1.pdf
&新-file2.pdf
.
I have a batch file that essentially finds all the pdf files in a folder, and gives them a prefix. SO example: file1.pdf
& file2.pdf
. When the batch file runs, it adds a prefix, let's say "new" to each file. New - file1.pdf
& New - file2.pdf
.
我不确定如何获取它来检查该文件是否已经包含此前缀,如果为true,则跳过该前缀.这是我当前的代码:
I'm unsure of how to get this to check to see if this file already contains this prefix and skip it if true. Here's my current code:
set strPrefix=New -
set fname=*.pdf
for %%f in (%fname%) Do echo Rename file "%%f" to "%strPrefix%%%f"
for %%f in (%fname%) Do ren "%%f" "%strPrefix%%%f"
推荐答案
使用DIR/B列出文件,并将结果通过管道传递给FINDSTR,以删除Aready具有前缀的文件.用FOR/F(而不是简单的FOR)处理结果.
Use DIR /B to list the files and pipe the result to FINDSTR to remove the files that aready have the prefix. Process that result with FOR /F (instead of a simple FOR).
@echo off
setlocal
set "prefix=New - "
set "mask=*.pdf"
for /f "eol=: delims=" %%F in (
'dir /b "%mask%" ^| findstr /vibc:"%prefix%"'
) do ren "%%F" "%prefix%%%F"
或者您可以使用我的 JREN.BAT正则表达式文件重命名实用程序来简化工作.它是纯脚本(混合JScript/批处理),可从XP开始在任何Windows计算机上本地运行.完整的文档嵌入在脚本中.
Or you could use my JREN.BAT regular expression file renaming utility to do things more simply. It is pure script (hybrid JScript/batch) that runs natively on any Windows machine from XP onward. Full documentation is embedded within the script.
以下命令(对于命令行)假定JREN.BAT在PATH内列出的文件夹中.
The following command (for the command line) assumes JREN.BAT is in a folder that is listed within your PATH.
jren "^" "New - " /fm "*.pdf" /fx "New - *"
如果将命令放在批处理脚本中,请使用CALL JREN.
Use CALL JREN if you put the command within a batch script.
这篇关于批处理脚本重命名文件前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!