I need to create a batch file that will strip the first four characters from a file name and dump the rest. so basically I may have file "1234_whatever.txt" and I need it to rename the file to "1234.txt" The format of the file name will always be the same however the filenames will always change. On top of that I need this to be all automatic with no user intervention. any help would be appreciated
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
@ECHO OFF
SETLOCAL
SET "sourcedir=."
FOR %%a IN ("%sourcedir%\*_*.*") DO (
FOR /f "tokens=1*delims=_" %%b IN ("%%a") DO IF NOT "%%c"=="" (
ECHO(REN "%%a" %%~nb%%~xa
)
)
GOTO :EOF
You'd need to change the setting of sourcedir
to your directoryname.
The required REN commands are merely ECHO
ed for testing purposes. After you've verified that the commands are correct, change ECHO REN
to REN
to actually rename the files.
Note that the SHORT filename for longfilenames will typically contain an underscore, and will be hit by the dir
command. Hence the requirement for the if not "%%c==""
You may wish to change *_*.*
to *_*.txt
if you only wish to process .txt
files.