Extract the date with Ms-dos

Solved
Hello,
I'm looking to extract today's date in the format dd_mm_yyyy and store the result in a variable (which I will use to open a file with that name).
Is that possible??????

32 answers

  1. Hello,

    an extremely simple way to get the system date in the format DD_MM_YYYY with MS-DOS in a single line in a batch file is as follows:

    set thedate=%date:/=_%

    Here we indicate that we need to replace / with _

    You can execute %date:/=_% in DOS and it will indeed display the date in the format DD_MM_YYYY
    9
    1. very clear explanation, even for a novice like me
      0
  2. ```batch
    @echo off
    set amj=%date:~6,4%%date:~3,2%%date:~0,2%
    set h=%time:~0,2%
    set m=%time:~3,2%
    set s=%time:~6,2%
    if %h% lss 10 set h=0%h:~1,1%
    set hms=%h%%m%%s%
    rar a -m3 -ep c:\contact-%amj%-%hms%.rar c:\contact.doc
    move c:\contact-%amj%-%hms%.rar E:\backup\
    pause
    ```
    1
    1. To display the time in a filename and avoid the null issue when the time is between (00h = 0h to 09 = 9h), you simply need to reformat the variable used to display the time. For example, if the variable used is vtime, you can use another variable ptime=%vtime: =0% which replaces the null in vtime with 0 (if vtime= 92531 then ptime=092531).
      0
      1. for /f "tokens=1-3 delims:;" %%i in ('time /t') do echo Testing: %date:/=_ % %%ih%%j >> _test-date-time.txt

        =)
        0
        1. Great, it works very well too.
          0
      2. Wow, 6 years later :) I say Bravo sir! ... It doesn't make us any younger! But it’s nice to see that the hours spent creating small pieces of code can still be useful 6 years later. I admit I occasionally do some copy/paste of the examples explained in this thread. Happy end of year celebrations to you.

        Poulpenstring
        0
        1. 6 years later, I thank Eric and Poulpenstring for their help.
          Thanks to them, I managed to complete my batch easily, and it works perfectly.
          Thank you very much.
          0
          1. Hi Van,

            Try the command string.com

            and run a .BAT script as follows

            Echo OFF
            STRINGS DAY = DAY

            RENAME file.txt %DAY%.TXT

            Good luck Van

            SEMTECH
            0

            1. weekday (Monday.....Friday)
              day(dd)
              month(mm)
              year(aaaa)
              0
              1. Je suis ici pour traduire. Veuillez fournir le texte à traduire.
                0
            2. Well, I had 2 minutes and 30 seconds ahead of me...

              Here is a complete example if we assume your files are on C:\ and you are copying them to D:\

              The example is intentionally simplistic; you can potentially optimize it with loops or other labels... (if you feel like it)

              set my_file=c:\bases_%Date:~-4%%Date:~-7,-5%%Date:~-10,-8%.FIC
              copy %my_file% d:\
              set my_file=c:\bases_%Date:~-4%%Date:~-7,-5%%Date:~-10,-8%.NDX
              copy %my_file% d:\
              set my_file=c:\histo_%Date:~-4%%Date:~-7,-5%%Date:~-10,-8%.FIC
              copy %my_file% d:\
              set my_file=c:\histo_%Date:~-4%%Date:~-7,-5%%Date:~-10,-8%.NDX
              copy %my_file% d:\
              set my_file=c:\reglem_%Date:~-4%%Date:~-7,-5%%Date:~-10,-8%.FIC
              copy %my_file% d:\
              set my_file=c:\reglem_%Date:~-4%%Date:~-7,-5%%Date:~-10,-8%.NDX
              copy %my_file% d:\


              (all in a .bat file of course)

              Have a nice day.
              1
              1. Little change (more rational and a bit cleaner)

                set Wdatjour=%Date:~-4%%Date:~-7,-5%%Date:~-10,-8%
                copy c:\bases_%wdatjour%.FIC D:\
                copy c:\bases_%wdatjour%.NDX D:\
                copy c:\histo_%wdatjour%.FIC D:\
                copy c:\histo_%wdatjour%.NDX D:\
                copy c:\reglem_%wdatjour%.FIC D:\
                copy c:\reglem_%wdatjour%.NDX D:\

                there you go...
                0
            3. Looking at this topic, I realized that answer #15 corresponds to your expectations...

              In the example:

              set my_file=c:\ZZZ-%Date:~-4%%Date:~-7,-5%%Date:~-10,-8%.XXX
              echo %my_file%

              you just need to replace c:\ with the correct path - ZZZ with the name of your file and XXX with the extension of your file, then proceed to copy instead of the echo command in this example...

              it would look something like:

              set my_file=c:\bases_-%Date:~-4%%Date:~-7,-5%%Date:~-10,-8%.FIC
              copy %my_file% DESTINATION (see exact syntax of copy)

              All of this in a .bat while doing the same for...
              bases_20080405.NDX
              histo_20080405.FIC
              histo_20080405.NDX
              reglem_20080405.FIC
              reglem_20080405.NDX

              To test.......

              Have a nice day!
              0
              1. Hello,

                Yes, it’s certainly possible. Being at the office, I don’t have much time to run a test, but in the meantime, why not try a good sync software like SyncBackSE which will do the job wonderfully?

                Sorry for this incomplete reply, but in the meantime, you never know if it might help you.

                Have a nice day.
                0
                1. I am working on software that creates 6 files every day corresponding to the history of the day.
                  These files are:
                  bases_20080405.FIC
                  bases_20080405.NDX
                  histo_20080405.FIC
                  histo_20080405.NDX
                  reglem_20080405.FIC
                  reglem_20080405.NDX

                  My task is to transfer, every day at 11 PM, only the 6 files that contain the date from the system to a remote computer (via IP) and paste them into a specific directory.

                  Is my question clear? Is it unfeasible? Anyway, thanks to whoever can help me out of this tough spot!!!
                  0
                  1. Remarks:

                    Be careful with variables declared in a for loop or in a call invoked within the for loop:
                    -- no update or retrieval possible.
                    -- use variables declared before the loop.
                    -- thoroughly test before putting into production.

                    For my part, I handle my operations entirely within the calls;
                    the for loop only serves to read values one after the other.
                    0
                    1. Thank you and well done Philippe, for this example rich in terms of code and comments, which I am eager to copy/paste in case of need.
                      Have a good day.
                      3
                      1. Thank you, I proposed this across the entire "CodeS-SourceS" sites and I got kicked out! On the grounds that it didn't fit into any category... too bad for them!#§@?
                        0
                    2. A small example of what can be done for XP/2000
                      (adaptable for other versions; expandable for calculating the date of Easter...)
                      Copy/paste the following lines into a text file to be customized as needed:
                      @echo off
                      REM ==============================================================================================================
                      REM RETRIEVAL OF THE LAST CLOSING DATE OF CASH REGISTERS FROM A GIVEN LIST
                      REM --------------------------------------------------------------------------------------------------------------
                      REM HISTORY:
                      REM Created on 26/05/2007 Philippe LEBLANC
                      REM Modified on 28/05/2007 Philippe LEBLANC
                      REM Redesigned because no value is returned by the subroutines in task planning
                      REM ==============================================================================================================
                      setlocal EnableDelayedExpansion
                      title RETRIEVAL OF THE LAST CLOSING DATE OF CASH REGISTERS FROM A GIVEN LIST

                      REM ----- SETUP -------------------------------------
                      set path=%path%;c:\progra~1\tivoli\lcf\bin\w32-ix86\tools

                      REM Windows version
                      for /F "usebackq delims==" %%i in (`ver`) do set OSVER=%%i

                      REM Identifications for Windows XP and 2K
                      set USERADMIN=xxxxxxxxx
                      set ADMPASXP=yyyyyyyyy
                      set ADMPAS2K=zzzzzzzzz
                      set USERCAISSE=ttttttttt\CAISSE
                      set CAIPAS2K=uuuuuuuuu
                      set CAIPASXP=vvvvvvvvv

                      REM File containing the list of registers
                      set FichierCaisses=liste.csv
                      REM Temporary file containing the list of already processed registers
                      set FichierTemp=liste.tmp
                      set Lecteur=C$
                      REM Retrieval directory
                      set Repertoire=winstore
                      set Chemin=%Lecteur%\%Repertoire%
                      REM INI file
                      set FichierData=Transferts.ini

                      REM Retrieval of the date and time
                      call :DateHeureSysteme
                      set Bissextile=0
                      set Semaine=0
                      set Journee=0
                      call :JourneeSemaineDate %Annee% %Mois% %Jour%
                      if "%Mois%" == "01" set NomMois=January
                      if "%Mois%" == "02" set NomMois=February
                      if "%Mois%" == "03" set NomMois=March
                      if "%Mois%" == "04" set NomMois=April
                      if "%Mois%" == "05" set NomMois=May
                      if "%Mois%" == "06" set NomMois=June
                      if "%Mois%" == "07" set NomMois=July
                      if "%Mois%" == "08" set NomMois=August
                      if "%Mois%" == "09" set NomMois=September
                      if "%Mois%" == "10" set NomMois=October
                      if "%Mois%" == "11" set NomMois=November
                      if "%Mois%" == "12" set NomMois=December
                      if "%Journee%"=="1" set NomJour=Monday
                      if "%Journee%"=="2" set NomJour=Tuesday
                      if "%Journee%"=="3" set NomJour=Wednesday
                      if "%Journee%"=="4" set NomJour=Thursday
                      if "%Journee%"=="5" set NomJour=Friday
                      if "%Journee%"=="6" set NomJour=Saturday
                      if "%Journee%"=="7" set NomJour=Sunday

                      REM Creation of the log file
                      set FichierLog=TransfertsIniCaisses_%aaaammjj%.log
                      echo %DateHeure% Start of retrieval for %NomJour% >> %FichierLog%

                      REM Test for launching in the presence of the file containing the list of registers
                      if not exist %FichierCaisses% (
                      echo %DateHeure% : No retrieval because the file containing the list of registers %FichierCaisses% is missing. >> %FichierLog%
                      goto :SORTIE) else (echo.)
                      REM Daily launch test based on the day of the week
                      if "%NomJour%"=="Saturday" (
                      echo %DateHeure% : No planned retrieval on %NomJour%. >> %FichierLog%
                      goto :SORTIE) else (echo.)

                      REM Initialization of the result file
                      set FichierResultat=ResultatTransfertsIniCaisses_%aaaammjj%.txt
                      echo Register;ClosingDate > %FichierResultat%

                      REM ----- LOOP ------------------------------------------
                      REM Loop to read the file of the list of registers
                      for /F "tokens=1,2 delims=;" %%i in (%FichierCaisses%) do (

                      REM Test of the 'already processed' indicator of the file of the list of registers
                      if %%j==O (echo %%i;O >> %FichierTemp%) else (

                      REM Connection with the register
                      net use \\%%i\ipc$ /user:%USERADMIN% %ADMPASXP% || net use \\%%i\ipc$ /user:%USERADMIN% %ADMPAS2K%
                      if ERRORLEVEL 1 (
                      echo %DateHeure% : %%i not connected >> %FichierLog%
                      echo %%i;N >> %FichierTemp%) else (

                      REM Retrieval of the last closing date of the register
                      call :DateClotureCaisse \\%%i\%Chemin%\%FichierData% %%i

                      )
                      REM End of connection with the register
                      net use \\%%i\ipc$ /d
                      )
                      REM End of the test of the 'already processed' indicator of the file of the list of registers
                      )
                      REM End of the loop reading the list of registers
                      REM -------------------------------------------------------

                      REM Updating the file of the list of registers
                      copy /Y %FichierTemp% %FichierCaisses%

                      REM Retrieval of the date and time
                      call :DateHeureSysteme

                      :SORTIE
                      REM Log message
                      echo %DateHeure% End of retrieval for %NomJour% >> %FichierLog%

                      REM Deleting temporary files
                      @del /q /f %FichierTemp%

                      REM End of main processing
                      goto :EOF
                      REM ==============================================================================================================

                      REM ----- SUBROUTINE ------------------------------------
                      :DateClotureCaisse
                      REM -------------------------------------------------------
                      REM Retrieval of the last closing date of the register
                      REM parameters: data file name, register name in 'VEG' format
                      REM
                      call :DateHeureSysteme
                      REM ----- LOOP ------------------------------------------
                      REM Loop to read the data file:
                      REM Searching for the date assigned to the clause 'DateDerniereCloture='
                      REM Initializing the variable 'DateCloture', null if the clause is not found
                      for /F "tokens=1,2 delims==" %%g in (%1) do (if %%g == DateDerniereCloture set DateCloture=%%h)
                      REM
                      REM Test of the 'DateCloture' variable:
                      REM Writing to the result file if the variable is populated
                      REM Updating the temporary file containing the list of already processed registers
                      if "%DateCloture%"=="" (
                      echo %DateHeure% : Failure when reading the closing date of the register %2 >> %FichierLog%
                      echo %2;N >> %FichierTemp%) else (
                      echo %2;%DateCloture% >> %FichierResultat%
                      echo %2;O >> %FichierTemp%)
                      REM
                      REM ----- End of the subroutine
                      goto :EOF

                      REM ----- SUBROUTINE ------------------------------------
                      :DateHeureSysteme
                      REM -------------------------------------------------------
                      REM Retrieval of the system date and time
                      REM according to OS version, Windows 2000 or XP
                      if "%OSVER:~18,2%"=="XP" (
                      set Annee=%DATE:~6,4%
                      set Mois=%DATE:~3,2%
                      set Jour=%DATE:~0,2%
                      set Heure=%TIME:~0,2%
                      set Minutes=%TIME:~3,2%
                      set Secondes=%TIME:~6,2%) else (
                      if "%OSVER:~18,4%"=="2000" (
                      set Annee=%DATE:~11,4%
                      set Mois=%DATE:~8,2%
                      set Jour=%DATE:~5,2%
                      set Heure=%TIME:~0,2%
                      set Minutes=%TIME:~3,2%
                      set Secondes=%TIME:~6,2%))
                      if %Heure% LSS 10 set Heure=0%Heure:~1,1%
                      REM
                      set DateHeure=%Jour%/%Mois%/%Annee% - %Heure%:%Minutes%:%Secondes%
                      set aaaammjj=%Annee%%Mois%%Jour%
                      REM
                      REM ----- End of the subroutine
                      goto :EOF

                      REM ----- SUBROUTINE ------------------------------------
                      :JourneeSemaineDate
                      REM -------------------------------------------------------
                      REM Retrieval of date elements such as
                      REM the leap year of the year,
                      REM the week number of the year,
                      REM the day number of the week
                      set yy=%1
                      set mm=%2
                      set dd=%3
                      set ly=0
                      set /a lx=%yy% %% 4
                      if %lx% EQU 0 set ly=1
                      set /a lx=%yy% %% 100
                      if %lx% EQU 0 set ly=0
                      set /a lx=%yy% %% 400
                      if %lx% EQU 0 set ly=1
                      if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
                      set /a dd=100%dd%%%100,mm=100%mm%%%100
                      set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,Jd=153*m+2
                      set /a Jd=Jd/5+dd+y*365+y/4-y/100+y/400-32045
                      set /a y=yy+4798,Jp=y*365+y/4-y/100+y/400-31738,t=Jp+3,Jp=t-t%%7
                      set /a y=yy+4799,Jt=y*365+y/4-y/100+y/400-31738,t=Jt+3,Jt=t-t%%7
                      set /a y=yy+4800,Jn=y*365+y/4-y/100+y/400-31738,t=Jn+3,Jn=t-t%%7
                      set /a Jr=%Jp%,yn=yy-1,yn+=Jd/Jt,yn+=Jd/Jn
                      if %Jd% GEQ %Jn% (set /a Jr=%Jn%) else (if %Jd% GEQ %Jt% set /a Jr=%Jt%)
                      set /a diff=Jd-Jr,cw=diff/7+1,wd=diff%%7,wd+=1
                      if %cw% LSS 10 set cw=0%cw%
                      REM
                      set %Bissextile%=%ly%
                      set Semaine=%cw%
                      set Journee=%wd%
                      REM echo yyyy: [%yy%] mm: [%mm%] dd: [%dd%] wd: [%wd%] cw: [%Semaine%] ly: [%Bissextile%]
                      REM
                      REM ----- End of the subroutine
                      goto :EOF
                      3
                      1. I've got a database in SQL Server and I want to add data to a certain number of tables, but I'm using Access XP. In fact, I'm linking tables to the SQL Server database via ODBC. What security settings should I apply to the tables to enable this?

                        0
                        1. To retrieve the date (or time), without using a tmp file:

                          for /F "tokens=1* delims=@" ('date /T') do set date=%%a
                          delims=@ allows you to get everything into a single variable
                          otherwise, you can use multiple delimiters and retrieve the fields individually.

                          for explanations: help for

                          only works under W/NT and above (not under W98)
                          0
                          1. Aaaaaaah finally the solution that works on my system

                            I was starting to despair...

                            Thank you Yazul
                            0
                        2. Hello,
                          under Win98, it's not working.
                          I want to rename a file with today's date.
                          All of this executed in a .bat

                          Does anyone have an idea?
                          0
                          1. Oufff after tinkering I made it :)
                            I propose the following code:

                            @Echo off SET ten=A%time:~0,1%B if A1B==%ten% goto OK if A2B==%ten% goto OK :ZERO SET ten=0 goto fin :OK SET ten=%time:~0,1% goto fin :FIN rename "%userprofile%\desktop\file.xls" file_%Date:~-4%%Date:~-7,-5%%Date:~-10,-8%_%ten%%Time:~-10,-9%%Time:~-8,-6%%Time:~-5,-3%.xls


                            THANK you very much for your help!!
                            and thank you also for the excellent link.
                            Take care :)
                            1
                            1. Just a heads up... The problem with the SET - DATE and TIME commands is their "non-portability" in certain cases. Indeed, as I mentioned a bit earlier, a batch containing these commands might not work under W95, W98, 2000, or even XP, depending on the version of DOS that is embedded.
                              That’s why, in my opinion, the ideal solution would be to use small functions (or exe and com programs) found in the Unix world or to create them yourself, which would be usable on any platform.

                              If you have any ideas in that direction... I'm all ears. Thanks in advance.

                              For example, vardate.exe would return, depending on its parameters, a variable in different formats like DD_mm_yy or HH_dd_mm or even MON (for Monday=) etc. etc. etc.
                              0
                              • 1
                              • 2