批处理作业删除带有日期和时间戳的文件名中的时间戳 [英] Batch job remove timestamp in a filename with date and time stamp

查看:184
本文介绍了批处理作业删除带有日期和时间戳的文件名中的时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个文件夹中,我有很多文件名,例如:

In a folder, I had many file names such as:

ABC.DEF.GHI.YYYYMMDDhhmmss
JKL.MNO.PQR.YYYYMMDDhhmmsss

(不确定为什么有时时间戳基于24小时会有9位数字)

(not sure why sometimes the timestamp had 9 digits, based on the 24 hours)

所需的结果是截断时间戳,仅使用YYYMMDD保留文件名:

The desired result is to truncate the timestamp, keep the filename with YYYMMDD only:

ABC.DEF.GHI.YYYYMMDD 
JKL.MNO.PQR.YYYYMMDD 

如何在脚本批处理文件Windows(非Linux)中执行此操作? 非常感谢您的帮助.

How can I do this in a script batch file, windows (not Linux)? I appreciate your help very much.

推荐答案

这是一个稍微完整些的版本,它假定您的目标文件实际上具有扩展名,您在问题中忽略了要告诉我们的扩展名. (我已将.csv用于演示目的,只需根据需要对其进行更改).

Here's a slightly more complete version which assumes that your target files actually have an extension which you omitted to tell us in your question. (I've used .csv for demonstration purposes, just change it as needed).

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F Delims^=^ EOL^= %%G In ('Dir /B /A:-D "<.<.<.>>>>>>>>>>>>>>>.csv"') Do (
    For %%H In ("%%~nG") Do (
        Set "DateStamp=%%~xH"
        SetLocal EnableDelayedExpansion
        Ren "%%G" "%%~nH!DateStamp:~,9!%%~xG"
        EndLocal
    )
)

根据您的澄清,文件创建软件未为其分配扩展名,因此可以简化代码:

Based upon your clarification, that your files were not assigned extensions by their creation software, your code can be simplified:

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F Delims^=^ EOL^= %%G In ('Dir /B /A:-D "<.<.<.>>>>>>>>>>>>>>>"') Do (
    Set "DateStamp=%%~xG"
    SetLocal EnableDelayedExpansion
    Ren "%%G" "%%~nG!DateStamp:~,9!"
    EndLocal
)

这篇关于批处理作业删除带有日期和时间戳的文件名中的时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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