Rename a file with today's date

Subira -  
oopapf Posted messages 2 Status Member -
Pour créer une copie d'un fichier sous Windows en le renommant avec la date du jour, vous pouvez utiliser un script batch. Voici un exemple de script que vous pouvez automatiser :

```batch
@echo off
setlocal enabledelayedexpansion

set "sourceFile=toto.txt"
set "destinationFile=toto_"
for /f "tokens=1-3 delims=- " %%a in ('date /t') do (
set "date=%%c%%a%%b"
)
set "destinationFile=!destinationFile!!date!.txt"

copy "%sourceFile%" "!destinationFile!"
```

1. Ouvrez le Bloc-notes ou un éditeur de texte.
2. Copiez le code ci-dessus.
3. Modifiez `toto.txt` pour correspondre au nom de votre fichier d'origine.
4. Enregistrez le fichier avec l'extension `.bat`, par exemple `copieFichier.bat`.
5. Exécutez le script en double-cliquant sur le fichier `.bat`.

Cela créera une copie du fichier avec le nom incluant la date du jour dans le format AAAAMMJJ.

25 answers

  • 1
  • 2
  1. Stowabi
     
    Well, I know I'm replying to an old post, but here's a very simple solution:

    Create a batch file:
     set mydate=%date:~6,4%%date:~3,2%%date:~0,2% move toto.txt toto-%mydate%.txt 

    Place the batch file in the folder of the file to be renamed
    11
    1. adslogique
       
      you can also add %time:~0,2% to have the time in the file name.

      a good old simple, effective, and very practical msdos script...
      thank you Stowabi!
      0
    2. dug > adslogique
       
      Thanks guys!
      That's exactly what I was looking for at the end of this post, moving from dbase to awk :)
      Simple, clean, and precise :)
      0
    3. Max
       
      Thank you very much, very good post!
      Just be careful with the linguistic parameters of the post, the date format may vary...
      0
    4. hillbilly > adslogique
       
      Hello,
      In the post where you respond to rename a file by adding the date, do you have the argument to add the minutes?

      Thank you.
      0
    5. Skaper
       
      It's working perfectly!!
      Thank you very much!!!
      0
  2. patrice
     
    I don’t know if my contribution can bring anything, but since I can't even write a small program, I use a freeware (on Win98) called "the rename," extremely powerful, which can modify the extension or name of all files in a directory: you can choose date, time, incremental counter, add or rename text, invert lowercase and uppercase, extract MP3 filenames (I believe I understand), remove spaces, etc., rename to short files; there might also be a way to modify and add your own codes?
    Here’s an example taken from the help:
    Situation: you want to rename a series of graphic files like

    affiche98.JPG
    back.jpg
    bakgrunn.jpg
    bunch1.jpg
    ours.jpg
    SAUT.jpg
    topbkg.jpg

    to a series of names like img00001.jpg, img00002.jpg, img00003.jpg. You also want the extension to be in lower case.

    Choose Modify Prefix in the Prefix frame, click on the tab Add a counter, leave beginning value and step at their original values and modify digits to 5. Then, click on the Text tab and choose Replace with text and type img. In the Extension frame, scroll to Lower case. Select files and click on Start. That's all!

    This is why I made this program: to create this kind of file name.

    This may seem complicated but is very simple to use. The author is Hervé Thouzard, but his site has moved. He deserves to be known; plus it’s free and distributable; I think I got this from Freelog maybe?

    Patrice
    2
  3. saad
     
    Hi everyone,
    It's true, why not use a little program that does that? I have one, but for renaming, it only takes 8 characters,
    Let me know if you want it,

    Hi and @+
    1
  4. ipl Posted messages 5738 Status Security Contributor 585
     
    Good evening Subira, good evening everyone,

    This is doable with a .bat file if you enter the date as a parameter.

    Something like this (file Sauve.bat):
    @echo off
    rem Sauve.bat YYYYMMDD to save the files
    if %1.==. goto mess
    for %%V in (C:\dossier\*.*) do copy C:\dossier\%%v *_%1.*
    goto fin
    :mess
    echo it is necessary to enter the date YYYYMMDD
    :fin

    where %1 is the parameter used to launch the .bat:
    Sauve 20021118

    @12C4
    Ipl
    0
  5. subira
     
    Thank you ipl, I have a good lead, do you have any tips for retrieving today's date as a parameter in the procedure???
    Perhaps beforehand echoing date > date.txt

    And then in the .bat file doing:
    save date.txt (or save is the .bat file)

    Otherwise, I'll give it a try!!!

    Thanks again
    0
  6. ipl Posted messages 5738 Status Security Contributor 585
     
    Good evening Subira,

    No, I don't see any trick in DOS or under Windows because the Date command displays "The current date is Mon 18/11/2002 but there could well be a small program capable of displaying only the date (without / which is a forbidden character in the file name)... in ancient times, I had a basic program that did this for me (of course, it's possible to do it in many other languages... any programmer knows how to do it... besides, it was the subject of a discussion thread about 2 weeks ago on CCM)

    If you obtained such a program -let's call it date.com- (a program that would display today's date in the format YYYYMMDD):
    Date|Save
    would do the job!

    - I don't believe there is an environment variable that gives us the date in this form
    - we could also get by with a program capable of writing the result to a file.
    - I don't think there's a problem with the length of the name (greater than 8) if the work is done in a DOS window that is to say in DOS under Windows.

    @12C4
    Ipl
    0
  7. SUBIRA
     
    Hello everyone

    Thank you for your insightful responses.

    I would appreciate the small program that does this even if it only has 8 characters!!!

    JMS
    0
    1. saadmohamed Posted messages 7 Status Member
       
      Hi,
      I have the program, I wrote it in Fox, it measures 9704 bytes, but it requires a slightly larger run time of 2 MB. I don't know where to send these two modules,

      hello sunny Casablanca, see you later.
      0
  8. ofournier.cbd Posted messages 5862 Registration date   Status Member Last intervention   84
     
    Hello,

    a similar topic was discussed in PROGRAMMING:
    http://www.commentcamarche.org/forum/affich-171991?full

    Another post also mentioned a small exe or com that shows today's date under DOS, but I can't seem to find it...

    Olivier Fournier
    0
  9. ipl Posted messages 5738 Status Security Contributor 585
     
    Hello Olivier, hello everyone,

    >Another post also mentioned a small exe or com that gives the date
    of the day under DOS, but I can't find it...
    Could it date back to the period of November 4-16? :-(

    @12C4
    Ipl
    0
  10. ipl Posted messages 5738 Status Security Contributor 585
     
    Hello saadmohamed,

    >Hi again,
    >I have the program, I wrote it in Fox, it is 9704 bytes long, however
    >it requires a slightly larger run time of 2 MB, I don’t
    >know where to send you these two modules,

    Thank you for your offer!
    Could you please send the 2 modules to my email address above (by replacing 'à' with '@')? Thanks!
    Fox... isn't it compatible with dBase, but with a compiler -and runtime-?

    @12C4
    Ipl
    0
  11. ipl Posted messages 5738 Status Security Contributor 585
     
    Damn... I'm getting old!

    I still have gwbasic.exe, dBase III+, but I realize that I don't even know how to use them anymore (for a small utility)!
    I'm going to try anyway! Good grief!

    @12C4
    Ipl
    0
  12. ofournier.cbd Posted messages 5862 Registration date   Status Member Last intervention   84
     
    Good evening,

    in dBase the function that returns the system date is date()
    there are eight other functions (day, month, year, ...) that can be used in variables. Be careful with the year 2000 bug with two-digit years (last 2 digits/characters): dBase returns 01 as 1901 hence before 2000 ... !

    I have .PRG applications in dBase III+ and dBase IV (the same as Clipper compilable), under DOS 3 to 6.22 (setver compatible) of a large DBMS project I did in 1991-92 using all sorts of date variables, functions, and choices to find (sequential searches) precise data at a specific time in a myriad of integrated databases.

    Example:
    * ============================ TRANDAT.PRG ===================================
    * created by OF 16/3/91 as a utility for JTOT, etc...
    * Transformation of a date (DATED2) into file name DATEFIC.(mem, dbf,...)
    * syntax: 00_00_00.xxx macro: &DATEFIC (+".xxx")
    * ============================================================================

    ***************** numerical version (determined interval) *******************

    public DATOBS && * first DATE defined
    TJ1=day(FDATOBS) && * day (number 1 … 31)
    TM1=month(FDATOBS) && * month (1 … 12)
    TANN=str(year(FDATOBS)) && * year from 90 … ..)
    TAN=right(TANN,2) && * Characters
    VTA1=val(TAN) && * Numeric
    *------------------------ for example in rechagr.prg (add -> f-add)
    do case
    case TJ1 < 5 .and. TM1=1 && * bounds
    TJ2=15 && *********
    TM2=12
    VTA2=VTA1 - 1
    case TJ1 < 5 .and. TM1 > 1
    TJ2=15
    TM2=TM1 - 1
    VTA2=VTA1
    case TJ1 > 5 .and. TJ1 <= 10 .and. TM1=1
    ************* etc....
    endcase
    CTA1=str(VTA2)
    DATAC=ltrim(str(TJ2))+"/"+ltrim(str(TM2))+"/"+ltrim(CTA1)

    DATAD=ctod(DATAC) && * new deadline

    return

    * *********************** character version (file name) ****************

    *public DATED2,DATEFIC,DATEFICM && * for example jtot1.prg (add, e-add)

    TJO=day(CDATED2)
    TMO=month(CDATED2)
    TANN=str(year(CDATED2))
    TAN=right(TANN,2)

    DATEFIC=ltrim(str(TJO))+chr(95)+ltrim(str(TMO))+chr(95)+ltrim(TAN)
    * File name format: 1_1_90 or 10_10_90 (suffix .MEM)

    DATEFICM=DATEFIC+".MEM" && stored in S/directory VAR-MEM
    return
    * ============================================================================

    Are you more interested?

    Olivier Fournier
    0
  13. ipl Posted messages 5738 Status Security Contributor 585
     
    Good evening Subira, Olivier, good evening everyone,

    Thank you Olivier! ;-)

    Subira... do you have dBase III or higher?

    @12C4
    Ipl
    0
  14. M&amp;M Posted messages 5058 Status Contributor 669
     
    I can play too?
    Well, Bernadette, you're going to look for the little Awk v3.20 program on http://www.rduff.com/ and create the Awk program file below that we'll call Bonjour.awk (but you can shorten it as you wish and include it in a bat file if you like).

    #---------------------------------------------------------------------------
    # FILE: Bonjour.AWK
    # AUTHOR: M&M
    # DESCRIPTION:
    # This program creates a copy of a given file as a parameter
    # by adding the date and time to its name while keeping its extension
    # SYNTAX: awk -f bonjour.AWK myfile.doc
    # PREREQUISITES:
    # The PC must be configured for the date in Belgian or French format
    # (Country=032 or 033)
    # OUTPUTS:
    # it copies the FILE.EXT file under the name FILE_AAAAMMJJ.EXT
    # It creates and then deletes a temporary file temp.tmp
    # VERSION:
    # 1.0 20/11/02 M&M Creation
    #---------------------------------------------------------------------------

    BEGIN {
    TMP="TEMP.TMP" # Temporary file
    IN_FILE = toupper(ARGV[1]) # ARGC-1] # Name of the source file
    if (ARGC<2 || index(IN_FILE, ".") ==0 ) {
    print "\nFile to copy not defined or missing extension."
    print "Usage: awk -f bonjour.awk file.doc"
    Abort=1; exit }
    else {
    print "To get the date" > TMP
    close (TMP)
    system ("dir /4 "TMP" > " TMP)
    # acquisition of the current date and time from win98
    for (i=1;i<8;i++) {
    getline < TMP
    if (i> 4 && NF>=5) {
    Date=$4; Time=$5
    close(TMP)
    break
    }
    }
    if (i>7) {print "Error bonjour.awk: Date not found."; Abort=1;exit}
    }
    match(IN_FILE,/\.[A-Za-z0-9\-_]+$/)
    FILE= substr(IN_FILE,1,RSTART-1)
    EXT=substr(IN_FILE,RSTART+1)
    split(Date,DD,"\/")
    Date=DD[3]DD[2]DD[1]
    NEW=FILE"_"Date"."EXT
    system ("copy "IN_FILE" "NEW)
    exit
    }
    #---------------------------------------------------------------------------

    And there you go, Subirou, I find it horribly complicated compared to Unix shell tools. Let's leave this exercise to the young ones, ofou!
    :,§_ ç _
    (@)=(@)
    0
  15. saadmohamed Posted messages 7 Status Member
     
    Hello everyone,
    my version is the following:
    *----------------------------------------------------------
    parameter file

    d = substr(dtoc(date()),7,4) + substr(dtoc(date()),4,2) + substr(dtoc(date()),1,2) + '.'

    erase &d
    rename &file to &d
    return
    *--------------------------------------
    the program receives a file as a parameter and renames it to today's date,
    PS: I compiled this program under Fox 2.6, and I generated an EXE,
    I still don't have an @ email to send it to you,
    Bye and @+
    0
  16. saadmohamed Posted messages 7 Status Member
     
    Hello

    I'm replying to O.Fournier, that there has never been any talk of a bug in Dbase (we only use it as we want), there is the SET CENTURY ON command, which allows displaying the years in 4 digits, making it possible to enter 2000 instead of 00 for 2000,
    I still have some Dbase applications running since the dawn of time...
    cheers and @+
    0
  17. ofournier.cbd Posted messages 5862 Registration date   Status Member Last intervention   84
     
    Hello,

    WOW!!! Luckily we’re not on the PROGRAMMING forum!

    Here we are back in the sweet madness of dBase from Ashton Tate (and the huge work of "La Commande Electronique") which were massively criticized by the crazy business (no more Pascals and turbos!).

    Subira is served!

    Let’s just go back to a small detail for saadmohamed:
    1) of course for "set century on" right away (config.db)
    2 but the function LUPDATE() (last access date to a *.DBF database) returns to the screen: 1901 1902, etc... Ouch, the increments of these dates with variables to manage the databases and their periodic access!
    3) And then like an idiot, I had created dozens of apps with short dates: my DBMS was returning since before the Great War... since 01/01/2000!

    Olivier Fournier
    0
    1. saadmohamed Posted messages 7 Status Member
       
      Hi everyone,
      I just checked, LUPDATE returns incorrect information (1902), we can fix this by using the DOS system date, a Dir of the directory, followed by an append from into a DBF file whose record structure is the same as that of the Dir line, and there you go, the date is retrieved in 3 data fields that we can manipulate as we want...

      Hi sunny Casablancans, @+
      0
  18. SUBIRA
     
    Good evening,

    Thank you all, I will try to sort through all your responses; what is certain is that I do not want to install any runtime on my machine!!!
    0
  19. M&amp;M Posted messages 5058 Status Contributor 669
     
    So, adopt my little Awk.exe script which weighs 47KB and create a small DD.BAT file that contains this line:

    c:\util\awk -f c:\util\bonjour.awk %1

    Put everything in a directory c:\util or any other directory that is in the PATH and it will run when you type DD followed by the name of the file to be dated.

    Small bugs fixed: I used the toupper instruction which makes the result uppercase. (replace toupper(ARGV[1]) with ARGV[1])
    Another bug fixed: if the extension contains an accented character, it crashes. (replace [A-Za-z0-9\-_]+ with [^.]+)
    Last remaining bug: if the name contains the sign =, my program will not find the file. (no workaround)
    Small advantage: if the file contains more than one point in the name, it correctly takes the last point as the extension separator
    It also accepts long names and extensions longer than 3 characters.
     :,§_ ç _
    (@)=(@)
    0
  20. JM DEWALQUE Posted messages 8 Status Member
     
    Hello,

    For the date given by LUPDATE, you need to add the following instruction at the beginning of the program:

    SET EPOCH TO 1960 (or any other pivotal date)

    This tells Clipper that any date in the format "18/05/04" should be interpreted as part of the year 2004 and not 1904 since EPOCH is set to 1960. However, a date with a two-digit year number (xx) earlier than "60" will be considered as "19xx" and not "20xx".

    The best approach is to use:
    SET CENTURY ON
    which forces the system to work with the year expressed in 4 digits. This eliminates any ambiguity in determining the century relative to a year expressed in 2 digits.
    0
  • 1
  • 2