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%"
pause

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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...