Jump to content

Timestamped File Name in Windows Batch Script


Recommended Posts

I've had two BashBunny payloads fail on me (USB_File_Exfiltration and SmartFileExtract_Exfiltration) when I ran them in the morning, after working on them for a full day trying to get them to work (the night before).

Debugging the scripts on Windows, I found out that the date/time stamp formatting for the filename was causing the issue.  The hour is left-padded with a space in the AM.

I found the following hint for creating Windows Batch Script variables that are properly formatted with the date/time.  My modified code to match the format in the payload scripts is below:

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%" & set "fullstamp=%YYYY%%MM%%DD%_%HH%%Min%%Sec%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"

Here is the output:

datestamp: "20190809"
timestamp: "084546"
fullstamp: "20190809_084546"
Press any key to continue . . .

And here is the link that helped me figure it out:

How do I get current datetime on the Windows command line, in a suitable format for using in a filename?

I hope this helps someone avoid the struggle I've been having the last two days.

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...