unable to get the time stamp with the file name by using batch script

Praveen, Ala 0 Reputation points
2023-02-07T07:45:22.9433333+00:00

Hi,

I am unable to get the time stamp with file name by using batch script in Windows Server 2022 Datacenter 64-bit Version 21H2(Build 20348.1487). Could you please help me on this.

Set Timestamp=%date:~4,2%%date:~7,2%%date:~10,4%_%time:~0,2%%time:~3,2%

ren "%dataexportfilename%.CSV" "%dataexportfilename%_%Timestamp%.CSV"

Thanks,

Praveen

Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
13,726 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Limitless Technology 44,666 Reputation points
    2023-02-07T16:33:22.4833333+00:00

    Hi. Thank you for your question and reaching out. I’d be more than happy to help you with your query

    It seems like you are trying to rename a file using a timestamp as part of the file name. The code you posted will work on Windows Server 2022 Datacenter 64-bit Version 21H2(Build 20348.1487), but it might not produce the desired result in terms of the format of the timestamp.

    Here's an updated version of the code that produces a timestamp in the format of "MMDDYYYY_HHMM" (e.g., "02072022_1648"):

    @echo off

    for /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)

    for /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a%%b)

    set Timestamp=%mydate:~-2%%mydate:~-7,2%%mydate:~-10,4%_%mytime%

    ren "%dataexportfilename%.CSV" "%dataexportfilename%_%Timestamp%.CSV"

    If the reply was helpful, please don’t forget to upvote or accept as answer, thank you.


  2. Ian Xue-MSFT 40,986 Reputation points Microsoft External Staff
    2023-02-08T06:29:35.8333333+00:00

    Hi,

    What is the value of Timestamp? You can add a line ECHO %Timestamp% to check the variable. Please note that the results of the date command could be different on systems of different languages so you may have to adjust the start index to get the substring you want.

    Best Regards,

    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.