Website Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • August 31, 2015, 01:47:00 AM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

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

wetsmellydog

  • Participant
  • Joined in 2007
  • *
  • gravatar avatar
  • Posts: 104
    • View Profile
    • Donate to Member
IDEA: AHK script to insert date and time to file name
« on: June 25, 2010, 11:30:46 AM »
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,217
    • View Profile
    • Coding Snacks by Lanux128
    • Donate to Member
Re: IDEA: AHK script to insert date and time to file name
« Reply #1 on: June 25, 2010, 11:53:32 AM »
is this what you're looking for? TimeStamp 2.1 by Skrommel..

http://www.donationc....msg116805#msg116805

wetsmellydog

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

http://www.donationc....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,217
    • View Profile
    • Coding Snacks by Lanux128
    • Donate to Member
Re: IDEA: AHK script to insert date and time to file name
« Reply #3 on: June 25, 2010, 12:23:19 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

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 5,429
  • Slartibartfarst
    • View Profile
    • Donate to Member
Re: IDEA: AHK script to insert date and time to file name
« Reply #4 on: June 25, 2010, 12:26: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
  • *
  • gravatar avatar
  • Posts: 104
    • View Profile
    • Donate to Member
Re: IDEA: AHK script to insert date and time to file name
« Reply #5 on: June 25, 2010, 01:12:07 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, 01:18:01 PM by wetsmellydog »

IainB

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 5,429
  • Slartibartfarst
    • View Profile
    • Donate to Member
Re: IDEA: AHK script to insert date and time to file name
« Reply #6 on: June 26, 2010, 11:21:38 PM »
@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
  • *
  • gravatar avatar
  • Posts: 104
    • View Profile
    • Donate to Member
Re: IDEA: AHK script to insert date and time to file name
« Reply #7 on: June 28, 2010, 04:19:42 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
    • View Profile
    • Donate to Member
Re: IDEA: AHK script to insert date and time to file name
« Reply #8 on: February 16, 2014, 04:37:44 PM »
Hi, I wonder if you could maybe mod this:
Code: [Select]
^+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

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 5,429
  • Slartibartfarst
    • View Profile
    • Donate to Member
Re: IDEA: AHK script to insert date and time to file name
« Reply #9 on: February 17, 2014, 02:08:16 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
  • ***
  • gravatar avatar
  • Posts: 2,853
    • View Profile
    • Donate to Member
Re: IDEA: AHK script to insert date and time to file name
« Reply #10 on: February 17, 2014, 03:39:37 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.pngIDEA: AHK script to insert date and time to file name      Snap2.pngIDEA: AHK script to insert date and time to file name

tomos

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 9,501
    • View Profile
    • Donate to Member
Re: IDEA: AHK script to insert date and time to file name
« Reply #11 on: February 17, 2014, 04:12:54 PM »
Hi, I wonder if you could maybe mod this:
Code: [Select]
^+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, 04:03:17 AM by tomos »

metaDCoder

  • Participant
  • Joined in 2014
  • *
  • Posts: 2
    • View Profile
    • Donate to Member
Re: IDEA: AHK script to insert date and time to file name
« Reply #12 on: February 18, 2014, 02:20:42 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.autohotke...or-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

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 5,429
  • Slartibartfarst
    • View Profile
    • Donate to Member
