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.
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.
精彩评论