3 Replies Latest reply on May 27, 2015 7:25 AM by Mark Fraser

    Append Today's date to file name (batch file)

    Mark Fraser

      Executive Summary: How to add today's date (ISO format YYYYMMDD) onto the file name in a batch job (Windows only)


      note: The reason I want to output in YYYYMMDD format is so users can sort the file names in date order, they can't sort properly (using file name) in DDMMYYY format.

       

      The file is automatically created via tabcmd/ batch file and I want to automatically append today's date onto the file name in the batch file, here's how I did it -

       

      This is the pure code (the underscores aren't required, they just help identify between what is file name and what is code)

      _%date:~-4,4%%date:~-7,2%%date:~-10,2%_

       

      With an example, my file name is My First Report and I want to save it as PDF.

      "C:\markf\My Documents\My First Report_%date:~-4,4%%date:~-7,2%%date:~-10,2%_.pdf"

       

      A useful trick for testing the output...

      Open Command Prompt      (Windows+R then type cmd)

       

      add echo in front of the file name >> echo My First Report_%date:~-4,4%%date:~-7,2%%date:~-10,2%.pdf

      As you can see below, it will display the output file name

      Its quite possible to add time as well, reformat the date etc. but I am not an expert on batch files and/or command lines, I needed the above, so that's all I did!

      Please feel free to share ideas/ comments/ alternatives.

       

      Hope that's clear and helpful

       

      Updated with correction 28th May 2015.

       

      Cheers

      Mark

        • 1. Re: Append Today's date to file name (batch file)
          Matt Coles

          Nice trick! I had to mess with my own Powershell scripts last night for this very need, as it happens, so here's a Powershell-based solution for the same problem in the event that it's useful to anyone:

           

           

          # Get date values
          $Date = Get-Date
          [String]$Year = $Date.Year
          [String]$Month = $Date.Month
          [String]$Day = $Date.Day
          
          
          # Pad date parts with leading 0's
          If ( $Month.ToString().Length -lt 2 ) { $Month = '0' + $Month }
          If ( $Day.ToString().Length -lt 2 ) { $Day = '0' + $Day }
          
          
          # Output
          "$Year$Month$Day"
          
          
          
          
          
          
          
          • 2. Re: Append Today's date to file name (batch file)
            Jeff Strauss

            thanks Mark.  Another way to do it that we have deployed is to create a windows variable that then can be used anywhere within the script.

             

            REM create variable ldt

            for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j

            set ldt=%ldt:~0,4%-%ldt:~4,2%-%ldt:~6,2%

             

            ...

             

            REM use the varaible ldt

            tabcmd export "xxx" -f "G:\testthis%ldt%.pdf" --fullpdf

            • 3. Re: Append Today's date to file name (batch file)
              Mark Fraser

              I'm still looking for a best practice... mine works but as to how efficient it is, I don't know?!

              I'm sure there are multiple ways to achieve it and thank you for sharing your methods! I hope this thread is useful to others in the same situation.

               

              Jeffrey, I recognize part of the code/ your method from reading about this today, I will experiment with your method tomorrow, expect questions