DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: IDEA: AHK script to insert date and time to file name  (Read 27,858 times)

wetsmellydog

  • Participant
  • Joined in 2007
  • *
  • Posts: 120
IDEA: AHK script to insert date and time to file name
« on: June 25, 2010, 04:30 PM »
While backing up some info today, I thought how cumbersome it is to type the date and time into the file name. It would be nice to have a shortcut that could do that.
Yes I know there is a time/date stamp on the file BUT this makes the file name unique too.

EX:

           "inserted text"
foobar 06_25_10 9-30am (does not have to follow this exact format just an idea)

Apologies up front if this has already been done but unfortunately I have not been able to find it.

Thanks!
Larry

lanux128

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 6,277
    • Coding Snacks
    • Volunteers/Staff
    • Gathering Badge
    • Participated in NANY Challenge Once
    • Linux User
Re: IDEA: AHK script to insert date and time to file name
« Reply #1 on: June 25, 2010, 04:53 PM »
is this what you're looking for? TimeStamp 2.1 by Skrommel..

• http://www.donationcoder.com/forum/index.php?topic=13669.msg116805#msg116805

wetsmellydog

  • Participant
  • Joined in 2007
  • *
  • Posts: 120
Re: IDEA: AHK script to insert date and time to file name
« Reply #2 on: June 25, 2010, 05:19 PM »
is this what you're looking for? TimeStamp 2.1 by Skrommel..

• http://www.donationcoder.com/forum/index.php?topic=13669.msg116805#msg116805

Thanks for the link. Will have to take a look...one thing I notice is the formatting...certain characters are no no for file naming.

lanux128

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 6,277
    • Coding Snacks
    • Volunteers/Staff
    • Gathering Badge
    • Participated in NANY Challenge Once
    • Linux User
Re: IDEA: AHK script to insert date and time to file name
« Reply #3 on: June 25, 2010, 05:23 PM »
yes, that script was originally made to be pasted in the active window but in your case, the reserved characters should be excluded.

Quote
The following reserved characters are not allowed in file/folder naming: ! @ # $ % ^ & * ( ) + = ~ ` < > : " / \ | ? *

IainB

  • @Slartibartfarst
  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 7,544
Re: IDEA: AHK script to insert date and time to file name
« Reply #4 on: June 25, 2010, 05:26 PM »
I use this - prune it how you like.
Having the date in ISO format at the front of the file name makes for good document filing/sorting.

AutoHotKey script:
Quote
^+d::  ; Ctrl+Shift+d outputs current date+time
FormatTime, CurrentDateTime,, yyyy-MM-dd HHmm  ; It will look like 2010-12-21 0353hrs
SendInput %CurrentDateTime%hrs
return

wetsmellydog

  • Participant
  • Joined in 2007
  • *
  • Posts: 120
Re: IDEA: AHK script to insert date and time to file name
« Reply #5 on: June 25, 2010, 06:12 PM »
I use this - prune it how you like.
Having the date in ISO format at the front of the file name makes for good document filing/sorting.

AutoHotKey script:
Quote
^+d::  ; Ctrl+Shift+d outputs current date+time
FormatTime, CurrentDateTime,, yyyy-MM-dd HHmm  ; It will look like 2010-12-21 0353hrs
SendInput %CurrentDateTime%hrs
return

Sweet!  :Thmbsup:

Thanks! Hopefully I will have more time to work on learning AHK to be able to know that it takes a whole two lines of code to do this!

BTW works like a charm.
« Last Edit: June 25, 2010, 06:18 PM by wetsmellydog »

IainB

  • @Slartibartfarst
  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 7,544
Re: IDEA: AHK script to insert date and time to file name
« Reply #6 on: June 27, 2010, 04:21 AM »
@wetsmellydog: Thanks for your thanks. I cannot take credit for this as being "all my own work" though!

