且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

批处理作业删除带有日期和时间戳的文件名中的时间戳

更新时间:2022-04-29 00:06:07

这是一个稍微完整些的版本,它假定您的目标文件实际上具有扩展名,您在问题中忽略了要告诉我们的扩展名. (我已将.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
)