Batch script to remove parts of a filename

自闭症网瘾萝莉.ら 提交于 2019-12-13 01:25:52

问题


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


回答1:


@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.




回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/19148942/batch-script-to-remove-parts-of-a-filename

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!