I actually lifted some AHK code from somewhere else (I forget where), and then later - after taking the time to discover/learn more about AHK - I simplified and modified the code to suit my particular requirements. So what you see is what I ended up with.

The sources I used were:

These are all good. The tutorials I came across were very helpful.

As an encouragement, I would suggest that you might pick it up quite quickly if you have no techo background to cloud your thinking with preconceptions.

You might think it would help to have a techo background, but sometimes I am not so sure.

For example, take my case: Having been introduced to computer programming via a low-level programming language used for assembler programming on mainframe computers years ago, I have always had difficulty changing my thinking to use any higher-level languages. It was thus difficult for me when I later had to adapt and learn to program in the programming language FORTRAN (which has to be compiled before execution), but when I learned the BASIC language it seemed quite foreign (it is not compiled, but runs through an interpreter at run-time). Years later, I had similar difficulty wrapping my mind around a scripting language (I think it was called "Telix") to control and manipulate data flowing between the PC serial port and a modem (driven by the Hayes AT command set).
So, when I came across AHK, though it at least had a degree of familiarity in its general structure, I had to push to one side everything that I had previously learned, lest it get in the way of my understanding this new thing.

You might not have that difficulty, from the sound of things.
Interestingly, from what I have read, a lot of the PC viruses over the years are apparently thought to have been developed by "script-kiddies" - virgins whose first PC programming language was a scripting language.

wetsmellydog

  • Participant
  • Joined in 2007
  • *
  • Posts: 120
Re: IDEA: AHK script to insert date and time to file name
« Reply #7 on: June 28, 2010, 09:19 PM »
IainB "As an encouragement, I would suggest that you might pick it up quite quickly if you have no techo background to cloud your thinking with preconceptions."

No truer words were ever spoken!

I am an electronics engineer by trade and now a nurse. My specialty was automation and controls.

It has been tough to wrap my mind around how they want "nurses" to think. What seems simple to the people who do not have a technical background take on a whole new meaning to me.   :huh:

I am far ahead of what they are expecting and I have to remember to go from a to b to c instead of jumping right into c because I "know" it is right. I have also been working in the medical field for the last 4 years which also hinders that KISS thinking.

Some of us think in binary and some of us think in analog and some of us keep confusing the 10!!

metaDCoder

  • Participant
  • Joined in 2014
  • *
  • Posts: 2
Re: IDEA: AHK script to insert date and time to file name
« Reply #8 on: February 16, 2014, 10:37 PM »
Hi, I wonder if you could maybe mod this:
Code
^+d::  ; Ctrl+Shift+d outputs current date+time
   FormatTime, CurrentDateTime,, yyyy-MM-dd HHmm  ; It will look like 2010-12-21 0353hrs
   SendInput %CurrentDateTime%hrs
   return

So that it would rather copy to the clipboard the ISO formatted timestamp as given by attributes/properties (rather than CURRENT timestamp).

Thanks

IainB

  • @Slartibartfarst
  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 7,544
Re: IDEA: AHK script to insert date and time to file name
« Reply #9 on: February 17, 2014, 08:08 AM »
You'd be able to use AHK FileGetTime for that:
From the Help file:
Parameters
  • OutputVar: The name of the variable in which to store the retrieved date-time in format YYYYMMDDHH24MISS. The time is your own local time, not UTC/GMT.
  • Filename: The name of the target file or folder, which is assumed to be in %A_WorkingDir% if an absolute path isn't specified. If omitted, the current file of the innermost enclosing File-Loop will be used instead.
  • WhichTime: Which timestamp to retrieve:
    M = Modification time (this is the default if the parameter is omitted)
    C = Creation time
    A = Last access time

I haven't used it in an AHK script, but it should presumably work fine.
First, you'd need to decide which of the three file date/times you wanted to retrieve by default - M, C, or A.
The new bit for me would be pinpointing the input file from which to take the date/time.

