自动重命名批处理文件

自动重命名批处理文件

本文介绍了自动重命名批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个批处理文件,该文件将从文件名中去除前四个字符,然后转储其余的文件.因此,基本上我可能会有文件"1234_whatever.txt",并且我需要将其重命名为"1234.txt".文件名的格式始终相同,但是文件名始终会更改.最重要的是,我需要这一切都是自动的,并且无需用户干预.任何帮助将不胜感激

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

推荐答案

@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

您需要将 sourcedir 的设置更改为您的目录名.

You'd need to change the setting of sourcedir to your directoryname.

所需的REN命令仅被 ECHO 用于测试目的.确认命令正确无误后,将 ECHO REN 更改为 REN ,以实际重命名文件.

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.

请注意,长文件名的SHORT文件名通常会包含下划线,并且会被 dir 命令击中.因此,对于 if not"%% c ==""

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==""

如果只希望处理 .txt 文件,则可能希望将 * _ *.* 更改为 * _ *.txt .

You may wish to change *_*.* to *_*.txt if you only wish to process .txt files.

这篇关于自动重命名批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 12:40