Batch script to remove parts of a filename

2019-08-14 03:43发布

Help please!

I need to remove time stamp and file number off of the filename using a windows batch script. Thank you! Thank you in advance!

OLD Filename= CAM168-NOTSET.2013.10.01.18.45.45.882.jpg

NEW Filename= CAM168-NOTSET.jpg

3条回答
▲ chillily
2楼-- · 2019-08-14 04:36

If by "remove time stamp and file number", you mean remove everything between the first and last dot, preserving the extension, then you don't need a batch script. A single one liner will do.

This command will rename all .jpg files in the current directory. It will support final names up to length 50, excluding date, number, and extension. Add additional ? if you have longer names, or you can remove some ? if your names are shorter.

for %F in (*.jpg) do @ren "%F" "??????????????????????????????????????????????????%~xF"

Double up the percents if you use the command in a batch file.

You can put any file mask, or list of files, etc in the in () clause.

If you are just going to rename .jpg files, then you can simply use:

ren *.jpg ??????????????????????????????????????????????????.jpg

Refer to How does the Windows RENAME command interpret wildcards? for info on why this works.


Lloyd has an interesting solution that will only rename files that match a template of
name.number.[number.]...ext, expressed as regex. But it requires installation of a third party executable. The same specificity can be achieved easily with pure script using a hybrid JScript/batch utility called REPL.BAT.

Assuming REPL.BAT is in your current directory, or better yet, somewhere within your PATH, then the following will match all files that match the regex template:

for /f "eol=* delims=* tokens=1,2" %A in (`dir /b /a-d^|repl "(.*?)(\.\d+)+(\.\w+) a" "$&*$1$3"') do ren "%A" "%B"

Double up the percents if used within a batch script.

查看更多
ら.Afraid
3楼-- · 2019-08-14 04:36

using renamer (cross-platform file renamer) with these input files:

CAM168-NOTSET.2013.10.01.18.45.45.882.jpg
CAM169-NOTSET.2013.10.01.18.45.45.883.gif
CAM170-NOTSET.2013.10.01.18.45.45.883.jpeg

and this command:

$ renamer --find '/(.*?)(\.\d+)+(\.\w+)/' --replace '$1$3' *

produces these filenames:

CAM168-NOTSET.jpg
CAM169-NOTSET.gif
CAM170-NOTSET.jpeg
查看更多
唯我独甜
4楼-- · 2019-08-14 04:50
@ECHO OFF
SETLOCAL
SET "fname=CAM168-NOTSET.2013.10.01.18.45.45.882.jpg"
FOR %%i IN ("%fname%") DO FOR /f "delims=." %%j IN ("%%i") DO ECHO REN "%%~i" "%%~j%%~xi"
GOTO :EOF

New name merely ECHOed.

查看更多
登录 后发表回答