auto renaming batch file

2019-09-01 23:45发布

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条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-09-02 00:36
@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 ECHOed 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.

查看更多
登录 后发表回答