Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: add current date/time to filename  (Read 3157 times)

0 Members and 1 Guest are viewing this topic.

app_dev

  • Guest
add current date/time to filename
« on: November 01, 2006, 08:11:41 AM »
Hi there.

I have a batch file that runs a dts package against a txt file (extract.txt). The batch file runs 4 times a week. I used to just delete the extract file after the dts had finished but now I have to move the Extract file into a folder called E:\Extract\OLD.

my problem is that the extracts have to have the current date/time added to their file name, before they are put in the OLD folder, so that a collection of past extract files are stored in a folder, which can be viewed for reference. So what I need to do is rename the extract.txt so that the current date/time is added to the end of the file name:

eg extract.txt would renamed extract011120061534.txt (todays date and time)

and then move it into the old folder.

Is there a way of doing this within my current batch file so that the date/time is automatically added to the file name.

Thanks in advance ps..im a novice at batch files, so please keep it simple if possible
« Last Edit: November 01, 2006, 08:15:52 AM by app_dev »

GuruGary



    Adviser
    Re: add current date/time to filename
    « Reply #1 on: November 01, 2006, 09:44:18 AM »
    Do you want to rename it with the current date and time?  Or the date and timestamp of the file?

    I am making the following assumptions: You are running Windows 2000 / XP / 2003 / Vista, your regional settings are U.S., English, and you want to use the preferred format of YYYYMMDDhhmm.  Try:
    Code: [Select]
    @echo off
    setlocal
    set hour=%time:~0,2%
    if %hour% LSS 10 set hour=0%hour:~-1%
    ren extract.txt extract%date:~-4%%date:~-10,2%%date:~-7,2%%hour%%time:~3,2%.txt
    If you need a different format, post back and let us know.

    If you just need to add the date to the filename (without time) it can be done in just 1 line of code.

    app_dev

    • Guest
    Re: add current date/time to filename
    « Reply #2 on: November 03, 2006, 05:14:43 AM »
    thanks I'll give that a try.

    I get email an extract several times a week. Each time the extract has the same file name. Now I have to put the extracts in a folder called old, for referencing purposes I need the date/timestamp added to the filename, so that over a period of week, the "Oldfolder" will have files listed like thus:

    extract110120061345.txt
    extract110220061406.txt

    QBasicMac

    • Guest
    Re: add current date/time to filename
    « Reply #3 on: November 03, 2006, 05:43:08 AM »
    Four times a week? Then you don't need the time.

    I suggest you change the standard for your history folder like this:
    extract20061101.txt
    extract20061102.txt

    Namely, put the year first so at the end of the year
    extract20061229.txt
    extract20070102.txt
    remain in order. Otherwise, all your Januarys will be together regardless of year.

    Mac


    LHinkel

    • Guest
    Re: add current date/time to filename
    « Reply #4 on: October 30, 2009, 09:11:36 AM »
    This worked great!

    Thanks.

    Lorie