重命名批处理文件并保留部分文件名 [英] Renaming a batch file and keeping part of the filename

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

问题描述

在类似的请求上,我已经看到很多帖子,但是我找不到适合我想要做的事情的帖子.这很简单,但我似乎无法理解.

I've seen plenty of posts on similar requests but I can't quite find one that suits what I am trying to do. It is fairly simple but I cannot seem to get it.

ren FILE??.txt FILE%Year%%Month%%Day%??.txt


copy FILE%Year%%Month%%Day%??.txt C:\Users\me\Desktop\Batch\renamed\achod%Year%%Month%%Day%??.txt

我无法获取保留'??'的脚本代表第一个文件可能具有的随机字符.

I cannot get the script to keep the '??' which represents random characters the first file may have.

感谢您的帮助.

推荐答案

您将无法使用通配符直​​接重命名文件.相反,您需要找到所有适用的文件,然后重命名每个文件.

You won't be able to rename files directly using a wildcard character. Instead you need to locate all the applicable files and then rename each.

以下脚本在您的问题/评论的假设下起作用:

The script below works under the assumptions of your question/comments:

  • 文件名长度为6个字符.
  • 只有最后两个字符可以互换.

当然,可以很容易地将脚本修改为适应其他设置,但这确实符合您的要求.

Of course, the script could be very easily adapted to accomodate other settings but this does just as you requested.

SETLOCAL EnableDelayedExpansion

REM Set your Year, Month, Day variable values here.
REM They will be used for file renaming.
...

CD "C:\Path\To\Files"

FOR /F "usebackq tokens=* delims=" %%A IN (`DIR "File??.txt" /B /A:-D`) DO (
    REM Extract the last 2 chars of the file name.
    SET FileName=%%~nA
    SET First4=!FileName:~0,4!
    SET Last2=!FileName:~-2!

    REM Rename the file, inserting the new data.
    RENAME "%%A" "!First4!%Year%%Month%%Day%!Last2!%%~xA"
)
ENDLOCAL

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