【问题标题】:Rename/update any date string with current date using batch file使用批处理文件用当前日期重命名/更新任何日期字符串
【发布时间】:2015-06-09 16:50:12
【问题描述】:

到目前为止,我有一个批处理文件,它重命名了更新括号中的日期字符串的文件。这工作正常,但日期可以是任何数字。如何让批次更新括号中包含的任何日期字符串。

echo on
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set MyDate=%%x
set today=%MyDate:~0,4%-%MyDate:~4,2%-%MyDate:~6,2%


set "_year=%MyDate:~0,4%"
set "_month=%MyDate:~4,2%"
set "_day=%MyDate:~6,2%"

REN C:\Users\xyz125\Documents\Erics's Docs\scripts\"test file (20150112).txt" "test file (%_year%%_month%%_day%).txt"

pause

【问题讨论】:

    标签: batch-file


    【解决方案1】:

    试试dir/b | Findstr /r "([0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])"

    这会产生以下文件this (20150609)something.txt,并且该文件夹还包含一个名为test(1234)else.txt 的文件,因为正则表达式正在寻找8 个数字而不是4,所以找不到该文件。该模式将是dir/b | findstr /r "([0-9][0-9][0-9][0-9])"`

    这篇文章对Piped Variable Into FINDSTR w/ Regular Expressions and Escaped Double Quotes 有帮助,您可以做类似的事情,并使用您希望文件重命名为的字符串设置一个变量。

    -编辑添加关于字符串连接的注释see here

    也可以在这里查看答案Generate unique file name with timestamp in batch script

    【讨论】:

    • 我不熟悉reg express。它看起来是否与此接近: echo ON for /f "skip=1" %%x in ('wmic os get localdatetime') 如果未定义则执行 MyDate set MyDate=%%x set today=%MyDate:~0, 4%-%MyDate:~4,2%-%MyDate:~6,2% 设置 "_year=%MyDate:~0,4%" 设置 "_month=%MyDate:~4,2%" 设置 "_day= %MyDate:~6,2%" C:\Batch | Findstr /r "([0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])" REN "C :\Batch\ | Findstr /r (%_year%%_month%%_day%).txt" rem ^ pause
    • 请使用代码更新编辑问题,试图在评论中理解代码并不有趣。
    【解决方案2】:
    @4317867.  Sorry about that.  For some reason I could not edit the code while pasting a response comment. Im not familiar with reg express. Would it look anything close to this: 
    
    
    
    echo ON
    for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set MyDate=%%x
    set today=%MyDate:~0,4%-%MyDate:~4,2%-%MyDate:~6,2%
    
    
    set "_year=%MyDate:~0,4%"
    set "_month=%MyDate:~4,2%"
    set "_day=%MyDate:~6,2%"
    
     C:\Batch | Findstr /r "([0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])"
    
    REN "C:\Batch\ | Findstr /r (%_year%%_month%%_day%).txt"
    
    rem ^
    
    pause
    

    【讨论】:

      猜你喜欢
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多