topbanner_forum
  *

avatar image

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

Login with username, password and session length
  • Friday March 29, 2024, 9:36 am
  • Proudly celebrating 15+ 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: Request: right-click on file to rename it to add current date  (Read 10385 times)

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
One thing I frequently do in my file explorer is right-click a file (zip file for example), and rename it to add the current date (MMDDYY).
It would be nice to have a right-click context menu extension to do this.

IainB

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 7,540
  • @Slartibartfarst
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: Request: right-click on file to rename it to add current date
« Reply #1 on: December 15, 2015, 11:30 PM »
Ergonomically, I much prefer using the keyboard to automate things, rather than using the mouse, so I wrote this into my AHK file: (please feel free to use/adapt it)
________________________________
Code: Autohotkey [Select]
  1. ^+G::  ; Ctrl+Shift+G - grabs folder/file path to clipboard and puts Modification (or Creation?) date/time into Clipboard
  2.         FileDateTime := ""      ; define and clear variable
  3.         FileNamePath := ""      ; define and clear variable
  4.         FileNameNoExt := ""             ; define and clear variable    
  5.         SuffixString := "hrs"   ; define suffix
  6. ;       ---------------------------------------
  7.         SetTitleMatchMode, 2    ; match must be for specified string anywhere in the window Title name
  8.         IfWinActive,  xplorer² ; do this if xplorer² is the string in the active window Title
  9.         {
  10.         clipboard := "" ; clear clipboard
  11.         SendInput, ^c   ; copies file path and name to clipboard
  12.         ClipWait, 2 ; wait for up to 2 seconds for state change before proceeding, so as to ensure capture to clipboard
  13. ;       MsgBox, 0, Pause 1 - Clipboard content, %Clipboard%     ; display clipboard text contents (for testing)
  14.         }
  15.         else    ; return (do nothing)
  16.         {
  17.         return
  18.         }
  19.         FileNamePath = %Clipboard%      ; puts just plain text of file name and path from clipboard into variable
  20. ;       MsgBox, 0, Pause 2 - FileNamePath, %FileNamePath%       ; display variable text contents (for testing)
  21.         FileGetTime, FileDateTime, %FileNamePath%, M    ; get file CREATION (C) or MODIFICATION (M) date/time for named path/file
  22.         FormatTime, FileDateTime, %FileDateTime%, yyyy-MM-dd HHmm  ; It will look like 2010-12-21 0353
  23. ;       MsgBox, 0, Pause 3 - formatted FileDateTime, %FileDateTime%     ; display variable text contents (for testing)
  24.         SplitPath, FileNamePath,,,,FileNameNoExt
  25.  
  26.         clipboard = %FileDateTime%%SuffixString% %FileNameNoExt% ; put the formatted date/time+ suffix + filename into clipboard.
  27.         send {F2}^v{Enter}
  28.         return
  29. ;-------------------------------------------------------------------

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,641
    • View Profile
    • Donate to Member
Re: Request: right-click on file to rename it to add current date
« Reply #2 on: December 16, 2015, 12:12 AM »
There's reg files in the archive to install/remove from the context menu.

It's assumed that AddDate.ps1 lives in C:\Scripts - edit the reg files if it doesn't.

AddDate.ps1
Code: PowerShell [Select]
  1. param (
  2.   [string]$file = $(throw "AddDate.ps1 <file>")
  3. )
  4.  
  5. $sDate =  ("{0:MM}" -f (Get-Date)) + ("{0:dd}" -f (Get-Date)) + ("{0:yy}" -f (Get-Date))
  6.  
  7. $sNewName = (Get-Item $file).BaseName + ' ' + $sDate + (Get-Item $file).Extension
  8. Rename-Item $file $sNewName

Limited testing here but it worked, eg.  AddDate.ps1 renamed to AddDate 121615.ps1

If you don't like the CLI window that opens you could use cmdow with the /HID switch.

EDIT: Added -ExecutionPolicy Bypass -NoProfile to reg entry so there's no need to modify Powershell ExecutionPolicy globally.

AddDate_Install.reg
Code: Text [Select]
  1. Windows Registry Editor Version 5.00
  2.  
  3. [HKEY_CLASSES_ROOT\*\shell\Add Date to Filename]
  4. @=""
  5.  
  6. [HKEY_CLASSES_ROOT\*\shell\Add Date to Filename\command]
  7. @="C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe -sta -ExecutionPolicy Bypass -NoProfile -Window Hidden C:\\\\Scripts\\\\AddDate.ps1 --% '%1'"

AddDate_Remove.reg
Code: Text [Select]
  1. Windows Registry Editor Version 5.00
  2.  
  3. [-HKEY_CLASSES_ROOT\*\shell\Add Date to Filename]

UPDATE: There was a problem calling the script on file names with spaces occasionally, changed the reg entry to call PoSh directly and replaced quotes around file arg which seems to have fixed it.
« Last Edit: December 16, 2015, 09:11 PM by 4wd »

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Request: right-click on file to rename it to add current date
« Reply #3 on: December 16, 2015, 12:32 PM »
Thanks IainB and 4wd  :up:

Since I am a right-click person, I used 4wd's solution, and it worked like a charm (after I changed my security policy to run powershell scripts, see here; I opened console, typed "powershell" to start a powershell console, then typed "Set-ExecutionPolicy RemoteSigned").

Works great, thank you!  :up:



ps. This is just fine for me -- I was going to suggest baking up an installer and sharing it as a NANY app, but unfortunately the need to do this esoteric step of adding a security exception to allow powershell scripts to run does pose a bit of a problem in terms of making this more widely available to other folks :(

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,641
    • View Profile
    • Donate to Member
Re: Request: right-click on file to rename it to add current date
« Reply #4 on: December 16, 2015, 03:46 PM »
Thanks mouser!

You can put the execution policy bypass in the command used to call the Powershell script.

Registry entries updated in archive above.
« Last Edit: December 16, 2015, 05:20 PM by 4wd »

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Request: right-click on file to rename it to add current date
« Reply #5 on: December 16, 2015, 05:27 PM »
awesome, will test and report.

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,641
    • View Profile
    • Donate to Member
Re: Request: right-click on file to rename it to add current date
« Reply #6 on: December 16, 2015, 09:10 PM »
UPDATE: There was a problem calling the script on file names with spaces occasionally, changed the reg entry to call PoSh directly and replaced quotes around file arg which seems to have fixed it.

Let me know if there is still a problem.

Archive up there

PhilB66

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 1,522
    • View Profile
    • Donate to Member

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,641
    • View Profile
    • Donate to Member
Re: Request: right-click on file to rename it to add current date
« Reply #8 on: December 17, 2015, 03:57 AM »
Fancy that, mouser not using the search function

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Request: right-click on file to rename it to add current date
« Reply #9 on: December 17, 2015, 07:39 AM »
 :huh: darn it. sorry :(