-1

I am unable to get the time stamp in 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.

Batch Script

SET dataauditfilename=myAuditReport
rem Section to rename the file

Set Timestamp=%date:~4,2%_%date:~7,2%_%date:~10,4%_%time:~1,1%%time:~3,2%%
ren "%dataauditfilename%.CSV" "%dataauditfilename%_%Timestamp%.CSV"

The above batch script works in Windows 11 but not in Windows server 2022 Datacenter 64-bit Version 21H2(Build 20348.1487).

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • 3
    Please use the `search` facility in the top bar and search for `[batch]date`. This question is asked repeatedly. The root cause is that date/time format is user-dependent, so without an indication of what format **you** are using, it's a guess. I'm particularly suspicious of your `time` substringing. My time format is `20:26:59.64` for instance, so that would yield `06:` using your code (2`0`:2`6:`59.64), which is nonsensical and includes a character that's illegal in a filename. – Magoo Jan 30 '23 at 14:31
  • 1
    May be be [this](https://stackoverflow.com/questions/203090/how-do-i-get-current-date-time-on-the-windows-command-line-in-a-suitable-format/19799236#19799236) will be helpful. – npocmaka Jan 30 '23 at 14:40
  • Does this answer your question? [How do I get current date/time on the Windows command line in a suitable format for usage in a file/folder name?](https://stackoverflow.com/questions/203090/how-do-i-get-current-date-time-on-the-windows-command-line-in-a-suitable-format) – Mostafa Fakhraei Jan 31 '23 at 18:23

1 Answers1

0

Could you write how you would want the file name be? Ie: 01_30_2023_2310 (This is my current date and time (01/30/2023 - 23:10).

When i run at the cmd the name your script should set Timestamp (%date:~4,2%_%date:~7,2%_%date:~10,4%_%time:~1,1%%time:~3,2%%), i get 01_30_2023_312% and maybe that's why isn't working.

Anyway, if my suggestion of name is what you want, then the script should be:

SET dataauditfilename=myAuditReport
rem Section to rename the file

Set Timestamp=%date:~4,2%_%date:~7,2%_%date:~10,4%_%time:~0,2%%time:~3,2%
ren "%dataauditfilename%.CSV" "%dataauditfilename%_%Timestamp%.CSV"
1endell
  • 15
  • 6