auto renaming batch file

五迷三道 提交于 2019-12-12 01:04:56

问题


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



来源:https://stackoverflow.com/questions/21691158/auto-renaming-batch-file

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