Re: IDEA: AHK script to insert date and time to file name
« Reply #13 on: February 18, 2014, 04:18:55 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

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,963
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: IDEA: AHK script to insert date and time to file name
« Reply #14 on: February 18, 2014, 07:18:06 PM »
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
  1. #region ;**** Directives created by AutoIt3Wrapper_GUI ****
  2. #AutoIt3Wrapper_UseUpx=n
  3. #endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
  4. ; http://www.autoitscript.com/forum/topic/89833-windows-explorer-current-folder/page__st__40#entry973904
  5. #include <Array.au3>
  6.  
  7. HotKeySet('+!v', '_HK_Path') ; Shift+Alt+V
  8. HotKeySet('+!d', '_HK_Date') ; Shift+Alt+D
  9.  
  10. Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ComErrFunc")
  11.  
  12.         Sleep(100)
  13.  
  14. Func _HK_Path()
  15.         _ExClip(False)
  16. EndFunc   ;==>_HK_Path
  17.  
  18. Func _HK_Date()
  19.         _ExClip(True)
  20. EndFunc   ;==>_HK_Date
  21.  
  22. Func _ExClip($temp)
  23.         $hExplr = WinActive("[REGEXPCLASS:(Explore|Cabinet)WClass]")
  24.         If $hExplr <> '' Then
  25.                 $aSelection = _ExplorerWinGetSelectedItems($hExplr)
  26. ;~              _ArrayDisplay($aSelection)
  27.                 If $aSelection[0] = 0 Then
  28.                         If StringLeft($aSelection[1], 2) <> '::' Then
  29.                                 If Not $temp Then
  30.                                         ClipPut($aSelection[1])
  31.                                 Else
  32.                                         $sDate = FileGetTime($aSelection[1], 0, 1) ; Get MODIFIED date (0), change 0 to 1 for CREATED, 2 for ACCESSED
  33.                                         ClipPut(StringLeft($sDate, 8) & " " & StringMid($sDate, 9))
  34.                                 EndIf
  35.                         EndIf
  36.                 Else
  37.                         If Not $temp Then
  38.                                 ClipPut($aSelection[2])
  39.                         Else
  40.                                 $sDate = FileGetTime($aSelection[2], 0, 1) ; Get MODIFIED date (0) (0), change 0 to 1 for CREATED, 2 for ACCESSED
  41.                                 ClipPut(StringLeft($sDate, 8) & " " & StringMid($sDate, 9))
  42.                         EndIf
  43.                 EndIf
  44.         EndIf
  45. EndFunc   ;==>_ExClip
  46.  
  47.  
  48.  
  49. ; ==========================================================================================================================
  50.  
  51. ; Func _ObjectSHFolderViewFromWin($hWnd)
  52. ;
  53. ; Returns an 'ShellFolderView' Object for the given Window handle
  54. ;
  55. ; Author: Ascend4nt, based on code by KaFu, klaus.s
  56. ; ==========================================================================================================================
  57.  
  58. Func _ObjectSHFolderViewFromWin($hWnd)
  59.         If Not IsHWnd($hWnd) Then Return SetError(1, 0, 0)
  60.         Local $oShell, $oShellWindows, $oIEObject, $oSHFolderView
  61.  
  62.         ; Shell Object
  63.         $oShell = ObjCreate("Shell.Application")
  64.         If Not IsObj($oShell) Then Return SetError(2, 0, 0)
  65.  
  66.         ;   Get a 'ShellWindows Collection' object
  67.         $oShellWindows = $oShell.Windows()
  68.         If Not IsObj($oShellWindows) Then Return SetError(3, 0, 0)
  69.  
  70.         ;   Iterate through the collection - each of type 'InternetExplorer' Object
  71.  
  72.         For $oIEObject In $oShellWindows
  73.                 If $oIEObject.HWND = $hWnd Then
  74.                         ; InternetExplorer->Document = ShellFolderView object
  75.                         $oSHFolderView = $oIEObject.Document
  76.                         If IsObj($oSHFolderView) Then Return $oSHFolderView
  77.                         Return SetError(4, 0, 0)
  78.                 EndIf
  79.         Next
  80.  
  81.         Return SetError(-1, 0, 0)
  82. EndFunc   ;==>_ObjectSHFolderViewFromWin
  83.  
  84. ; ==========================================================================================================================
  85. ; Func _ExplorerWinGetSelectedItems($hWnd)
  86. ;
  87. ;
  88. ; Author: klaus.s, KaFu, Ascend4nt (consolidation & cleanup, Path name simplification)
  89. ; ==========================================================================================================================
  90.  
  91. Func _ExplorerWinGetSelectedItems($hWnd)
  92.         If Not IsHWnd($hWnd) Then Return SetError(1, 0, '')
  93.         Local $oSHFolderView
  94.         Local $iSelectedItems, $iCounter = 2, $aSelectedItems[2] = [0, ""]
  95.  
  96.         $oSHFolderView = _ObjectSHFolderViewFromWin($hWnd)
  97.         If @error Then Return SetError(@error, 0, '')
  98.  
  99.         ;   SelectedItems = FolderItems Collection object->Count
  100.         $iSelectedItems = $oSHFolderView.SelectedItems.Count
  101.  
  102.         Dim $aSelectedItems[$iSelectedItems + 2] ; 2 extra -> 1 for count [0], 1 for Folder Path [1]
  103.  
  104.         $aSelectedItems[0] = $iSelectedItems
  105.         ;   ShellFolderView->Folder->Self as 'FolderItem'->Path
  106.         $aSelectedItems[1] = $oSHFolderView.Folder.Self.Path
  107.  
  108.         ;   ShellFolderView->SelectedItems = FolderItems Collection object
  109.         $oSelectedFolderItems = $oSHFolderView.SelectedItems
  110.  
  111.         #cs
  112.                 ; For ALL items in an Explorer Window (not just the selected ones):
  113.                 $oSelectedFolderItems = $oSHFolderView.Folder.Items
  114.                 ReDim $aSelectedItems[$oSelectedFolderItems.Count+2]
  115.         #ce
  116.  
  117.         For $oFolderItem In $oSelectedFolderItems
  118.                 $aSelectedItems[$iCounter] = $oFolderItem.Path
  119.                 $iCounter += 1
  120.         Next
  121.  
  122.         Return SetExtended($iCounter - 2, $aSelectedItems)
  123. EndFunc   ;==>_ExplorerWinGetSelectedItems
  124.  
  125. Func _ComErrFunc($oError)
  126.         ConsoleWrite("COM Error occurred:" & @CRLF & _
  127.                         "Number: " & @TAB & $oError.number & @CRLF & _
  128.                         "Windescription:" & @TAB & $oError.windescription & @CRLF & _
  129.                         "Description is: " & @TAB & $oError.description & @CRLF & _
  130.                         "Source is: " & @TAB & $oError.source & @CRLF & _
  131.                         "Helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
  132.                         "Helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
  133.                         "Lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
  134.                         "Scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
  135.                         "Retcode is: " & @TAB & $oError.retcode & @CRLF & @CRLF)
  136. EndFunc   ;==>_ComErrFunc
  137.  

