Batch: adding a time stamp to file names

Posted on 2006-05-22
Last Modified: 2013-12-03
I'd like to add time stamp to file names like this:

file1.txt -> file1%timestamp%.txt
file2.txt -> file2%timestamp%.txt

How do I do it so that timestamp doesn't come after .txt extension?

Question by:gromul
    LVL 95

    Accepted Solution

    That can get a little trickier - ASSUMING the file names DO NOT have more than one period in them - to seperate the extensions, then use this:

    FOR /F "tokens=1,2 delims=." %%a in ('dir /b /a-d') do @ren %%a.%%b %%a%date:~-4%%date:~4,2%%date:~7,2%%time:~0,2%%time:~3,2%%time:~6,2%.%%b

    If used OUTSIDE of a batch file, use SINGLE percents on the %%a references:

    FOR /F "tokens=1,2 delims=." %a in ('dir /b /a-d') do @ren %a.%b %a%date:~-4%%date:~4,2%%date:~7,2%%time:~0,2%%time:~3,2%%time:~6,2%.%b

    What I mean by one period:

    These would be ok,
    This would NOT work.

    Author Comment


    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
    This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now