How to rename a file with the current date from the context menu in Explorer?

0

0

I want to be able to rename any file or folder with the current date, so I added the following key value to the registry, to get this option to show up in the right-click context menu:

HKEY_CLASSES_ROOT\*\shell

Cmd.exe /c ren %1 "sample".*

However, I couldn't find how to perform this with the current date. Can anyone help?

Mgnfcnt

Posted 2014-03-27T07:04:34.923

Reputation: 3

Do you mean you don't know how to retrieve the current date? If so, then the command to do that in Windows is date /t – Indrek – 2014-03-27T07:10:44.623

Cmd.exe /c ren %1 date /t or date.* these are not working – Mgnfcnt – 2014-03-27T07:14:11.873

Answers

0

To get the current date in the Windows command prompt, use the following:

date /t

To rename a file with the current date, you'll need to use the for construct to capture the output of the date command and pass it to ren:

for /f "useback delims=" %x in (`date /t`) do ren oldfile.foo %x

Note, however, that the output of date can, depending on your regional settings, contain symbols that cannot be used in a filename, for instance the slash (/). If so, you have two options:

  • Format the date such that it is suitable for use in a filename; see this post at Stack Overflow.

  • Use PowerShell instead. The following should do the trick:

    Rename-Item oldfile.foo (Get-Date -Format yyyy-MM-dd)
    

    That example uses the ISO date format. See Formatting Dates and Times for more information.


To add a new context menu entry to all files, do the following:

  1. Open regedit.exe and navigate to HKEY_CLASSES_ROOT\*\shell

  2. Add a new key and set its name to whatever you want the menu item to be called

  3. Add a new key under that, with the name command

  4. Double-click the (Default) value under the command key and set its value as follows:

    • if using the regular command prompt (note the double percentage signs):

      cmd /c for /f "useback delims=" %%x in (`date /t`) do ren "%1" %%x
      
    • if using PowerShell:

      cmd /c powershell Rename-Item "%1" (Get-Date -Format yyyy-MM-dd)
      

Indrek

Posted 2014-03-27T07:04:34.923

Reputation: 21 756

this is not working. when clicked it opens "OPEN FILE WITH" – Mgnfcnt – 2014-03-27T07:27:43.690

@Mgnfcnt Please see updated post. I tested both the cmd.exe and PowerShell versions, and both work fine. – Indrek – 2014-03-27T08:20:08.437

sorry but none of them is working. i am using Windows 7 X64 – Mgnfcnt – 2014-03-27T09:17:22.530

I tested with Windows 8.1. Looks like you can't invoke PowerShell directly in Windows 7, so I've amended that command slightly (added cmd /c in front). After that, though, both worked just fine for me in Windows 7 as well. Are you sure you're creating the registry key correctly? Right-click on HKEY_CLASSES_ROOT\*\shell, select Export, save it to a file and post the contents of the file (either edit them into your question, or use a site like pastebin.com). – Indrek – 2014-03-27T09:34:08.993

https://gist.github.com/mgnfcnt/9803937 – Mgnfcnt – 2014-03-27T09:47:37.210

Thanks. That looks identical to what I have, so I'm not sure why it isn't working for you. What exactly happens when you select that option from the right-click menu? Does the menu entry even show up? Does a command prompt window flash by? Try replacing cmd /c with cmd /k, that'll cause the command prompt window to stay visible instead of closing automatically. Are there any errors there? – Indrek – 2014-03-27T09:54:23.593

If you're using the command prompt version, did you see what I wrote in my answer about the date format? From the registry export you posted it seems you're using a format that includes the slash character (/), so that's what might be causing the command to fail. Just to clarify, you did try the PowerShell version as well? If you did, did you write it exactly as shown, or did you change the -Format parameter somehow? – Indrek – 2014-03-27T09:57:10.893

ren "C:\Users\mgnfcnt\Desktop\aaaa.xml" Thu 03/27/2014 The syntax of the command is incorrect. – Mgnfcnt – 2014-03-27T11:10:29.090

Yes, the problem is the date format. I've addressed this in my answer. – Indrek – 2014-03-27T11:14:35.853