IainB

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 5,429
  • Slartibartfarst
    • View Profile
    • Donate to Member
Re: IDEA: AHK script to insert date and time to file name
« Reply #15 on: February 18, 2014, 08:44:20 PM »
@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
  1. ;=============================================================
  2. ^+G::  ; Ctrl+Shift+G grabs folder/file path to clipboard and puts CREATION date/time into Clipboard
  3.         FileDateTime := ""      ; define and clear variable
  4.         FileNamePath := ""      ; define and clear variable
  5.         SetTitleMatchMode, 2    ; match must be for specified string anywhere in the window Title name
  6.         IfWinActive,  xplorer² ; do this if xplorer² is the string in the active window Title
  7.         {
  8.         clipboard := "" ; clear clipboard
  9.         SendInput, ^c   ; copies file path and name to clipboard
  10.         ClipWait, 2 ; wait for up to 2 seconds for state change before proceeding, so as to ensure capture to clipboard
  11. ;       MsgBox, 0, Pause 1 - Clipboard content, %Clipboard%     ; display clipboard text contents (for testing)
  12.         }
  13.         else    ; return (do nothing)
  14.         {
  15.         return
  16.         }
  17.         FileNamePath = %Clipboard%      ; puts just plain text of file name and path from clipboard into variable
  18. ;       MsgBox, 0, Pause 2 - FileNamePath, %FileNamePath%       ; display variable text contents (for testing)
  19.         FileGetTime, FileDateTime, %FileNamePath%, C    ; get file CREATION date/time for named path/file
  20.         FormatTime, FileDateTime, %FileDateTime%, yyyy-MM-dd HHmm  ; It will look like 2010-12-21 0353
  21. ;       MsgBox, 0, Pause 3 - formatted FileDateTime, %FileDateTime%     ; display variable text contents
  22.         clipboard = %FileDateTime%      ; put the formatted date/time into clipboard
  23.         return
  24. ;-------------------------------------------------------------------
  25. ;============================================================
  26.  
« Last Edit: February 27, 2014, 11:41:29 PM by IainB, Reason: CORRECTION to code: 2014-02-28 1837hrs »

IainB

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 5,429
  • Slartibartfarst
    • View Profile
    • Donate to Member
Re: IDEA: AHK script to insert date and time to file name
« Reply #16 on: February 27, 2014, 11:42:46 PM »
Just corrected the AHK code above. I had forgotten to put the final (correct) code in. Sorry.