Jump to content
Sign in to follow this  
NCC10281982B

Append date to filename

Recommended Posts

At work I have a situation where by I want to be able to have the current date appended to the document file name. ex: nte_wire_order_12152015.txt.

 

Is there possibly a way to tweak the registry to have this happen or an auto hot key script?

 

Thanks guys

Share this post


Link to post
Share on other sites

You will have to change the extension back to .bat, but this should do the trick. You can just drag and drop the files you want renamed onto the batch file, and it will do the rest. (It will take me a bit longer, but I think I can also make a script that will add the file's modified date to its name.)

 

@echo off

set month=%date:~4,2%
set day=%date:~7,2%
set year=%date:~10,4%
set date=%year%%month%%day%

::%date pulls the current date from your computer
::the :~x,y parts cut out the rest of the string, to isolate the desired portions

:start

ren "%~1" "%~n1_%date%%~x1"

::renames the first file (%~1) dropped onto the batch file to have its name ($~n1) augmented with the date, and the original extension (%~x1)
::by using %~1, which includes the path to the file, the batch file does not need to be in the same directory as the files to rename.

shift

::this allows you to drag-and-drop multiple files onto the batch file, as it will shift their order (%~2 becomes %~1, so the above command still works)

if "%~1"=="" goto end
goto start

:end

::pause

 


Date in Name.txt

Had to finagle with the variables a little (batch parameters don't like being truncated apparently) but here's a version that will append on the file's date modified date:

 

 

@echo off


:start
::%date pulls the current date from your computer
::the :~x,y parts cut out the rest of the string, to isolate the desired portions
set date=%~t1
::necessary for the truncation below to work

set month=%date:~0,2%
set day=%date:~3,2%
set year=%date:~6,4%
set date=%year%%month%%day%

ren "%~1" "%~n1_%date%%~x1"

::renames the first file (%~1) dropped onto the batch file to have its name ($~n1) augmented with the date, and the original extension (%~x1)
::by using %~1, which includes the path to the file, the batch file does not need to be in the same directory as the files to rename.

shift

::this allows you to drag-and-drop multiple files onto the batch file, as it will shift their order (%~2 becomes %~1, so the above command still works)

if "%~1"=="" goto end
goto start

:end

::pause

 

 

Share this post


Link to post
Share on other sites

Thanks man! This will do what I want, abet with an extra step.

 

It would be great if an option existed in the save dialog to append a variable like the date. hint hint

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...