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: Request: right-click on file to rename it to add current date  (Read 15,918 times)

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,931
    • Gathering Badge
    • Plugin Coding Medal
    • Participated in NANY Challenge Seventeen Times
    • Cody Award
    • Coding Snacks
    • Volunteers/Staff
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

  • @Slartibartfarst
  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 7,544
Re: Request: right-click on file to rename it to add current date
« Reply #1 on: December 16, 2015, 05:30 AM »
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)
^+G::  ; Ctrl+Shift+G - grabs folder/file path to clipboard and puts Modification (or Creation?) date/time into Clipboard
	FileDateTime := ""	; define and clear variable
	FileNamePath := ""	; define and clear variable
	FileNameNoExt := ""		; define and clear variable	
	SuffixString := "hrs"	; define suffix
;	---------------------------------------
	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%, M	; get file CREATION (C) or MODIFICATION (M) 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 (for testing)
	SplitPath, FileNamePath,,,,FileNameNoExt

	clipboard = %FileDateTime%%SuffixString% %FileNameNoExt% ; put the formatted date/time+ suffix + filename into clipboard.
	send {F2}^v{Enter}
	return
;-------------------------------------------------------------------

4wd

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Re: Request: right-click on file to rename it to add current date
« Reply #2 on: December 16, 2015, 06: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)
param (
  [string]$file = $(throw "AddDate.ps1 <file>")
)

$sDate =  ("{0:MM}" -f (Get-Date)) + ("{0:dd}" -f (Get-Date)) + ("{0:yy}" -f (Get-Date))

$sNewName = (Get-Item $file).BaseName + ' ' + $sDate + (Get-Item $file).Extension
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: ( xml)
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\shell\Add Date to Filename]
@=""

[HKEY_CLASSES_ROOT\*\shell\Add Date to Filename\command]
@="C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe -sta -ExecutionPolicy Bypass -NoProfile -Window Hidden C:\\\\Scripts\\\\AddDate.ps1 --% '%1'"

AddDate_Remove.reg
Code: ( xml)
Windows Registry Editor Version 5.00

[-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.
"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
« Last Edit: December 17, 2015, 03:11 AM by 4wd »

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,931
    • Gathering Badge
    • Plugin Coding Medal
    • Participated in NANY Challenge Seventeen Times
    • Cody Award
    • Coding Snacks
    • Volunteers/Staff
Re: Request: right-click on file to rename it to add current date
« Reply #3 on: December 16, 2015, 06: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

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Re: Request: right-click on file to rename it to add current date
« Reply #4 on: December 16, 2015, 09: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.
"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
« Last Edit: December 16, 2015, 11:20 PM by 4wd »

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,931
    • Gathering Badge
    • Plugin Coding Medal
    • Participated in NANY Challenge Seventeen Times
    • Cody Award
    • Coding Snacks
    • Volunteers/Staff
Re: Request: right-click on file to rename it to add current date
« Reply #5 on: December 16, 2015, 11:27 PM »
awesome, will test and report.

4wd

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Re: Request: right-click on file to rename it to add current date
« Reply #6 on: December 17, 2015, 03:10 AM »
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
"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

4wd

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Re: Request: right-click on file to rename it to add current date
« Reply #8 on: December 17, 2015, 09:57 AM »
Fancy that, mouser not using the search function
"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

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,931
    • Gathering Badge
    • Plugin Coding Medal
    • Participated in NANY Challenge Seventeen Times
    • Cody Award
    • Coding Snacks
    • Volunteers/Staff
Re: Request: right-click on file to rename it to add current date
« Reply #9 on: December 17, 2015, 01:39 PM »
:huh: darn it. sorry :(

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