rjbull

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 3,217
Re: IDEA: AHK script to insert date and time to file name
« Reply #10 on: February 17, 2014, 09:39 PM »
it would rather copy to the clipboard the ISO formatted timestamp
You might like to look at Horst Schaeffer's freeware Calendar which allows you to roll your own formats and have them available from a drop-down.  E.g.:
Snap1.png Snap2.png

tomos

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 11,970
    • Gathering Badge
    • Participated in NANY Challenge Once
Re: IDEA: AHK script to insert date and time to file name
« Reply #11 on: February 17, 2014, 10:12 PM »
Hi, I wonder if you could maybe mod this:
Code
^+d::  ; Ctrl+Shift+d outputs current date+time
   FormatTime, CurrentDateTime,, yyyy-MM-dd HHmm  ; It will look like 2010-12-21 0353hrs
   SendInput %CurrentDateTime%hrs
   return

So that it would rather copy to the clipboard the ISO formatted timestamp as given by attributes/properties (rather than CURRENT timestamp).

FWIW, I have an older script (2008...)
[edit] which did something completely different :-[ (pasted current date/time [/edit]
Tom
« Last Edit: February 28, 2014, 10:03 AM by tomos »

metaDCoder

  • Participant
  • Joined in 2014
  • *
  • Posts: 2
Re: IDEA: AHK script to insert date and time to file name
« Reply #12 on: February 18, 2014, 08:20 PM »
Hi guys, I just wanted to thank you for your comments and suggestions concerning my question (of inserting attributes:time_stamp into the clipboard, and eventually filename).   :)

@IainB - I appreciate the references on the proper functions to use. I don't think it will be so hard now for me to write a functioning script.  Check out http://www.autohotkey.com/board/topic/80479-get-file-path-for-files-on-desktop/ for a super-simple method of retrieving file-path. Note that that script breaks if trying to use the hot-key with no file/folder selected.

IainB

  • @Slartibartfarst
  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 7,544
Re: IDEA: AHK script to insert date and time to file name
« Reply #13 on: February 18, 2014, 10:18 PM »
@metaDCoder: Ah, thanks for that link to http://www.autohotkey.com...ath-for-files-on-desktop/
I shall write/try out a script for this idea and post it here when I get it to work.

4wd

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Re: IDEA: AHK script to insert date and time to file name
« Reply #14 on: February 19, 2014, 01:18 AM »
Don't know if you're familiar with AutoIt but I just added a few lines to a script I modified here.

Only for Explorer.  (Most decent file managers would do this type of thing already, ie. insert file dates, meta data into a rename function.)

Shift+Alt+V - copies the file path/name of the first selected item in an Explorer window, (file or folder), or if nothing is selected then it copies the current Explorer path to the clipboard.

Shift+Alt+D - copies the MODIFIED date of the selected item, (or Explorer path if nothing selected), into the clipboard in Universal format: YYYYMMDD HHMMSS  (NOTE: Won't capture the date of root directories.)

The hotkeys, type of date retrieved, and it's output format could all be changed in the source.

Code: (autoit)
#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseUpx=n
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
; http://www.autoitscript.com/forum/topic/89833-windows-explorer-current-folder/page__st__40#entry973904
#include <Array.au3>

HotKeySet('+!v', '_HK_Path') ; Shift+Alt+V
HotKeySet('+!d', '_HK_Date') ; Shift+Alt+D

Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ComErrFunc")

While 1
	Sleep(100)
WEnd

Func _HK_Path()
	_ExClip(False)
EndFunc   ;==>_HK_Path

Func _HK_Date()
	_ExClip(True)
EndFunc   ;==>_HK_Date

Func _ExClip($temp)
	$hExplr = WinActive("[REGEXPCLASS:(Explore|Cabinet)WClass]")
	If $hExplr <> '' Then
		$aSelection = _ExplorerWinGetSelectedItems($hExplr)
;~ 		_ArrayDisplay($aSelection)
		If $aSelection[0] = 0 Then
			If StringLeft($aSelection[1], 2) <> '::' Then
				If Not $temp Then
					ClipPut($aSelection[1])
				Else
					$sDate = FileGetTime($aSelection[1], 0, 1) ; Get MODIFIED date (0), change 0 to 1 for CREATED, 2 for ACCESSED
					ClipPut(StringLeft($sDate, 8) & " " & StringMid($sDate, 9))
				EndIf
			EndIf
		Else
			If Not $temp Then
				ClipPut($aSelection[2])
			Else
				$sDate = FileGetTime($aSelection[2], 0, 1) ; Get MODIFIED date (0) (0), change 0 to 1 for CREATED, 2 for ACCESSED
				ClipPut(StringLeft($sDate, 8) & " " & StringMid($sDate, 9))
			EndIf
		EndIf
	EndIf
EndFunc   ;==>_ExClip



; ==========================================================================================================================

; Func _ObjectSHFolderViewFromWin($hWnd)
;
; Returns an 'ShellFolderView' Object for the given Window handle
;
; Author: Ascend4nt, based on code by KaFu, klaus.s
; ==========================================================================================================================

Func _ObjectSHFolderViewFromWin($hWnd)
	If Not IsHWnd($hWnd) Then Return SetError(1, 0, 0)
	Local $oShell, $oShellWindows, $oIEObject, $oSHFolderView

	; Shell Object
	$oShell = ObjCreate("Shell.Application")
	If Not IsObj($oShell) Then Return SetError(2, 0, 0)

	;   Get a 'ShellWindows Collection' object
	$oShellWindows = $oShell.Windows()
	If Not IsObj($oShellWindows) Then Return SetError(3, 0, 0)

	;   Iterate through the collection - each of type 'InternetExplorer' Object

	For $oIEObject In $oShellWindows
		If $oIEObject.HWND = $hWnd Then
			; InternetExplorer->Document = ShellFolderView object
			$oSHFolderView = $oIEObject.Document
			If IsObj($oSHFolderView) Then Return $oSHFolderView
			Return SetError(4, 0, 0)
		EndIf
	Next

	Return SetError(-1, 0, 0)
EndFunc   ;==>_ObjectSHFolderViewFromWin

; ==========================================================================================================================
; Func _ExplorerWinGetSelectedItems($hWnd)
;
;
; Author: klaus.s, KaFu, Ascend4nt (consolidation & cleanup, Path name simplification)
; ==========================================================================================================================

Func _ExplorerWinGetSelectedItems($hWnd)
	If Not IsHWnd($hWnd) Then Return SetError(1, 0, '')
	Local $oSHFolderView
	Local $iSelectedItems, $iCounter = 2, $aSelectedItems[2] = [0, ""]

	$oSHFolderView = _ObjectSHFolderViewFromWin($hWnd)
	If @error Then Return SetError(@error, 0, '')

	;   SelectedItems = FolderItems Collection object->Count
	$iSelectedItems = $oSHFolderView.SelectedItems.Count

	Dim $aSelectedItems[$iSelectedItems + 2] ; 2 extra -> 1 for count [0], 1 for Folder Path [1]

	$aSelectedItems[0] = $iSelectedItems
	;   ShellFolderView->Folder->Self as 'FolderItem'->Path
	$aSelectedItems[1] = $oSHFolderView.Folder.Self.Path

	;   ShellFolderView->SelectedItems = FolderItems Collection object
	$oSelectedFolderItems = $oSHFolderView.SelectedItems

	#cs
		; For ALL items in an Explorer Window (not just the selected ones):
		$oSelectedFolderItems = $oSHFolderView.Folder.Items
		ReDim $aSelectedItems[$oSelectedFolderItems.Count+2]
	#ce

	For $oFolderItem In $oSelectedFolderItems
		$aSelectedItems[$iCounter] = $oFolderItem.Path
		$iCounter += 1
	Next

	Return SetExtended($iCounter - 2, $aSelectedItems)
EndFunc   ;==>_ExplorerWinGetSelectedItems

Func _ComErrFunc($oError)
	ConsoleWrite("COM Error occurred:" & @CRLF & _
			"Number: " & @TAB & $oError.number & @CRLF & _
			"Windescription:" & @TAB & $oError.windescription & @CRLF & _
			"Description is: " & @TAB & $oError.description & @CRLF & _
			"Source is: " & @TAB & $oError.source & @CRLF & _
			"Helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
			"Helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
			"Lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
			"Scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
			"Retcode is: " & @TAB & $oError.retcode & @CRLF & @CRLF)
EndFunc   ;==>_ComErrFunc
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903

IainB

  • @Slartibartfarst
  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 7,544
Re: IDEA: AHK script to insert date and time to file name
« Reply #15 on: February 19, 2014, 02:44 AM »
@4wd: Thanks for the AutoIT code.
I just did pretty much the same thing in AHK out of interest (a training exercise) and for @metaDCoder (if needed):

Notes:
  • This works for files and folders alike.
  • I did this for xplorer² only, as that is the file explorer I use and I am unsure what the Active Window name for Windows Explorer is (or should be). You need a check like this so as to ensure that the code only runs when your file explorer is the active window, so that way it does not try to run when no file has been selected in a window for some other non-file explorer application.
    I did this generically for Windows File Explorer otherwise. If I was doing this for myself, I might have some slightly different code as xplorer² has a command to pass the file name/path directly to the clipboard
  • If you want to test it at each stage, you can un-comment (remove the semi-colon) at the start of each of the three MsgBox lines.
  • I have assumed that the file CREATION date time is what is wanted. There are options in the FileGetTime instruction:
    M = Modification time (this is the default if the parameter is omitted)
    C = Creation time
    A = Last access time

Code: (autohotkey)
;=============================================================
^+G::  ; Ctrl+Shift+G grabs folder/file path to clipboard and puts CREATION date/time into Clipboard
	FileDateTime := ""	; define and clear variable
	FileNamePath := ""	; define and clear variable
	SetTitleMatchMode, 2	; match must be for specified string anywhere in the window Title name
	IfWinActive,  xplorer²	; do this if xplorer² is the string in the active window Title
	{
	clipboard := ""	; clear clipboard
	SendInput, ^c	; copies file path and name to clipboard
	ClipWait, 2 ; wait for up to 2 seconds for state change before proceeding, so as to ensure capture to clipboard
;	MsgBox, 0, Pause 1 - Clipboard content, %Clipboard%	; display clipboard text contents (for testing)
	}
	else	; return (do nothing)
	{
	return
	}
	FileNamePath = %Clipboard%	; puts just plain text of file name and path from clipboard into variable
;	MsgBox, 0, Pause 2 - FileNamePath, %FileNamePath%	; display variable text contents (for testing)
	FileGetTime, FileDateTime, %FileNamePath%, C	; get file CREATION date/time for named path/file
	FormatTime, FileDateTime, %FileDateTime%, yyyy-MM-dd HHmm  ; It will look like 2010-12-21 0353
;	MsgBox, 0, Pause 3 - formatted FileDateTime, %FileDateTime%	; display variable text contents
	clipboard = %FileDateTime%	; put the formatted date/time into clipboard
	return
;-------------------------------------------------------------------
;============================================================
« Last Edit: February 28, 2014, 05:41 AM by IainB »

IainB

  • @Slartibartfarst
  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 7,544
Re: IDEA: AHK script to insert date and time to file name
« Reply #16 on: February 28, 2014, 05:42 AM »
Just corrected the AHK code above. I had forgotten to put the final (correct) code in. Sorry.

Original address: https://www.donationcoder.com/forum/index.php?topic=23220.0