Dos批处理文件重命名

发布于 2024-10-15 19:05:27 字数 441 浏览 1 评论 0原文

我有一组这样命名的文件(单词可以是任何单词或数字):

Word word-word word word Floor B2342 Word Word-word.pdf
Word word-word word Floor: B-2342 Word Word-word.pdf
Word word- Floor C43 Word Word.pdf
Word word word- Floor- E2AD342 Word Word.pdf

我想重命名文件夹中的所有内容,只包含 Floor 后面的组...您可以指望 Floor 始终位于文件中名称以及我想保留的内容如下楼层。

B2342.pdf
B-2342.pdf
C43.pdf
E2AD342.pdf

一个衬垫会很好,但也可以更多,因为它将位于 .bat 文件中。主要关注的是稳健性。

I have a group of files that are named as such (word can be any word or numbers):

Word word-word word word Floor B2342 Word Word-word.pdf
Word word-word word Floor: B-2342 Word Word-word.pdf
Word word- Floor C43 Word Word.pdf
Word word word- Floor- E2AD342 Word Word.pdf

I want to rename everything in the folder to only have the group that follows Floor... You can count on Floor always being in the file name and what I want to keep following floor.

B2342.pdf
B-2342.pdf
C43.pdf
E2AD342.pdf

A one liner would be nice but it can be more since it will be in a .bat file. The main concern is being robust.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

絕版丫頭 2024-10-22 19:05:27

如果还不算太晚的话,你能尝试一下我的这个小批处理脚本吗?它处理当前目录中的所有 .pdf 文件。如果名称没有“Floor”部分,脚本将跳过该文件。

:@ECHO OFF
FOR %%a IN (*.pdf) DO CALL :RenameFile "%%a" %%~na
GOTO :EOF


:RenameFile
SET oldname=%1
SET newname=
SET floored=0

:loop
IF %2.==. GOTO :endloop
IF %floored%==1 SET newname=%2& GOTO :endloop
SET tmp=%2
IF /I %tmp:~0,5%==Floor SET floored=1
SHIFT
GOTO :loop
:endloop

IF NOT %newname%.==. COPY %oldname% %newname%.*

您可能已经注意到令人惊讶的 COPY 命令而不是 RENAME。它是按设计而存在的。我只是想让你先确定一切是否顺利。不过我已经测试过了,这个脚本似乎没有任何问题。

只有一件事。您的示例名称之一包含冒号字符,这在文件名中是非法的。我在测试期间已将其替换为“#”。

If it isn't too late, will you try this out little batch script of mine? It processes all .pdf files in the current directory. If a name has no 'Floor' part, the script just skips the file.

:@ECHO OFF
FOR %%a IN (*.pdf) DO CALL :RenameFile "%%a" %%~na
GOTO :EOF


:RenameFile
SET oldname=%1
SET newname=
SET floored=0

:loop
IF %2.==. GOTO :endloop
IF %floored%==1 SET newname=%2& GOTO :endloop
SET tmp=%2
IF /I %tmp:~0,5%==Floor SET floored=1
SHIFT
GOTO :loop
:endloop

IF NOT %newname%.==. COPY %oldname% %newname%.*

You may have noticed the surprising COPY command instead of RENAME. It is there by design. I would just like you to make sure first if everything's going to be smooth. I've tested it, though, and there doesn't seem to be anything wrong with this script.

Only one thing. One of your sample names contains the colon character, which is illegal in file names. I've replaced it with '#' during my tests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文