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. date /T > date.txt

    you will almost have what you want in the file date.txt:
    Thu 07/24/2003
    0
    1. Yes, but the problem is that I want to use this date to name a file, so the "/" is not great!
      0
    2. @van'What programming languages do you use? DOS? Java? C/C++? Others?
      0
    3. @van'

      Does anyone know how to rename a directory with today's date???

      0
  3. Dans ton fichier date.txt, tu fais Édition > Rechercher > et tu remplaces "/" par "_".
    0
    1. I think he would like to do it automatically from DOS (like with awk commands in Unix) but in DOS, in my opinion, it's a lost cause...
      0
    2. It's "He" not "She", but oh well, it's not a big deal...
      Well, if no one has any ideas, I'll do it differently....
      0
    3. @van'Hello,

      to achieve what you're asking, you need to use environment variables in your batch file.

      example toto.bat
      rem*** assign to the variable mon_fic a file name (and its path) formatted in the following way jour.txt ***
      set mon_fic = c:\dossier1\mesfichiers\%DATE:~0,-8%.txt

      rem ** write hello in the file **
      echo hello > %mon_fic%

      to vary the string, you need to adjust the "0,-8" part of the command...
      0 = from the first character
      -8 = except the last 8

      you can find different ways to format the date by typing the command in dos: set /help

      There you go, good luck...
      0
    4. @poulpenstringThank you for this post poulpenstring, it's exactly what I was looking for, thanks again.
      ruxpin
      0
    5. @van'sorry ...

      mirza
      0
  4. I tried the command under Dos but it creates a %Date%.txt file
    can someone help me?
    0
    1. Go to the site
      http://www.chez.com/scudo/Faq/dos/
      there is the answer... it's powerful!
      It creates a variable _d that contains the date. Then you'll have to manipulate a bit as written above to make it what you want.

      Mbt
      0
  5. command /e:2000 /c for %%i in (1 2) do prompt set _d=$d$_ | find "$" /v >{t}.bat
    for %%c in (call del) do %%c {t}.bat
    0
    1. I tried the method provided by poulpenstring. It works for responding to toto. We created a *.bat

      set my_file=d:\tes-%Date:~-4%%Date:~-7,-5%%Date:~-10,-8%.txt
      echo head> %my_file%

      it generates a file tes-20031208.txt. In DOS, by typing help set
      you will get the online help.
      Zorti
      7
      1. Hello,

        Just a quick note regarding the manipulation with dates... I just noticed that unfortunately, depending on the operating system, the results are different, or even non-functional. This is specifically about "DOS," which can vary depending on whether you are using ... win95, win98, win98SE, 2000, or XP.

        For example, under 95 and certain versions of 98, the function "date /T" does not work, and it is impossible to get the date without waiting for a response... (that's what the /T is for, I believe).

        Anyway, there is always a possibility, or even to look online for Unix functions that work under MS/DOS.

        Have a good evening.
        2
        1. Hello everyone, I would like to know some uses of DOS for programming!
          What can we do with DOS?
          Do you have any programs?
          Can we create an exe file with DOS?
          Thank you in advance for your answers
          Happy New Year to everyone and good continuation!
          See you later
          Tom
          0
          1. Hello;
            How to rename a file
            e.txt to 12:59.txt using a .bat file
            e.txt: file to rename
            12:59.txt: desired name, 12:59 is the current time
            It would be fabulous for automating backups...
            0
            1. Hello everyone :)

              I used this topic to create an automatic backup batch.
              The file to be backed up will be renamed with the date and then the time.

              The code is as follows:
              rename "%userprofile%\desktop\FILE.xls" FILE.xls_%Date:~-4%%Date:~-7,-5%%Date:~-10,-8%_%Time:~-11,-9%%Time:~-8,-6%%Time:~-5,-3%.xls


              Which gives a file named:
              FILE_20040420_114238.xls

              Everything works perfectly from 10:00.00 to 23:59.59 :))
              But the moment the ten's digit (for the hour) goes to 0 (that is, 00h, 01h, up to 09h), it causes an error :/

              FILE_20040420_

              Because the variable that contains the ten's digit is actually half empty; it consists of:
              %Time:~-11,-10% => ten's digit
              %Time:~-10,-9% => unit's digit
              When there is no ten's digit, well there is something other than 1 or 2, but what?

              FILE_20040420_?14238.xls

              What is this character??

              If I know it, I can replace it with 0:

              SET ten %Time:~-11,-10% If %ten% == "unknown_character" SET %ten% 0


              Thank you very much for any help or advice you can provide!
              4
              1. I tried to replace the variable in question: if it contains neither 1 nor 2, I set it to 0 :)
                but I can't even test it... :(
                help !!

                @Echo off SET dizaine=%Time:~-11,-10% @echo original var : %dizaine% IF %dizaine% == 1 goto fin IF %dizaine% == 2 goto fin SET dizaine=0 GOTO fin :fin @echo var after test : %dizaine% pause
                0
                1. Hello,
                  Which OS are you on? I tested your batch, but apparently zero does not exist in %time%, so the first character is... either the tens or the units (so it’s not possible to test). I will take a look at your problem. In my opinion, it would be best to start by testing the number of characters before the : (colon). How? We’ll see...

                  Have a good day.
                  0
                2. Hello :)
                  I'm using Win2k.

                  So, what you're telling me is that when I want to fill the variable "dizaine":
                  SET dizaine=%Time:~-11,-10%

                  it contains either 1 or 2, or it's not defined?
                  Yet, when I display the time, there's a "space":
                  _9:48
                  and not
                  9:48
                  but when I try to display it alone, it indeed throws an error, I believe.

                  How can I figure out what this damn character is? Does it even exist? I'm honestly quite lost, even with all the good intentions in the world :/

                  I have no idea how to test the number of characters in a string :(

                  On the other hand, I tried something like
                  if not defined set dizaine à 0

                  but that didn't work...

                  Thanks for the helping hand :)
                  0
              2. Hi,
                If you want to play with batch files, check out the website http://www.robvanderwoude.com/
                You will find everything about everything.

                See you!
                1
                1. Hello Mbt,

                  Indeed, this site seems very interesting... So it should be immediately added to Favorites. Thank you

                  Have a good evening
                  0
              3. Here is a way...
                try the following batch and modify it as needed...
                especially for creating the file, I didn't take care of that part...

                it's a bit of a stretch but it works...

                ______________________________________________________

                echo off
                rem *** add a zero in front of the hour****
                set wcontrol=%time:~0,1%
                set wcontrol=A%wcontrol%B
                echo %wcontrol%

                if A1B==%wcontrol% goto OK
                if A2B==%wcontrol% goto OK

                :ZERO
                echo 0%time:~1,10%.xls
                goto fin

                :OK
                echo %time%.xls
                goto fin

                :FIN
                echo on


                ______________________________________________________

                Good luck and keep me posted.
                0
                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. 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. 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. 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. 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. 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. 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
                          • 1
                          • 2