ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE. Software > Post New Requests Here

Request: right-click on file to rename it to add current date

(1/2) > >>

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.

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;-------------------------------------------------------------------

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.


--- 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).ExtensionRename-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.


--- Code: Text ---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'"

--- Code: Text ---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.

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 :(

Thanks mouser!

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

Registry entries updated in archive above.


[0] Message Index

[#] Next page

Go to full version